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

This commit is contained in:
Brian Coca 2021-03-18 15:31:33 -04:00 committed by GitHub
parent efe97aeb31
commit ccd9a992cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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']}}" msg: "{{(ansible_facts.mounts | selectattr('mount', 'in', path) | list | sort(attribute='mount'))[-1]['mount']}}"
.. _omit_elements_from_list:
Omit elements from a 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" - "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 Custom Fileglob Based on a Variable
----------------------------------- -----------------------------------