Azure ARM - DSC 虚拟机配置

Azure ARM - DSC VM configuration

我想使用 ARM 模板和 DSC 配置我的 VM。我在 powershell 中准备了简单的 DCS 脚本,基于使用 powershell 命令创建的 .zip 文件。提到的 .zip 文件上传到存储帐户容器。现在我想使用这个 .zip 文件在我的 ARM 模板下面对我的测试虚拟机进行配置更改。我收到错误消息 New-AzResourceGroupDeployment : 10:12:09 AM - VM has reported a failure when processing extension 'dscExtension'. Error message: "The DSC Extension failed to execute: Error downloading https://storageAccountName.blob.core.windows.net/containerName/test.zip after 2 attempts: <?xml version="1.0" encoding="utf-8"?><Error><Code>ResourceNotFound</Code><Message>The specified resource does not exist.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "List of virtual machines to be reconfigured, if using multiple VMs, make their names comma separate. E.g. VM01, VM02, VM03."
            },
            "defaultValue": "VM1,VM2"
        },
        "Location": {
            "type": "string",
            "metadata": {
                "description": "Location of the VM"
            },
            "defaultvalue": "WestEurope"
        },
        "functionName": {
            "type": "string",
            "metadata": {
                "description": "Specify the function name"
            },
            "defaultvalue": "test.ps1\testConfigurationName"
        },
        "storageAccountName": {
            "type": "string",
            "metadata": {
                "description": "Specify the Storage Account name, Storage Account where DCS .zip module is located"
            }
        },
        "setupScriptContainerName": {
            "type": "string",
            "metadata": {
                "description": "Specify the Storage Account container name, container where DCS .zip module is located"
            }
        },
        "DSCSetupArchiveFileName": {
            "type": "string",
            "metadata": {
                "description": "Specify the Storage Account container name, container where DCS .zip module is located"
            },
            "defaultvalue": "test.zip"
        },
        "nodeConfigurationName": {
            "type": "string",
            "metadata": {
                "description": "The name of the node configuration, on the Azure Automation DSC pull server, that this node will be configured as"
            },
            "defaultValue": "testConfigurationName.localhost"
        },
        "registrationKey": {
            "type": "securestring",
            "metadata": {
                "description": "Registration key to use to onboard to the Azure Automation DSC pull/reporting server"
            },
            "defaultValue": "AutomationAccountPrimaryKey"
        },
        "registrationUrl": {
            "type": "string",
            "metadata": {
                "description": "Registration url of the Azure Automation DSC pull/reporting server"
            },
            "defaultValue": AutomationAccountRegistrationURL"
        }
    },
    "variables": {
        "vmListArray": "[split(parameters('vmName'),',')]"
    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2015-06-15",
            "name": "[concat(trim(variables('vmListArray')[copyIndex()]),'/dscExtension')]",
            "copy": {
                "name": "ExtentionLooptoAllVMs",
                "count": "[length(variables('vmListArray'))]"
            },
            "location": "[parameters('Location')]",
            "properties": {
                "autoUpgradeMinorVersion": true,
                "publisher": "Microsoft.Powershell",
                "type": "DSC",
                "typeHandlerVersion": "2.19",
                "protectedSettings": {
                    "Items": {
                        "registrationKeyPrivate": "[parameters('registrationKey')]"
                    }
                },
                "settings": {
                    "ModulesUrl": "[concat('https://',parameters('storageAccountName'),'.blob.core.windows.net/',parameters('setupScriptContainerName'),'/',parameters('DSCSetupArchiveFileName'))]",
                    "ConfigurationFunction": "[parameters('functionName')]",
                    "Properties": [
                        {
                            "Name": "RegistrationKey",
                            "Value": {
                                "UserName": "PLACEHOLDER_DONOTUSE",
                                "Password": "PrivateSettingsRef:registrationKeyPrivate"
                            },
                            "TypeName": "System.Management.Automation.PSCredential"
                        },
                        {
                            "Name": "RegistrationUrl",
                            "Value": "[parameters('registrationUrl')]",
                            "TypeName": "System.String"
                        },
                        {
                            "Name": "NodeConfigurationName",
                            "Value": "[parameters('nodeConfigurationName')]",
                            "TypeName": "System.String"
                        }
                    ]
                }
            }
        }
    ]
}

