我的任务是
- 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地址的格式是什么?
答案 0 :(得分:1)
如果floating_ip_address
不是列表,而是输入中的简单键,例如:
- set_fact:
address: "{{ (result.stdout | from_json)['floating_ip_address'] }}"