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

4,420 views
Skip to first unread message

John Wang

unread,
Jul 14, 2014, 4:30:48 AM7/14/14
to ansible...@googlegroups.com
I'm new to Ansible and want use it to configure a NTP service. here is my situation:

my hosts file:
    
    [ntp_servers]

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", "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

 

Michael Peters

unread,
Jul 14, 2014, 9:28:47 AM7/14/14
to ansible...@googlegroups.com
Try something like this:

{% for svr in groups.ntp_servers %}
{% if not svr == inventory_hostname %}
peer svr
{% endif %}
{% endfor %}
> --
> 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/684a949d-05c9-4be7-8a66-0558f84a6979%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Jul 14, 2014, 9:31:40 AM7/14/14
to ansible...@googlegroups.com
also:

{% for svr in groups.ntp_servers|difference([inventory_hostname]) %}
  peer svr
{% endfor %}




--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

John Wang

unread,
Jul 14, 2014, 10:05:56 PM7/14/14
to ansible...@googlegroups.com
I know it should be simple, but never imagine it is soooo simple. Thanks guys.
Reply all
Reply to author
Forward
0 new messages