更新版本:

"resources": [
      {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "apiVersion": "2018-10-01",
          "name": "[concat(trim(variables('vmListArray')[copyIndex()]),'/dscExtension')]",
          "copy": {
              "name": "ExtentionLooptoAllVMs",
              "count": "[length(variables('vmListArray'))]"
          },
          "location": "[parameters('Location')]",
          "properties": {
              "autoUpgradeMinorVersion": true,
              "publisher": "Microsoft.Powershell",
              "type": "DSC",
              "typeHandlerVersion": "2.9",
              "protectedSettings": {
                  "Items": {
                      "registrationKeyPrivate": "[parameters('registrationKey')]"
                  }
              },
              "settings": {
                  "configuration": {
                      "url": "[concat('https://',parameters('storageAccountName'),'.blob.core.windows.net/',parameters('setupScriptContainerName'),'/',parameters('DSCSetupArchiveFileName'))]",
                      "script": "[parameters('scriptName')]",
                      "function": "[parameters('functionName')]"
                  },
                  "Properties": [
                      {
                          "Name": "RegistrationKey",
                          "Value": {
                              "UserName": "PLACEHOLDER_DONOTUSE",
                              "Password": "PrivateSettingsRef:registrationKeyPrivate"
                          },
                          "TypeName": "System.Management.Automation.PSCredential"
                      },
                      {
                          "Name": "RegistrationUrl",
                          "Value": "[parameters('registrationUrl')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "NodeConfigurationName",
                          "Value": "[parameters('nodeConfigurationName')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "ConfigurationMode",
                          "Value": "[parameters('configurationMode')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "ConfigurationModeFrequencyMins",
                          "Value": "[parameters('configurationModeFrequencyMins')]",
                          "TypeName": "System.Int32"
                      },
                      {
                          "Name": "RefreshFrequencyMins",
                          "Value": "[parameters('refreshFrequencyMins')]",
                          "TypeName": "System.Int32"
                      },
                      {
                          "Name": "RebootNodeIfNeeded",
                          "Value": "[parameters('rebootNodeIfNeeded')]",
                          "TypeName": "System.Boolean"
                      },
                      {
                          "Name": "ActionAfterReboot",
                          "Value": "[parameters('actionAfterReboot')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "AllowModuleOverwrite",
                          "Value": "[parameters('allowModuleOverwrite')]",
                          "TypeName": "System.Boolean"
                      }
                  ]
              }
          }
      }
  ]

DSC部分:

Configuration SetRegistryxxx {


    Node 'localhost' {
         Registry configxxx {
                Ensure = "Present"
                Key = "HKLM:\xx"
                ValueName = "xx"
                ValueData = "http://0.0.0.0:xxx
                ValueType = "String"
        }
        Registry configxxx {
                Ensure = "Present"
                Key = "HKLM:\xx"
                ValueName = "xx"
                ValueData = "http://0.0.0.0:xx"
                ValueType = "String"
        }
    }
}

根据错误,您无法从您使用的 Azure blob 存储帐户下载 zip 文件。请为 blob 或 set the blob access level to Public.

创建一个 sas 令牌

例如

 "resources": [
    {
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "name": "[concat(parameters('vmName'),'/Microsoft.Powershell.DSC')]",
      "apiVersion": "2015-06-15",
      "location": "[parameters('location')]",
      "properties": {
        "publisher": "Microsoft.Powershell",
        "type": "DSC",
        "typeHandlerVersion": "2.19",
        "autoUpgradeMinorVersion": true,
        "protectedSettings": {
          "Items": {
            "registrationKeyPrivate": "[parameters('registrationKey')]"
          }
        },
        "settings": {
          "ModulesUrl": "<the url of you azure blob>",
          "SasToken": "<the sas token for the blob>",
          "ConfigurationFunction": "[parameters('configurationFunction')]",
        ...
}
]

详情请参考document and the template