From ccd9a992cf7b7b057cfd9a2d06c69c44b7094696 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 18 Mar 2021 15:31:33 -0400 Subject: [PATCH] complex data example using value and default from list of dicts (#73937) --- .../user_guide/complex_data_manipulation.rst | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/docsite/rst/user_guide/complex_data_manipulation.rst b/docs/docsite/rst/user_guide/complex_data_manipulation.rst index 9f10984a250..3f6da7b8051 100644 --- a/docs/docsite/rst/user_guide/complex_data_manipulation.rst +++ b/docs/docsite/rst/user_guide/complex_data_manipulation.rst @@ -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 -----------------------------------