The goal is to extract as efficiently as possible (the original file can be quite large) a workstation <url> matching:
A major constraint is to avoid "json_query" as it cannot deal properly with keys containing a colon (as it may happen in my real context), last time I checked
- 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:
- name: Including domain_definition
include_vars: "domain_definition.yml"
- name: 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
- name: Extracting the OS URL when <os_revision> is known
vars:
- os_name: ubuntu1
- os_revision: 2017-08-16
debug:
msg:
"os_url: {{ domain_definition |
list |
selectattr('workstations.revision', 'eq', os_revision) |
map(attribute= 'workstations.url') |
first }}"
ignore_errors: yes
- name: Extracting the OS URL when <os_revision> is unknown
vars:
- os_name: debian2
debug:
msg:
"os_url: {{ domain_definition |
selectattr('workstations') |
list |
selectattr('name', 'eq', os_name) |
map(attribute= 'url') |
first }}"
ignore_errors: yes
- name: Extracting the OS URL when <os_revision> is unknown
vars:
- os_name: debian2
debug:
msg:
"os_url: {{ domain_definition |
map(attribute= 'workstations.url') |
first }}"
ignore_errors: yes
I always get the error msg: 'dict object' has no attribute 'workstations'.