ntp.j2 for different set of hosts

19 views
Skip to first unread message

coach rhca

unread,
Jan 18, 2019, 1:36:41 AM1/18/19
to Ansible Project
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 .



Kai Stian Olstad

unread,
Jan 18, 2019, 11:03:36 AM1/18/19
to ansible...@googlegroups.com
You could add a group and put them like ntp_grp and ntp1_grp

Then the when will be
when: inventory_hostname in group['ntp_grp']

If you don't want to have groups then you need to list every host like so
when: inventory_hostname in ['test1','testops1','db-client1']



--
Kai Stian Olstad


Stephen Granger

unread,
Jan 18, 2019, 1:03:01 PM1/18/19
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/53386831.S9lhg9mtCV%40x1.
For more options, visit https://groups.google.com/d/optout.


--
Steve

coach rhca

unread,
Jan 21, 2019, 1:50:51 AM1/21/19
to ansible...@googlegroups.com
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.

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/7xTvuEagsGo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages