Ansible unicode对象没有属性值json解析
Ansible unicode object has no attribute value json parsing
我的任务是
- name: task name
shell: openstack floating ip create provider --format json
register: result
输出将采用以下 json 格式
{
"router_id": null,
"status": "DOWN",
"description": "",
"created_at": "2017-05-24T10:49:15Z",
"updated_at": "2017-05-24T10:49:15Z",
"floating_network_id": "923-cc77237b08e7",
"headers": "",
"fixed_ip_address": null,
"floating_ip_address": "192.*.*.*",
"revision_number": 1,
"project_id": "2709ad381fcf41c5bce673c916fded10",
"port_id": null,
"id": "c5d187elg-d269-4eae-b6ae-7d258f04983"
}
我想做的是,只获取 floating_ip_address 并将其存储到一个变量中,以便我可以在另一个任务中使用它。
我使用以下代码来执行此操作,
- set_fact:
address: "{{ (result.stdout | from_json | selectattr('floating_ip_address') | list | first).floating_ip_address }}"
但是我得到一个错误
"ERROR! 'unicode object' has no attribute 'floating_ip_address'"
仅获取 ip 地址的正确格式是什么?
如果floating_ip_address
不是列表,而是您输入的简单键,例如:
- set_fact:
address: "{{ (result.stdout | from_json)['floating_ip_address'] }}"
我的任务是
- name: task name
shell: openstack floating ip create provider --format json
register: result
输出将采用以下 json 格式
{
"router_id": null,
"status": "DOWN",
"description": "",
"created_at": "2017-05-24T10:49:15Z",
"updated_at": "2017-05-24T10:49:15Z",
"floating_network_id": "923-cc77237b08e7",
"headers": "",
"fixed_ip_address": null,
"floating_ip_address": "192.*.*.*",
"revision_number": 1,
"project_id": "2709ad381fcf41c5bce673c916fded10",
"port_id": null,
"id": "c5d187elg-d269-4eae-b6ae-7d258f04983"
}
我想做的是,只获取 floating_ip_address 并将其存储到一个变量中,以便我可以在另一个任务中使用它。
我使用以下代码来执行此操作,
- set_fact:
address: "{{ (result.stdout | from_json | selectattr('floating_ip_address') | list | first).floating_ip_address }}"
但是我得到一个错误
"ERROR! 'unicode object' has no attribute 'floating_ip_address'"
仅获取 ip 地址的正确格式是什么?
如果floating_ip_address
不是列表,而是您输入的简单键,例如:
- set_fact:
address: "{{ (result.stdout | from_json)['floating_ip_address'] }}"