• Uncategorized

About linux : Azure-ARM-Template-to-Create-Linux-VM-and-Add-Line-to-etchosts

Question Detail

We have an ARM template that correctly creates a Linux VM via a powershell command:

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

We would like to leverage the CustomScriptForLinux extension to add a line /etc/hosts

Here is the relevant part of the 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"
           }
    }
  }
],

The deployment succeeds without error, but the /etc/hosts file does not have the new line.

The output from /var/log/azure/Microsoft.OSTCExtensions.CustomScriptForLinux/1.5.2.2/extension.log includes:

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

Removing ‘sudo’ from the command makes no difference.

Question Answer

Turns out you need to use bash -c "echo '10.X.X.X sample.acme.com' >> etc/hosts" to make linux parse the input from script extension properly

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.