My main question is how do I assign values of variables in var1 to dest1 and var2 to dest2
- hosts: localhost
vars_files:
- vars/var1.yml
tasks:
- name: Ansible Template
template:
src: template.j2
dest: dest1
- hosts: localhost
vars_files:
- vars/var2.yml
tasks:
- name: Ansible Template
template:
src: template.j2
dest: dest2
Other options: include_vars before each one or add to task's vars:
myvar: '{{lookup("file", "vars/varX.yml"}}}'
I intend to use the same playbook. Not different playbooks like you suggest
It is one playbook but two play.
A playbook can have multiple plays in it.
Can you give an example since one variable-file overwrites both plays for me
Brian gave you an example that work perfectly.
So the most productive is that you show us your code so we review it for potentially errors.
#file: nyc.yml
region: nyc
db_server: sql
#file: sg.yml
region: sg
db_server: db2
#file: templates/temp.j2
location = {{ region }}
db_server = {{ db_server }}
ansible-playbook:
- hosts: localhost
vars_files: - vars/nyc.yml
- vars/sg.yml
tasks:
- name: Populating template
template:
src: temp.j2
dest: {{ items }}
with_items: - des/nyc.yml
- des/sg.yml
I want to populate des/nyc.yml from var/nyc.yml and des/sg.yml from var/sg.yml. How do I link var files to a particular file. Coz only the last loaded var file populates both des/nyc.yml and des/sg.yml.
As Brian said you can use to plays.
But this in your playbook file and run it.
- hosts: localhost
vars_files:
- vars/nyc.yml
tasks:
- name: Populating template
template:
src: temp.j2
dest: /what/ever/dest/you/need1
- hosts: localhost
vars_files:
- vars/sg.yml
tasks:
- name: Populating template
template:
src: temp.j2
dest: /what/ever/dest/you/need2