azure - 如何预先检测Azure ARM脚本是否会更改customData?

标签 azure azure-devops azure-resource-manager azure-rm-template cloud-init

我正在尝试使用 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/

相关文章:

java - 用于 Java 代理的 ADAL

azure - 创建 Azure Active Directory 应用程序需要什么权限?

powershell - 如何使用个人访问 token 向 Visual Studio Team Services 和 Team Foundation Server 进行身份验证?

azure - 多个虚拟机可以在 azure 中使用单个 NIC 吗?

azure 政策: only allow certain tag values in azure resources group tag

javascript - 如何使用Azure移动服务REST api?

c# - 如何重放已通过Azure服务总线处理过的消息?

Azure 粘性 session /sourceIP 负载平衡和实例探测

git - 如何指定 YAML Pipeline 的保存位置?

c# - Azure 资源管理器 - backupLongTermRetentionPolicies - 错误 "WeekOfYear is required to be set between 1 and 52 in order to set yearly retention."