Skip to content

Instantly share code, notes, and snippets.

@azrdev
Created February 26, 2020 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azrdev/d8cc5aedd1124194796b520a5273c3ee to your computer and use it in GitHub Desktop.
Save azrdev/d8cc5aedd1124194796b520a5273c3ee to your computer and use it in GitHub Desktop.
ansible/jinja: transform dict values
---
- name: "define fact user_homes as dict(user name => user home directory)"
when: user_homes is undefined
block:
- name: getent
getent:
database: passwd
split: ":"
- name: set fact
set_fact:
# python would do: `{key: values[4] for key, values in getent_passwd}`
#
# instead in jinja:
# build a dict by passing all usernames to…
# … zip which concatenates them with the result of passing all usernames through…
# … map which extracts the homedir as getent_passwd[current username][4]
user_homes: >
{{ dict(getent_passwd.keys()
| zip(getent_passwd.keys()
| map('extract', getent_passwd, 4)
)
)
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment