我正在尝试使用 Azure DevOps 发布管道、Azure ARM 模板和 cloud-init 脚本在 Azure 中部署 Ubuntu 虚拟机。
我设置了一个脚本,该脚本首先使用 AzureResourceManagerTemplateDeployment@3
任务验证 ARM 模板,然后使用 AzurePowerShell@5
运行 Get-AzResourceGroupDeploymentWhatIfResult
命令code> 任务,最后使用另一个 AzureResourceManagerTemplateDeployment@3
任务部署模板。如果虚拟机不存在,这一切第一次都会起作用。
第二次及后续尝试时会出现问题。如果 cloud-init 模板已更改验证任务传递,则 Get-AzResourceGroupDeploymentWhatIfResult
看不到任何相关更改,但部署任务会失败并出现以下错误:
2020-07-26T13:47:33.2408130Z ##[error]At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.
2020-07-26T13:47:33.2424660Z ##[error]Details:
2020-07-26T13:47:33.2426481Z ##[error]PropertyChangeNotAllowed: Changing property 'customData' is not allowed.
2020-07-26T13:47:33.2429346Z ##[error]Check out the troubleshooting guide to see if your issue is addressed: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-resource-group-deployment?view=azure-devops#troubleshooting
2020-07-26T13:47:33.2431470Z ##[error]Task failed while creating or updating the template deployment.
显然这是因为我更改了cloud-init脚本。如果我随后删除虚拟机并重新部署,它就会按预期工作。
我想检测这种情况并在部署失败之前删除虚拟机,以便仅在需要时重新创建虚拟机。验证部署和 Get-AzResourceGroupDeploymentWhatIfResult 似乎都没有解决这种情况。有什么想法可以做到这一点吗?
问完这个问题后,我有了另一个聪明的想法。这是调用 (get-azvm -name my-vm-name).OSProfile.CustomData
并将结果与部署即将应用的自定义数据进行比较。唉,这也不起作用,因为 cmdlet 不返回创建虚拟机所用的自定义数据。
最佳答案
Terraform通常非常擅长检测需要重新创建资源的更改。如果您愿意从 ARM 模板切换,azurerm_linux_virtual_machine
将是要尝试的元素。
custom_data - (Optional)
The Base64-Encoded Custom Data which should be used for this Virtual Machine. Changing this forces a new resource to be created.
关于azure - 如何预先检测Azure ARM脚本是否会更改customData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63101270/