# ntp.j2 for different set of hosts

**URL:** <https://forum.ansible.com/t/ntp-j2-for-different-set-of-hosts/29462>\
**Category:** Ansible Project\
**Created:** [January 18, 2019, 6:36am UTC](https://forum.ansible.com/t/ntp-j2-for-different-set-of-hosts/29462 "2019-01-18T06:36:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [January 18, 2019, 6:36am UTC](https://forum.ansible.com/t/ntp-j2-for-different-set-of-hosts/29462/1 "2019-01-18T06:36:41Z")

</div>

Hi,

I have created a role for the ntp with the ntp.j2 and ntp1.j2 templates in templates diretory .

with ntp.j2 consisting of

server 2.2.2.1 iburst

and with tp.  
ntp1.j2 consist of

server 1.2.1.3 iburst

In the task under the

- name:

template:

src: ntp.conf.j2  
dest: /etc/ntp.conf  
notify: restart ntp

I want to use the template for different hosts if the hosts are say test1 testops1 db-client1 ans 10 others etc .apply ntp.j2 and if the hosts are dev1 devops1 db-devclien1 app-devclient2 and others … how do we use the when clause to achieve this . this hosts are randomly mentioned in the inventory in different groups.

Thank you .

---

<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:** [January 18, 2019, 4:03pm UTC](https://forum.ansible.com/t/ntp-j2-for-different-set-of-hosts/29462/2 "2019-01-18T16:03:25Z")

</div>

You could add a group and put them like ntp\_grp and ntp1\_grp

Then the when will be  
&nbsp;&nbsp;when: inventory\_hostname in group['ntp\_grp']

If you don't want to have groups then you need to list every host like so  
&nbsp;&nbsp;when: inventory\_hostname in ['test1','testops1','db-client1']

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [January 18, 2019, 6:02pm UTC](https://forum.ansible.com/t/ntp-j2-for-different-set-of-hosts/29462/3 "2019-01-18T18:02:39Z")

</div>

Why not have a variable ntp\_template and just set the group\_var for each group to be the template required.

Have two template files ntp1.j2 and ntp2.j2. (This a poor naming scheme, you should name it related to the group)

[group1]  
testops1 db-client1

[group2]  
testops1 other-client

testops2 other-client

testops3 other-client

testops4 other-client

[group1:vars]  
ntp\_template: ntp1.j2

[group2:vars]  
ntp\_template: ntp2.j2

The play would look like this

- name:  
template:  
src: “{{ ntp\_template }}”  
dest: /etc/ntp.conf  
notify: restart ntp

If you just want to change the ip of the server, you can just use a variable in the template

server {{ ntp\_server }} iburst

I find it better to limit conditionals(when) and better to rely on different groups\_vars or different inventories(/group\_vars). It make it much more readable and enables your code to be more flexible, re-usable.

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [January 21, 2019, 6:50am UTC](https://forum.ansible.com/t/ntp-j2-for-different-set-of-hosts/29462/4 "2019-01-21T06:50:30Z")

</div>

Thanks Kai and Steve for your different approaches to the issue.

So Steve, you have mentioned in the last line _"If you just want to change the ip of the server, you can just use a variable in the template_

_server {{ ntp\_server }} iburst"-- \>_ you mean that to have a single template file and in the group\_vars (say /etc/ansible/group\_vars/group1.yml ) have a variable  
as ntp\_server : 2.2.2.1 and for (/etc/ansible/group\_vars/group2.yml) have a variable as ntp\_server: 1.2.1.3 . and have a single template file which has the variable as  
server {{ ntp\_server}} iburst etc and it take it from there… is that what you mean.
