complex data example using value and default from list of dicts (#73937)

pull/73956/head
Brian Coca 3 years ago committed by GitHub
parent efe97aeb31
commit ccd9a992cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -122,6 +122,7 @@ In this case, we want to find the mount point for a given path across our machin
msg: "{{(ansible_facts.mounts | selectattr('mount', 'in', path) | list | sort(attribute='mount'))[-1]['mount']}}"
.. _omit_elements_from_list:
Omit elements from a list
-------------------------
@ -158,6 +159,30 @@ Another way is to avoid adding elements to the list in the first place, so you c
- "bar"
.. _combine_optional_values:
Combine values from same list of dicts
---------------------------------------
Combining positive and negative filters from examples above, you can get a 'value when it exists' and a 'fallback' when it doesn't.
.. code-block:: YAML+Jinja
:caption: Use selectattr and rejectattr to get the ansible_host or inventory_hostname as needed
- hosts: localhost
tasks:
- name: Check hosts in inventory that respond to ssh port
wait_for:
host: "{{ item }}"
port: 22
loop: '{{ has_ah + no_ah }}'
vars:
has_ah: '{{ hostvars|dictsort|selectattr("1.ansible_host", "defined")|map(attribute="1.ansible_host")|list }}'
no_ah: '{{ hostvars|dictsort|rejectattr("1.ansible_host", "defined")|map(attribute="0")|list }}'
.. _custom_fileglob_variable:
Custom Fileglob Based on a Variable
-----------------------------------

Loading…
Cancel
Save