Last active
November 3, 2016 16:07
-
-
Save greenbrian/8101723 to your computer and use it in GitHub Desktop.
Ansible template usage and with_nested
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
#roles/myapp/tasks/main.yml | |
--- | |
- name: manage config files | |
template: src={{ item[0].src }} dest=/etc/{{ item[1] }}/{{ item[0].dest }} | |
with_nested: | |
- config_files | |
- app_instance |
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
#roles/myapp/templates/conf1.j2 | |
app_instance_name: {{ item.1 }} | |
port: {{ app_instance[item.1].port }} | |
option1: {{ app_instance[item.1].option1 }} | |
{% if item.1 == 'instance1' %} | |
do stuff | |
{% endif %} | |
{% if item.1 == 'instance2' %} | |
do other stuff | |
{% endif %} |
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
#roles/myapp/vars/main.yml | |
--- | |
config_files: | |
- { src: 'conf1.j2', dest: 'conf1.conf' } | |
- { src: 'conf2.j2', dest: 'conf2.conf' } | |
app_instance: | |
instance1: | |
port: 888 | |
domain: example.com | |
option1: "off" | |
instance2: | |
port: 889 | |
domain: example.org | |
option1: "on" | |
# or should it be done this way: | |
# - name: instance3 | |
# domain: example.org | |
# option1: "on" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a working role of this type, but not able to open source it. I've posted this here in hopes of getting feedback on my structuring of the variable and template play.