Extract Ansible Fields with selectattr
· 3 phút để đọc
When working with Ansible, processing task results to extract meaningful information is often necessary. This article demonstrates how to filter attributes and extract a field from any Ansible variables / outputs.
Intro
Let's imagine we have a registered Ansible variable called output
, the structure might look like this:
ok: [localhost] => {
"output": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
<< truncated >>
"item": {
"key": "nova-api",
"value": null
},
"rc": 1,
},
{
<< truncated >>
"item": {
"key": "nova-scheduler",
"value": null
},
"rc": 0,
},
{
<< truncated >>
"item": {
"key": "nova-conductor",
"value": null
},
"rc": 0,
}
]
}
}