@ -60,7 +60,8 @@ There are several ways to do it in Ansible, this is just one example:
tasks:
tasks:
- name: Show extracted list of keys from a list of dictionaries
- name: Show extracted list of keys from a list of dictionaries
debug: msg="{{ chains | map('extract', chains_config) | map(attribute='configs') | flatten | map(attribute='type') | flatten }}"
ansible.builtin.debug:
msg: "{{ chains | map('extract', chains_config) | map(attribute='configs') | flatten | map(attribute='type') | flatten }}"
vars:
vars:
chains: [1, 2]
chains: [1, 2]
chains_config:
chains_config:
@ -110,7 +111,8 @@ In this case, we want to find the mount point for a given path across our machin
path: /var/lib/cache
path: /var/lib/cache
tasks:
tasks:
- name: The mount point for {{path}}, found using the Ansible mount facts, [-1] is the same as the 'last' filter
- name: The mount point for {{path}}, found using the Ansible mount facts, [-1] is the same as the 'last' filter
debug: msg="{{(ansible_facts.mounts | selectattr('mount', 'in', path) | list | sort(attribute='mount'))[-1]['mount']}}"
ansible.builtin.debug:
msg: "{{(ansible_facts.mounts | selectattr('mount', 'in', path) | list | sort(attribute='mount'))[-1]['mount']}}"
@ -123,8 +125,8 @@ The special ``omit`` variable ONLY works with module options, but we can still u
:caption: Inline list filtering when feeding a module option
:caption: Inline list filtering when feeding a module option
:emphasize-lines: 3, 7
:emphasize-lines: 3, 7
- name: e nable a list of Windows features, by name
- name: E nable a list of Windows features, by name
set_fact:
ansible.builtin. set_fact:
win_feature_list: "{{ namestuff | reject('equalto', omit) | list }}"
win_feature_list: "{{ namestuff | reject('equalto', omit) | list }}"
vars:
vars:
namestuff:
namestuff:
@ -139,8 +141,8 @@ Another way is to avoid adding elements to the list in the first place, so you c
:caption: Using set_fact in a loop to increment a list conditionally
:caption: Using set_fact in a loop to increment a list conditionally
:emphasize-lines: 3, 4, 6
:emphasize-lines: 3, 4, 6
- name: b uild unique list with some items conditionally omitted
- name: B uild unique list with some items conditionally omitted
set_fact:
ansible.builtin. set_fact:
namestuff: ' {{ (namestuff | default([])) | union([item]) }}'
namestuff: ' {{ (namestuff | default([])) | union([item]) }}'
when: item != omit
when: item != omit
loop:
loop:
@ -194,7 +196,7 @@ A bit more complex, using ``set_fact`` and a ``loop`` to create/update a diction
:emphasize-lines: 3, 4
:emphasize-lines: 3, 4
- name: Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists
- name: Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists
set_fact:
ansible.builtin. set_fact:
mydict: "{{ mydict | default({}) | combine({item[0]: item[1]}) }}"
mydict: "{{ mydict | default({}) | combine({item[0]: item[1]}) }}"
loop: "{{ (keys | zip(values)) | list }}"
loop: "{{ (keys | zip(values)) | list }}"
vars:
vars:
@ -242,7 +244,8 @@ https://www.reddit.com/r/ansible/comments/gj5a93/trying_to_get_uptime_from_secon
.. code-block :: YAML+Jinja
.. code-block :: YAML+Jinja
- debug:
- name: Show the uptime in a certain format
ansible.builtin.debug:
msg: Timedelta {{ now() - now().fromtimestamp(now(fmt='%s') | int - ansible_uptime_seconds) }}
msg: Timedelta {{ now() - now().fromtimestamp(now(fmt='%s') | int - ansible_uptime_seconds) }}