I have couple of templates which has different configuration for switches.The inventory files has different groups(example given below)
cat ~/inventory/hosts
[cumulus_1]
1.1.1.1
2.2.2.2
[cumulus_2]
3.3.3.3
4.4.4.4
I want to have a single playbook which can execute a separate template for cumulus_1 and another template for cumulus_2.
With the playbook which i have now i can use only one template file as i have mentioned the host group in the playbook.
- hosts: cumulus_1
gather_facts: false
vars:
vars_1: ‘xxxx’
vars_2: ‘yyyy’
tasks:
- name: Generating Shell Script for Configuring device
template:
src: ~/templates/config.j2
dest: ~/config.sh
mode: 0777
Any suggestions on how to achieve this?
Regards,
Churchill
You have several option, but the easiest is to add a play.
Your code above is called a play, a playbook can contain several plays, so you can add this play to your playbook
- hosts: cumulus_2
gather_facts: false
vars:
vars_1: 'aaaa'
vars_2: 'bbbb'
tasks:
- name: Generating Shell Script for Configuring device
template:
src: ~/templates/config.j2
dest: ~/config.sh
mode: 0777
Thanks Kai.This really helped me and i was able to accomplish what i wanted.
Meanwhile for enlightenment and also for the benefit of the group members can you also please share what are the other ways to achieve this as it might be useful in some other circumstances?
Regards,
Churchill
Every Ansible book out there will explain how to do this in more detail.
But you could set group variables[1] and/or host variables[2] in the inventory or use group_vars[3] and/or host_vars[3].
Then you only need one template because each host and/or group will have different values in the variable and the output of the template will differ accordingly.
[1] https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#group-variables
[2] https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#host-variables
[3] https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#splitting-out-host-and-group-specific-data
Thanks Kai for the details.
Regards,
Churchill