An additional example of creating a list from a dictionary var using Jinja filters in an ansible play task. see article: http://www.blue-bag.com/blog/ansible-filters-taming-lists-part-2
Last active
August 22, 2021 13:09
-
-
Save iAugur/44e7917b042fb6ea8996 to your computer and use it in GitHub Desktop.
Ansible: Example of working with lists of host vars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: servers | |
gather_facts: true | |
sudo: true | |
vars: | |
fail2ban_config_ignoreip: | |
- "127.0.0.1/8" | |
- "{{ ansible_ssh_host }}" | |
tasks: | |
- debug: msg="{{ groups['servers'] }}" | |
- name: get hostvars for servers' ansible_default_ipv4 | |
set_fact: | |
ip_list1: "{{ hostvars|fetchlistfromdict(groups.servers)|map(attribute='ansible_default_ipv4.address')|list }}" | |
- name: Show the list of ansible_default_ipv4 | |
debug: msg="{{ ip_list1 }}" | |
- name: Get the servers' ansible_all_ipv4_addresses | |
set_fact: | |
ip_list2: "{{hostvars|fetchlistfromdict(groups.servers)|map(attribute='ansible_all_ipv4_addresses')|list }}" | |
- name: Show the list we obtained for ansible_all_ipv4_addresses | |
debug: msg="{{ip_list2}}" | |
- name: Flatten the list of ansible_all_ipv4_addresses | |
set_fact: | |
ip_list3: "{{ ip_list2 | flatten_dict_values }}" | |
- debug: msg="{{ ip_list3 }}" | |
- name: Join all of the lists | |
set_fact: | |
ip_list4: "{{ fail2ban_config_ignoreip | union(ip_list1) | union(ip_list3) }}" | |
- name: show the final list | |
debug: msg="{{ ip_list4 }}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This function will take a dictionary composed of sub arrays | |
# and flatten it | |
# e.g. | |
# [[u'46.101.45.8', u'10.131.174.53'], [u'46.101.45.10', u'10.131.174.54']] | |
# to | |
# [u'46.101.45.8', u'10.131.174.53', u'46.101.45.10', u'10.131.174.54'] | |
def flatten_dict_values(dictionary): | |
result = [] | |
result = reduce(list.__add__, dictionary,[]) | |
return result | |
class FilterModule (object): | |
def filters(self): | |
return { | |
"flatten_dict_values": flatten_dict_values | |
} | |
# http://feldboris.alwaysdata.net/blog/python-trick-how-to-flatten-dictionaries-values-composed-of-iterables.html | |
# http://stackoverflow.com/questions/15995/useful-code-which-uses-reduce-in-python |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook ip_lists.yml -K | |
SUDO password: | |
PLAY [servers] **************************************************************** | |
GATHERING FACTS *************************************************************** | |
ok: [web01.example.co.uk] | |
ok: [db01.example.co.uk] | |
TASK: [debug msg="{{ groups['servers'] }}"] *********************************** | |
ok: [web01.example.co.uk] => { | |
"msg": "['web01.example.co.uk', 'db01.example.co.uk']" | |
} | |
ok: [db01.example.co.uk] => { | |
"msg": "['web01.example.co.uk', 'db01.example.co.uk']" | |
} | |
TASK: [get hostvars for servers' ansible_default_ipv4] ************************ | |
ok: [db01.example.co.uk] | |
ok: [web01.example.co.uk] | |
TASK: [Show the list of ansible_default_ipv4] ********************************* | |
ok: [web01.example.co.uk] => { | |
"msg": "[u'1.2.3.4', u'1.2.3.5']" | |
} | |
ok: [db01.example.co.uk] => { | |
"msg": "[u'1.2.3.4', u'1.2.3.5']" | |
} | |
TASK: [Get the servers' ansible_all_ipv4_addresses] *************************** | |
ok: [web01.example.co.uk] | |
ok: [db01.example.co.uk] | |
TASK: [Show the list we obtained for ansible_all_ipv4_addresses] ************** | |
ok: [db01.example.co.uk] => { | |
"msg": "[[u'1.2.3.4', u'10.10.20.1'], [u'1.2.3.5', u'10.10.20.2']]" | |
} | |
ok: [web01.example.co.uk] => { | |
"msg": "[[u'1.2.3.4', u'10.10.20.1'], [u'1.2.3.5', u'10.10.20.2']]" | |
} | |
TASK: [Flatten the list of ansible_all_ipv4_addresses] ************************ | |
ok: [db01.example.co.uk] | |
ok: [web01.example.co.uk] | |
TASK: [debug msg="{{ ip_list3 }}"] ******************************************** | |
ok: [db01.example.co.uk] => { | |
"msg": "[u'1.2.3.4', u'10.10.20.1', u'1.2.3.5', u'10.10.20.2']" | |
} | |
ok: [web01.example.co.uk] => { | |
"msg": "[u'1.2.3.4', u'10.10.20.1', u'1.2.3.5', u'10.10.20.2']" | |
} | |
TASK: [Join all of the lists] ************************************************* | |
ok: [web01.example.co.uk] | |
ok: [db01.example.co.uk] | |
TASK: [show the final list] *************************************************** | |
ok: [web01.example.co.uk] => { | |
"msg": "['127.0.0.1/8', u'1.2.3.4', u'1.2.3.5', u'10.10.20.1', u'10.10.20.2']" | |
} | |
ok: [db01.example.co.uk] => { | |
"msg": "['127.0.0.1/8', u'1.2.3.5', u'1.2.3.4', u'10.10.20.1', u'10.10.20.2']" | |
} | |
PLAY RECAP ******************************************************************** | |
db01.example.co.uk : ok=10 changed=0 unreachable=0 failed=0 | |
web01.example.co.uk : ok=10 changed=0 unreachable=0 failed=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of
fetchlistfromdict
the new method is to use theextract
filter with amap
.This StackOverflow Answer does a great job of answering.
There's also the new
json_query
filter in 2.2, but I'm not sure how to use it for this case.