ansible hcloud set_事实添加目录列表中的项目
我正在ansible hcloud模块上测试自己。 我需要用我创建的服务器的IP设置一个事实列表 第一台服务器很简单,因为它总是只有一台: 创建空列表:ansible hcloud set_事实添加目录列表中的项目,ansible,yaml,Ansible,Yaml,我正在ansible hcloud模块上测试自己。 我需要用我创建的服务器的IP设置一个事实列表 第一台服务器很简单,因为它总是只有一台: 创建空列表: - set_fact: ips: [] 然后创建服务器并将服务器IP添加到列表中: - set_fact: ips: - "{{ ips + [ a3srv_fact.hcloud_server.ipv4_address ] }}" 下一个块带有循环,因此寄存器给出: "
- 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"
}
]
}
结果块可以有一个或多个部分,有两个
我需要将每个ipv4\u地址添加到现有事实列表中
我很确定yml语法有一个简单的方法,但我就是不明白。使用json\u查询。下面的任务
- set_fact:
ips: "{{ ips|default([]) +
hc_fact.results|
json_query('[*].hcloud_server.ipv4_address')
}}"
给予
"ips": [
"116.203.204.49",
"116.203.204.136"
]