Skip to content

Instantly share code, notes, and snippets.

@kcem
Last active March 17, 2022 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcem/5ec90b97c08fafd0397f248e8c31b25b to your computer and use it in GitHub Desktop.
Save kcem/5ec90b97c08fafd0397f248e8c31b25b to your computer and use it in GitHub Desktop.
Create, modify, refill dictionary in Ansible based on other variables. Way of Ansible rename dictionary keys.
---
- hosts: all
gather_facts: false
vars:
data_source:
- {src_host: 172.16.0.10, src_user: www-data, dst_host: 172.16.0.11, dst_user: mysql}
- {src_host: 172.16.0.10, src_user: ftp, dst_host: 172.16.0.11, dst_user: mysql}
- {src_host: 172.16.0.10, src_user: ftp, dst_host: 172.16.0.12, dst_user: nat}
- {src_host: 172.16.0.10, src_user: www-data, dst_host: 172.16.0.14, dst_user: mysql}
list_of_users:
"{{ data_source|map(attribute='src_user')|union(data_source|map(attribute='dst_user'))|unique }}"
list_of_users_pairs_yml: |-
{% for item in data_source %}
- src: {{ item.src_user }}
dst: {{ item.dst_user }}
opt: {{ item.optional_value|d('') }}
{% endfor %}
list_of_users_pairs: "{{ list_of_users_pairs_yml|from_yaml|unique }}"
tasks:
- debug: var=list_of_users
- debug: var=list_of_users_pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment