Thanks for your answer Vladimir.
Now, there is a 'workstations' attribute all the time.
However, I still cannot get 'os_url'.
The following includes your few lines:
```
- name: Extracting a value of a field deep within a list of complex
dictionaries when some keys match other values
hosts:
- localhost
strategy: debug
tasks:
- include_vars:
file: domain_definition.yml
name: domain_definition_raw
- set_fact:
domain_definition: "{{ domain_definition|default([]) +
[my_item] }}"
loop: "{{ domain_definition_raw.domain_definition }}"
vars:
empty_workstations:
workstations: []
my_item: "{{ (item.workstations is defined)|
ternary(item,
item|combine(empty_workstations)) }}"
- name: (1) Extracting the OS URL when <os_revision> is known
vars:
- os_name: ubuntu1
- os_revision: 2017-08-16
debug:
msg:
"os_url: {{ domain_definition |
selectattr('workstations') |
list |
selectattr('name', 'eq', os_name) |
list |
selectattr('revision', 'eq',
os_revision) |
map(attribute= 'url') |
first }}"
ignore_errors: yes
```
leads to:
"The task includes an option with an undefined variable. The error was: No
first item, sequence was empty..."
If I try to list only the workstations dictionaries with:
```
- name: (0) Listing all workstations
debug:
msg:
"workstations: {{ domain_definition |
selectattr('workstations') |
list }}"
ignore_errors: yes
```
I get:
ok: [localhost] => {
"msg": "workstations: [{'name': 'server11', 'cluster': 'cluster1',
'port': '8080', 'workstations': [{'name': 'debian1', 'revision':
'2016-06-21', 'url': 'http://debian1.example.com:9911/2016-06-21'},
{'name': 'ubuntu1', 'revision': '2017-08-16', 'url':
'http://ubuntu1.example.com:9912/2017-08-16'}]}, {'name': 'server12',
'cluster': 'cluster2', 'port': '8090', 'workstations': [{'name': 'debian2',
'revision': '2018-04-15', 'url':
'http://debian2.example.com:9921/2018-04-15'}, {'name': 'ubuntu2',
'revision': '2020-01-12', 'url':
'http://ubuntu2.example.com:9922/2020-01-12'}]}]"
}
On top of that, even if I were able to extract only the 'workstations'
dictionaries instead of the whole structure, there would also probably be
an issue with the empty one(s) since I would not be able to access their
non-existent nested attributes (name, version and url).
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/27ad2cab-1823-4bf0-98d7-d00194e9c548n%40googlegroups.com.