Hello Community
,
the last few days I’ve been working with HTTP requests via ansible.builtin.uri. So far this works as expected. The return value of this API call is a complex JSON structure. It looks something like this:
{
"link": [{
"attributes": [{
"value": "4711",
"name": "id"
}, {
"value": "bambi",
"name": "name"
}, {
"name": "description"
}, {
"value": "de.stschnell/bambi",
"name": "fqn"
}, {
"value": "0.0.0",
"name": "version"
}
],
"href": "http://localhost/4711/",
"rel": "yogi"
}, ...
],
...
"total": 1234
}
Here the ID of an attribute must be detected via the fqn. This means the input is de.stschnell/bambi and the output is 4711. I realized this with a two-step approach. In the first step I determine whether the FQN is identical and, if so, the ID is determined in the second step. That also works as expected.
- ansible.builtin.set_fact:
attribute: "{{ item }}"
loop: "{{ dict_actions.link | subelements('attributes') }}"
when:
- item.1.name == 'fqn'
- item.1.value == 'de.stschnell/bambi'
- ansible.builtin.set_fact:
action_id: "{{ item.value }}"
loop: "{{ attribute[0].attributes }}"
when:
- item.name == 'id'
Using the search function in this forum, I saw that several questions have already been asked about processing complex JSON structures. I don’t have any questions about this particular case at the moment. But I spent quite some time creating the playbook. Handling complex JSON structures with Ansible seems complicated to me. Perhaps I simply lack experience, but I would like to ask a few questions about this:
- Are there any established standard procedures for handling complex JSON structures in Ansible playbooks?
- Are there any available resources that describe the handling of complex JSON structures in Ansible playbooks with examples?
- I found this great example from Todd [@utoddl] in which two approaches are demonstrated
. Jinja2 is used in one implementation. If I understand correctly, Jinja2 seems to offer a very powerful approach to this requirement. Are there some good resources available for learning how to use Jinja2 in Ansible playbooks?
My Ansible journey has begun. ![]()
Thanks for hints and tips.
Best regards
Stefan