linux - 用于创建 Linux VM 并向/etc/hosts 添加行的 Azure ARM 模板

标签 linux powershell azure arm-template

我们有一个 ARM 模板,可以通过 powershell 命令正确创建 Linux VM:

New-AzureRmResourceGroupDeployment -TemplateFile .\template.json -TemplateParameterFile .\parameters.json -ResourceGroupName my-rg -virtualMachineName my-test-vm

我们想利用 CustomScriptForLinux 扩展添加一行/etc/hosts

这是 template.json 的相关部分:

{
      "name": "[concat(parameters('virtualMachineName'),'/edithosts')]",
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-06-15",
      "dependsOn": [
          "[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
      ],
      "properties": {
            "publisher": "Microsoft.OSTCExtensions",
             "type": "CustomScriptForLinux",
             "typeHandlerVersion": "1.5",
             "autoUpgradeMinorVersion": true,
             "settings": {
               "commandToExecute": "sudo echo '10.X.X.X  sample.acme.com' >> /etc/hosts"
           }
    }
  }
],

部署成功,没有报错,但是/etc/hosts 文件没有换行。

/var/log/azure/Microsoft.OSTCExtensions.CustomScriptForLinux/1.5.2.2/extension.log 的输出包括:

Command to execute:sudo echo '10.X.X.X  sample.acme.com' >> /etc/hosts
2018/03/20 15:22:29 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.2] Command is finished.
2018/03/20 15:22:29 ---stdout---
2018/03/20 15:22:29 10.X.X.X  sample.acme.com >> /etc/hosts
2018/03/20 15:22:29
2018/03/20 15:22:29 ---errout---
2018/03/20 15:22:29
2018/03/20 15:22:29

从命令中删除“sudo”没有任何区别。

最佳答案

原来你需要使用 bash -c "echo '10.X.X.X sample.acme.com' >> etc/hosts" 让 linux 正确解析来自脚本扩展的输入

关于linux - 用于创建 Linux VM 并向/etc/hosts 添加行的 Azure ARM 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49389081/

相关文章:

linux - awk'BEGIN {sum = 0} END {print sum}'文件名的输出是什么?

c# - MobileServiceTable.InsertAsync ZUMO-API-版本错误

azure - VM扩展配置错误:while creating TeamServicesAgent extension on azure

azure - 将 ADF 权限添加到 ARM 中的 key 保管库

linux - 我的 drupal 仅在我在线时运行

java - 运行 Jitsi-meet [jicofo 组件] 时出现问题?

powershell - Excel自动化速度慢?

powershell - ErrorVariable无法使用null或空参数

linux - 用于验证备份文件夹是否存在的 Bash 脚本

powershell - 如何通过对象的属性递归枚举?