Radfd13
Radfd13

Reputation: 185

How to add Custom Script Extension to Azure VM using Ansible

Following advice on https://superuser.com/questions/1210215/how-to-bootstrap-windows-hosts-with-remote-powershell-for-use-with-ansible I am trying to add Custom Script extension to existing VM.

Below is my playbook

- name: Create VM playbook
  hosts: localhost
  connection: local
  tasks:

  - name: Custom Script Extension
    azure_rm_deployment:
      state: present
      location: 'uk west'
      resource_group_name: 'AnsibleRG'
      template: "{{ lookup('file', '/etc/ansible/playbooks/extension.json') | from_json }}"
      deployment_mode: incremental

This is extension.json

{
  "publisher": "Microsoft.Compute",
  "type": "CustomScriptExtension",
  "typeHandlerVersion": "1.4",
  "settings": {
    "fileUris": [
      "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
    ],
    "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File ConfigureRemotingForAnsible.ps1"
  }
}

When I run the playbook I get following error on azure

The request content was invalid and could not be deserialized: 'Could not find member 'publisher' on object of type 'Template'. Path 'properties.template.publisher', line 1, position 64.'.

Can anyone please point me in right direction?

Thanks

Upvotes: 1

Views: 2482

Answers (1)

4c74356b41
4c74356b41

Reputation: 72176

  1. You still need to provide a valid template
  2. You need to provide proper type for the resource, extensions isnt a proper type
  3. Your name has to include the vm name, as this is how the template supposed to figure out which vm apply this extension to.

Example:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "name": "[concat(parameters('vmName'),'/ConfigureRemotingForAnsible')]",
            "apiVersion": "2015-06-15",
            "location": "[resourceGroup().location]",
            "properties": {
                "publisher": "Microsoft.Compute",
                "type": "CustomScriptExtension",
                "typeHandlerVersion": "1.8",
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "fileUris": [
                        "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
                    ],
                    "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File ConfigureRemotingForAnsible.ps1"
                }
            }
        }
    ]
}

Upvotes: 1

Related Questions