Skip to content

Instantly share code, notes, and snippets.

@iAugur
Last active August 22, 2021 13:09
Show Gist options
  • Save iAugur/44e7917b042fb6ea8996 to your computer and use it in GitHub Desktop.
Save iAugur/44e7917b042fb6ea8996 to your computer and use it in GitHub Desktop.
Ansible: Example of working with lists of host vars
---
- 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 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
$ 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
@Freyert
Copy link

Freyert commented Jul 9, 2017

Instead of fetchlistfromdict the new method is to use the extract filter with a map.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment