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.
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.
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.