【问题标题】:ansible hcloud set_fact add items from list in dictansible hcloud set_fact 从字典中的列表中添加项目
【发布时间】:2019-08-07 12:22:12
【问题描述】:

我正在尝试使用 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 个或多个部分,有两个。

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

我很确定 yml 语法有一个简单的方法,但我就是不明白。

【问题讨论】:

    标签: ansible yaml


    【解决方案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"
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2016-06-06
      • 1970-01-01
      • 2023-03-10
      • 2021-07-20
      相关资源
      最近更新 更多