如何使用Ansible将自定义脚本扩展添加到Azure VM

时间:2018-02-10 16:44:52

标签: windows powershell azure ansible

关于https://superuser.com/questions/1210215/how-to-bootstrap-windows-hosts-with-remote-powershell-for-use-with-ansible的建议我试图将自定义脚本扩展添加到现有VM。

以下是我的剧本

- 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

这是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"
  }
}

当我运行剧本时,我在azure上得到以下错误

  

请求内容无效,无法反序列化:'可以   找不到会员'发布者'对象'模板'。路径   ' properties.template.publisher',第1行,第64位。'。

任何人都可以指出我正确的方向吗?

由于

1 个答案:

答案 0 :(得分:1)

  1. 您仍需提供有效的模板
  2. 您需要为资源提供正确的类型,extensions不是正确的类型
  3. 您的名字必须包含vm名称,因为这是模板应该如何确定哪个vm应用此扩展名。
  4. 示例:

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