I'm trying to get the value of an attribute in a json array with Ansible. Example data:
"domains.json.data": [
{
"axfr_ips": [],
"description": "",
"domain": "mydomain.net",
"expire_sec": 0,
"group": "",
"id": 687088,
},
{
"axfr_ips": [],
"description": "",
"domain": "myotherdomain.net",
"expire_sec": 0,
"group": "",
"id": 687089,
}
]
}
So I tried with json query:
"{{ domains.json.data | json_query(\"data[?domain=='{{ server_domain }}'].id\") }}"
or with:
- set_fact:
domain_id: "{{ domains | json_query(query) | first }}"
vars:
query: "domains.json[?name=='{{ server_domain }}'].id"
Also tried with selectattrib:
"{{ linode_domains.json.data | selectattr(\"domain\", \"{{ server_domain }}\") | list }}"
So what I need is to get the id of the domain I got in {{ server_domain }}.
The query can't work because there is no attribute name
in the dictionary
query: "domains.json[?name=='{{ server_domain }}'].id"
Use the attribute domain
. For example the task below
- debug:
msg: "{{ domains.json.data|json_query(query) }}"
vars:
server_domain: 'mydomain.net'
query: "[?domain=='{{ server_domain }}'].id"
gives
"msg": [
687088
]
Notes
msg: "{{ domains.json.data|json_query(query)|first }}"
You can use selectattr
to filter and use map
to get the id value. Below is the sample you can try.
- name: Retrieve value from JSON array
debug:
msg: "{{ test.domains | selectattr('domain', 'equalto', server_domain) | map(attribute='id') | join(',') }}"
As map returns list, I have used join
to convert to string.
I have set server_domain value to myotherdomain.net for testing.
Below is the output of the command
TASK [Retrieve value from JSON array] **************************
ok: [localhost] => {
"msg": "687089"
}
EDIT: equalto was introduced in v2.8 of jinja2. If you get TemplateRuntimeError: no test named 'equalto', update the jinja2.
To check version use pip list | grep Jinja2
.
To update use following command pip install --upgrade Jinja2
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With