ansible hcloud set_fact从字典列表中添加项目

时间:2019-08-07 12:22:12

标签: ansible yaml

我正在尝试使用ansible hcloud模块。 我需要使用创建的服务器的IP设置事实列表。

第一个服务器很容易,因为它总是一个:

创建空列表:

    - set_fact:
        ips: []

然后创建服务器,并将服务器IP添加到列表中:

    - set_fact:
        ips:
          - "{{ ips + [ a3srv_fact.hcloud_server.ipv4_address ] }}"

下一个块带有循环,因此寄存器给了我这个

"hc_fact": {
    "changed": false,
    "msg": "All items completed",
    "results": [
        {
            "ansible_loop_var": "item",
            "changed": false,
            "failed": false,
            "hcloud_server": {
                "backup_window": "None",
                "datacenter": "nbg1-dc3",
                "id": "3083849",
                "image": "centos-7",
                "ipv4_address": "116.203.204.49",
                "ipv6": "2a01:4f8:c2c:ade4::/64",
                "labels": {},
                "location": "nbg1",
                "name": "a3hc1",
                "rescue_enabled": false,
                "server_type": "cx11",
                "status": "running"
            },
            "invocation": {
                "module_args": {
                    "api_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                    "backups": false,
                    "datacenter": null,
                    "endpoint": "https://api.hetzner.cloud/v1",
                    "force_upgrade": false,
                    "id": null,
                    "image": "centos-7",
                    "labels": null,
                    "location": null,
                    "name": "a3hc1",
                    "server_type": "cx11",
                    "ssh_keys": null,
                    "state": "present",
                    "upgrade_disk": false,
                    "user_data": null,
                    "volumes": null
                }
            },
            "item": "hc1"
        },
        {
            "ansible_loop_var": "item",
            "changed": false,
            "failed": false,
            "hcloud_server": {
                "backup_window": "None",
                "datacenter": "nbg1-dc3",
                "id": "3083921",
                "image": "centos-7",
                "ipv4_address": "116.203.204.136",
                "ipv6": "2a01:4f8:c2c:ae25::/64",
                "labels": {},
                "location": "nbg1",
                "name": "a3hc2",
                "rescue_enabled": false,
                "server_type": "cx11",
                "status": "running"
            },
            "invocation": {
                "module_args": {
                    "api_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                    "backups": false,
                    "datacenter": null,
                    "endpoint": "https://api.hetzner.cloud/v1",
                    "force_upgrade": false,
                    "id": null,
                    "image": "centos-7",
                    "labels": null,
                    "location": null,
                    "name": "a3hc2",
                    "server_type": "cx11",
                    "ssh_keys": null,
                    "state": "present",
                    "upgrade_disk": false,
                    "user_data": null,
                    "volumes": null
                }
            },
            "item": "hc2"
        }
    ]
}

结果块可以包含1个或多个部分,有2个部分。

我需要将每个ipv4_address添加到现有的事实列表中。

我很确定yml语法有一种直接的方法,但是我无法理解。

1 个答案:

答案 0 :(得分:1)

使用json_query。下面的任务

- set_fact:
    ips: "{{ ips|default([]) +
             hc_fact.results|
             json_query('[*].hcloud_server.ipv4_address')
             }}"

给予

"ips": [
    "116.203.204.49", 
    "116.203.204.136"
]