1

我正在尝试使用 Ansible 为 Azure 自动化设置管道,一切正常,除非我尝试将公共 IP 地址放入变量以与 add_host 一起使用。这是我的示例任务:

---
- name: Get Public IP
  azure_rm_publicipaddress_facts:
    resource_group: '{{ my_resource_group }}'
    name: '{{ my_name }}'
  register: azure_ip

- debug: var=azure_ip verbosity=2

- name: Add new instance to host group
  add_host:
    hostname: '{{ item.ipAddress }}'
    groupname: launched
  with_items: azure_ip.azure_publicipaddresses

这给了我以下错误:

致命的:[本地主机]:失败!=> {"failed": true, "msg": "'args' 字段的值无效,似乎包含未定义的变量。

但是值在那里,如图所示:

TASK [azure : debug] ***********************************************************
task path: mytest/roles/azure/tasks/get_ip.yml:14
ok: [localhost] => {
    "azure_ip": {
        "ansible_facts": {
            "azure_publicipaddresses": [
                {
                    "etag": “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                    "id": "/subscriptions/0000000000000/resourceGroups/myrgrp/providers/Microsoft.Network/publicIPAddresses/my_ip_001",
                    "location": “euwest",
                    "name": “my_ip_001",
                    "properties": {
                        "idleTimeoutInMinutes": 4,
                        "ipAddress": “20.113.125.63",
                        "ipConfiguration": {
                            "id": "/subscriptions/000000000000/resourceGroups/mygrprgrp/providers/Microsoft.Network/networkInterfaces/myrgrp-nic001/ipConfigurations/default"
                        },
                        "provisioningState": "Succeeded",
                        "publicIPAddressVersion": "IPv4",
                        "publicIPAllocationMethod": "Dynamic",
                        "resourceGuid": “fffff-4444444-cccccc"
                    },
                    "type": "Microsoft.Network/publicIPAddresses"
                }
            ]
        },
        "changed": false
    }
}

所以,我想我错过了一些东西,有人能指出我正确的方向吗?

4

1 回答 1

2

改变:

- name: Add new instance to host group
  add_host:
    hostname: '{{ item.ipAddress }}'
    groupname: launched
  with_items: azure_ip.azure_publicipaddresses

至:

- name: Add new instance to host group
  add_host:
    hostname: "{{ item.properties.ipAddress }}"
    groupname: launched
  with_items: "{{ azure_ip.ansible_facts.azure_publicipaddresses }}"
  • with_items你需要在几个 Ansible 版本中包含"{{ }}"变量
  • 您引用的键名似乎与调试输出不同
于 2017-02-21T13:46:47.960 回答