# different template files on different devices

**URL:** <https://forum.ansible.com/t/different-template-files-on-different-devices/28190>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [July 30, 2018, 11:44am UTC](https://forum.ansible.com/t/different-template-files-on-different-devices/28190 "2018-07-30T11:44:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![churchill\_davidson](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@churchill\_davidson](https://forum.ansible.com/u/churchill_davidson)\
**Post date:** [July 30, 2018, 11:44am UTC](https://forum.ansible.com/t/different-template-files-on-different-devices/28190/1 "2018-07-30T11:44:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 30, 2018, 3:45pm UTC](https://forum.ansible.com/t/different-template-files-on-different-devices/28190/2 "2018-07-30T15:45:01Z")

</div>

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  
&nbsp;&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars\_1: 'aaaa'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars\_2: 'bbbb'

&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Generating Shell Script for Configuring device  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: ~/templates/config.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: ~/config.sh  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0777

---

<div class="post-metadata">

**Author:** ![churchill\_davidson](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@churchill\_davidson](https://forum.ansible.com/u/churchill_davidson)\
**Post date:** [July 31, 2018, 7:06am UTC](https://forum.ansible.com/t/different-template-files-on-different-devices/28190/3 "2018-07-31T07:06:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 31, 2018, 8:54am UTC](https://forum.ansible.com/t/different-template-files-on-different-devices/28190/4 "2018-07-31T08:54:19Z")

</div>

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](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](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](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#splitting-out-host-and-group-specific-data)

---

<div class="post-metadata">

**Author:** ![churchill\_davidson](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@churchill\_davidson](https://forum.ansible.com/u/churchill_davidson)\
**Post date:** [August 1, 2018, 8:21am UTC](https://forum.ansible.com/t/different-template-files-on-different-devices/28190/5 "2018-08-01T08:21:21Z")

</div>

Thanks Kai for the details.

Regards,  
Churchill
