# How to use hosts listed in a group in a template, excluding the current host.

**URL:** <https://forum.ansible.com/t/how-to-use-hosts-listed-in-a-group-in-a-template-excluding-the-current-host/15662>\
**Category:** Ansible Project\
**Created:** [July 14, 2014, 8:30am UTC](https://forum.ansible.com/t/how-to-use-hosts-listed-in-a-group-in-a-template-excluding-the-current-host/15662 "2014-07-14T08:30:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@John\_Wang](https://forum.ansible.com/u/John_Wang)\
**Post date:** [July 14, 2014, 8:30am UTC](https://forum.ansible.com/t/how-to-use-hosts-listed-in-a-group-in-a-template-excluding-the-current-host/15662/1 "2014-07-14T08:30:47Z")

</div>

I’m new to Ansible and want use it to configure a NTP service. here is my situation:

my hosts file:

[ntp\_servers]  
[s1.aaa.com](http://s1.aaa.com)  
[s2.aaa.com](http://s2.aaa.com)  
[s3.aaa.com](http://s3.aaa.com)  
[s4.aaa.com](http://s4.aaa.com)

The “/taskts/mail.yaml” for role “ntp\_server”:

- name: install ntp  
apt: name=ntp state=present

- name: config ntpd  
template: src=ntp.conf dest=/etc/ntp.conf

The “templates/ntp.conf” for role “ntp\_server”:

…  
restrict {{ ntp\_net }} mask {{ ntp\_netmask }} nomodify notrap  
{% for svr in servers %}  
peer svr  
{% endfor %}  
…

My questions are about for loop in the template file (please leave along the NTP specific things):

1. How to refer the hosts defined in the hosts file from within the template file? In another words, I need the value of “servers” variable in the for loop to be a list of “[s1.aaa.com](http://s1.aaa.com)”, “[s2.aaa.com](http://s2.aaa.com)” …

2. How to know the current host so that the for loop could exclude it(it should not be peer to itself) ?

Thanks and Regards  
John

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [July 14, 2014, 1:28pm UTC](https://forum.ansible.com/t/how-to-use-hosts-listed-in-a-group-in-a-template-excluding-the-current-host/15662/2 "2014-07-14T13:28:44Z")

</div>

Try something like this:

{% for svr in groups.ntp\_servers %}  
&nbsp;&nbsp;{% if not svr == inventory\_hostname %}  
&nbsp;&nbsp;peer svr  
&nbsp;&nbsp;{% endif %}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 14, 2014, 1:31pm UTC](https://forum.ansible.com/t/how-to-use-hosts-listed-in-a-group-in-a-template-excluding-the-current-host/15662/3 "2014-07-14T13:31:35Z")

</div>

also:

{% for svr in groups.ntp\_servers|difference([inventory\_hostname]) %}  
&nbsp;&nbsp;peer svr  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![John\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@John\_Wang](https://forum.ansible.com/u/John_Wang)\
**Post date:** [July 15, 2014, 2:05am UTC](https://forum.ansible.com/t/how-to-use-hosts-listed-in-a-group-in-a-template-excluding-the-current-host/15662/4 "2014-07-15T02:05:55Z")

</div>

I know it should be simple, but never imagine it is soooo simple. Thanks guys.
