Created
February 26, 2020 13:44
-
-
Save azrdev/d8cc5aedd1124194796b520a5273c3ee to your computer and use it in GitHub Desktop.
ansible/jinja: transform dict values
This file contains 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
--- | |
- 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