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

时间:2018-03-20 16:09:19

标签: 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'没有任何区别。

1 个答案:

答案 0 :(得分:0)

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