如何从 Ansible azure_rm_publicipaddress_facts 模块获取 public IP 到变量中以与 add_host 一起使用?

How to get public IP from Ansible azure_rm_publicipaddress_facts module into a variable to use with add_host?

我正在尝试使用 Ansible 为 Azure 自动化设置管道,一切正常,除非我尝试将 public 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

这给了我以下错误:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined.

但是如图所示,该值存在:

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

所以,我想我遗漏了什么,有人能给我指出正确的方向吗?

变化:

- 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 版本
  • 您引用的键名似乎与调试输出不同