所以我现在让我的角色工作,结果现在是“var”,我看到了一个输出。首先,我要写的是这个角色:
- name: create a server
os_server:
state: present
flavor: m1.nano
auth:
auth_url: ****
username: ****
password: ****
project_name: admin
domain_id: ****
name: ansibletest
# region_name: RegionOne
image: 4e7ab5c8-4b39-4c77-b68d-cf2ea7e1df1a
key_name: ansible
timeout: 200
auto_ip: yes
nics:
- net-id: a5a73ab9-3ee5-49a6-bea0-f44f9e376ca0
register: result
- debug: var=result
我得到了这样的长输出:
TASK [openstack : debug] *******************************************************
ok: [172.20.22.21] => {
"result": {
"changed": false,
"id": "6f40f396-7ef8-4e0e-9769-2b9cea898269",
.........
"accessIPv4": "172.20.22.58",
.......
{
..........
},
{
............
"addr": "172.20.22.58",
......
}
]
},
........
},
"interface_ip": "172.20.22.58",
.................
.............
]
},
"os-extended-volumes:volumes_attached": [],
"private_v4": "10.0.100.92",
"progress": 0,
"public_v4": "172.20.22.58",
................
{
...........
}
}
我只需要IP地址(在这种情况下它是172.20.22.58
中包含的"accessIPv4"
) - 是否有任何方法可以“过滤”输出以仅检索此值?
答案 0 :(得分:1)
您可以使用Ansible的字典访问方法访问它。您已经过多地修改了问题中的输出,无法真正了解输出的内容,但看起来如下:
"result": {
"changed": false,
"id": "6f40f396-7ef8-4e0e-9769-2b9cea898269",
"output": {
...
"accessIPv4": "172.20.22.58",
...
然后您应该可以使用result.output.accessIPv4
访问该值。