Skip to content

Instantly share code, notes, and snippets.

@greenbrian
Last active November 3, 2016 16:07
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 greenbrian/8101723 to your computer and use it in GitHub Desktop.
Save greenbrian/8101723 to your computer and use it in GitHub Desktop.
Ansible template usage and with_nested
#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
#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 %}
#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"
@greenbrian
Copy link
Author

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.

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