我正在尝试在 Azure 上部署一个二头肌文件,以便它创建一个带有 Windows 操作系统的虚拟机,安装 IIS,从公共 GitHub 存储库下载代码,重新启动计算机。我可以通过 DSC 扩展完成除最后一步之外的所有步骤。最后一步是必需的,否则网站将不会显示下载的代码。对于最后一步,我使用了 runcommand,但我不断收到无效的 Url(错误请求)。
作为故障排除步骤,我什至将重新启动命令更改为 echo 命令,但我不断收到相同的错误。
为了创建此脚本,我从 https://github.com/Azure/azure-quickstart-templates/tree/master/demos/vmss-windows-webapp-dsc-autoscale 获得灵感,并针对虚拟机对其进行了调整.
这是代码:
@description('Name of the virtual machine.')
param vmName string = 'vm1'
param location string = resourceGroup().location
@description('Size of the virtual machine.')
param vmSize string = 'Standard_D2s_v3'
param adminUsername string = 'adminUser'
param adminPassword string = 'some-secret-password'
param vNetName string ='vnet1'
param subnetName string='subnet1'
param nsgName string='nsg1'
var nicName = '${vmName}-nic'
resource nsg 'Microsoft.Network/networkSecurityGroups@2024-03-01' existing = {
name: nsgName
}
resource nic 'Microsoft.Network/networkInterfaces@2022-05-01' = {
name: nicName
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: resourceId('Microsoft.Network/virtualNetworks/subnets', vNetName, subnetName)
}
}
}
]
networkSecurityGroup:{
id: nsg.id
}
}
}
resource vm 'Microsoft.Compute/virtualMachines@2022-03-01' = {
name: vmName
location: location
properties: {
hardwareProfile: {
vmSize: vmSize
}
osProfile: {
computerName: vmName
adminUsername: adminUsername
adminPassword: adminPassword
}
storageProfile: {
imageReference: {
publisher: 'MicrosoftWindowsServer'
offer: 'WindowsServer'
sku: '2019-Datacenter'
version: 'latest'
}
osDisk: {
createOption: 'FromImage'
managedDisk: {
storageAccountType: 'Standard_LRS'
}
}
}
networkProfile: {
networkInterfaces: [
{
id: nic.id
}
]
}
}
}
resource vmExtension 'Microsoft.Compute/virtualMachines/extensions@2022-03-01' = {
parent: vm
name: 'IIS'
location: location
properties: {
publisher: 'Microsoft.Powershell'
type: 'DSC'
typeHandlerVersion: '2.9'
autoUpgradeMinorVersion: true
settings: {
configuration: {
url: 'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/refs/heads/master/demos/vmss-windows-webapp-dsc-autoscale/DSC/InstallIIS.zip'
script: 'InstallIIS.ps1'
function: 'InstallIIS'
}
configurationArguments: {
nodeName: 'localhost'
WebDeployPackagePath: 'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/refs/heads/master/demos/vmss-windows-webapp-dsc-autoscale/WebDeploy/DefaultASPWebApp.v1.0.zip'
}
}
}
}
resource rebootCommand 'Microsoft.Compute/virtualMachines/runCommand@2024-07-01' = {
parent: vm
name: 'reboot'
location: location
properties: {
asyncExecution: false
source: {
script: 'echo Hello World'
}
}
}
通过 VSCode 在二头肌模板中执行“runCommand@2024-07-01”时出现“无效网址”错误请求
感谢 thomas 通过提及 depends_on 的使用指出了对该问题的正确见解。
这里运行模块的操作取决于虚拟机扩展安装的状态。运行命令的提供应在部署扩展之后完成。我广泛使用模块化方法,以便根据部署顺序实现更顺畅的操作。
演示配置:
主要.二头肌:
@description('Name of the virtual machine.')
param vmName string = 'vm1'
@description('Azure region for all resources.')
param location string = resourceGroup().location
@description('Size of the virtual machine.')
param vmSize string = 'Standard_D2s_v3'
@description('Admin username for the virtual machine.')
param adminUsername string = 'adminUser'
@secure()
@description('Admin password for the virtual machine.')
param adminPassword string
@description('Name of the virtual network.')
param vNetName string = 'vnet1'
@description('Name of the subnet.')
param subnetName string = 'subnet1'
@description('Name of the network security group.')
param nsgName string = 'nsg1'
module networkModule 'modules/network.bicep' = {
name: 'networkDeployment'
params: {
location: location
vNetName: vNetName
subnetName: subnetName
nsgName: nsgName
vmName: vmName
}
}
module vmModule 'modules/vm.bicep' = {
name: 'vmDeployment'
params: {
location: location
vmName: vmName
vmSize: vmSize
adminUsername: adminUsername
adminPassword: adminPassword
nicId: networkModule.outputs.nicId
}
}
module iisModule 'modules/iis.bicep' = {
name: 'iisDeployment'
params: {
location: location
vmName: vmName
}
dependsOn: [
vmModule
]
}
output vmName string = vmModule.outputs.vmName
output nicId string = networkModule.outputs.nicId
output vnetName string = networkModule.outputs.vnetName
output nsgName string = networkModule.outputs.nsgName
模块/iis.bicep:
// modules/iis.bicep
param location string
param vmName string
resource vm 'Microsoft.Compute/virtualMachines@2023-03-01' existing = {
name: vmName
}
resource iisExtension 'Microsoft.Compute/virtualMachines/extensions@2023-03-01' = {
parent: vm
name: 'IIS'
location: location
properties: {
publisher: 'Microsoft.Powershell'
type: 'DSC'
typeHandlerVersion: '2.9'
autoUpgradeMinorVersion: true
settings: {
configuration: {
url: 'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/demos/vmss-windows-webapp-dsc-autoscale/DSC/InstallIIS.zip'
script: 'InstallIIS.ps1'
function: 'InstallIIS'
}
configurationArguments: {
nodeName: 'localhost'
WebDeployPackagePath: 'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/demos/vmss-windows-webapp-dsc-autoscale/WebDeploy/DefaultASPWebApp.v1.0.zip'
}
}
}
}
resource rebootCommand 'Microsoft.Compute/virtualMachines/runCommands@2023-03-01' = {
parent: vm
name: 'reboot'
location: location
properties: {
asyncExecution: false
source: {
script: 'Write-Host "Rebooting the machine"; Restart-Computer -Force'
}
timeoutInSeconds: 1800
}
dependsOn: [
iisExtension
]
}
配置:
参考: