Skip to content

Instantly share code, notes, and snippets.

@smarthall
Last active February 16, 2018 08:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smarthall/1e302b40f38ddd4e218d to your computer and use it in GitHub Desktop.
Save smarthall/1e302b40f38ddd4e218d to your computer and use it in GitHub Desktop.
Getting lists from dictionaries in Ansible
# In Ansible lots of things take lists (or comma seperated
# strings), however lots of things return dicts. One
# example of this is the hostvars and groups variable.
#
# groups returns a list of machines in a group, and
# hostvars is a dict containing all the hosts. So if you
# need a list of ip addresses of those hosts for the
# route53 module you cant. This filter makes this possible.
def fetchlistfromdict(d, l):
result = []
for item in l:
result.append(d[item])
return result
class FilterModule(object):
def filters(self):
return {
'fetchlistfromdict': fetchlistfromdict,
}
@jwerak
Copy link

jwerak commented Nov 28, 2014

This is really useful filter.
Did you try to push it to ansible core or maybe is it there already? I can't find it...

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