简体   繁体   中英

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

  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"
                }
            }
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM