# Problem With Simple Template

**URL:** <https://forum.ansible.com/t/problem-with-simple-template/23069>\
**Category:** Ansible Project\
**Created:** [November 22, 2016, 5:42am UTC](https://forum.ansible.com/t/problem-with-simple-template/23069 "2016-11-22T05:42:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [November 22, 2016, 5:42am UTC](https://forum.ansible.com/t/problem-with-simple-template/23069/1 "2016-11-22T05:42:26Z")

</div>

(This might appear twice, sorry).

(Ansible 2.2 - Mac OS 12.1)

I’m having trouble with a template I’m trying to create. So, I cut  
it down to a bare minimum.

Consider the following:

jon.yml

---

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [November 22, 2016, 5:58am UTC](https://forum.ansible.com/t/problem-with-simple-template/23069/2 "2016-11-22T05:58:31Z")

</div>

As a follow up, I realize that I made a couple of mistakes in my  
original posting. Unfortunately, fixing these mistakes didn’t change  
the results.

templates/jon.j2 is now

{% for backend in groups[‘ntpservers\_datacenter’] %}  
server {{ inventory\_hostname }} {{ backend }}  
{% endfor %}

and jon.inv is now

[datacenter]  
[host1.example.com](http://host1.example.com)  
[host2.example.com](http://host2.example.com)

[ntpservers\_datacenter]  
[host1.example.com](http://host1.example.com)

Jon

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [November 22, 2016, 8:14am UTC](https://forum.ansible.com/t/problem-with-simple-template/23069/3 "2016-11-22T08:14:58Z")

</div>

That playbook tells ansible to create the template on the 'all' group  
i.e. everything in the inventory.

That's what it's trying to do.

for your use case, just make 2 roles: ntp\_server and ntp\_client, and  
apply them to groups as  
required.

---

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [November 22, 2016, 2:58pm UTC](https://forum.ansible.com/t/problem-with-simple-template/23069/4 "2016-11-22T14:58:36Z")

</div>

> That playbook tells ansible to create the template on the 'all' group  
> i.e. everything in the inventory.
> 
> That's what it's trying to do.

The moment I read this I realized my mistake. You're absolutely  
right! I had stupidly been thinking that this would happen on  
the control machine. Maximum mea culpa. Changing 'all' to  
'localhost' fixes the problem and lets me test the template.  
This was driving me crazy.

> for your use case, just make 2 roles: ntp\_server and ntp\_client, and  
> apply them to groups as required.

The issue I was trying to address in my posting is how to  
recognize which role to apply to a host. I think my basic  
idea is sound. All I have to do is avoid making more stupid  
mistakes.

Thank you \*very\* much!

Jon Forrest

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [November 22, 2016, 8:20pm UTC](https://forum.ansible.com/t/problem-with-simple-template/23069/5 "2016-11-22T20:20:39Z")

</div>

On the 'how do i apply roles to specific hosts, I'd go for a full  
ntp\_server role and an ntp\_client role.  
Then your site.yml looks like

--------------8\<---------------

- hosts: ntpservers  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- ntp\_server

- hosts: servers  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- ntp\_client  
&nbsp;&nbsp;&nbsp;&nbsp;- presumably\_something\_useful

--------------8\<------------------

if you want to avoid hardcoding a group into the ntp\_client roles  
templates, you can pass in a group  
name using 'parameterised roles' - though TBH that might be overkill.

the folder layout is then something like (for completeness)

.  
├── hosts  
├── roles  
│ ├── ntp\_client  
│ │ ├── tasks  
│ │ │ └── main.yml  
│ │ └── templates  
│ │ └── etc  
│ │ └── ntp.conf.j2  
│ └── ntp\_server  
│ ├── tasks  
│ │ └── main.yml  
│ └── templates  
│ └── etc  
│ └── ntp.conf.j2  
└── site.yml

---

<div class="post-metadata">

**Author:** ![Jon\_Forrest](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jon\_Forrest](https://forum.ansible.com/u/Jon_Forrest)\
**Post date:** [November 22, 2016, 9:27pm UTC](https://forum.ansible.com/t/problem-with-simple-template/23069/6 "2016-11-22T21:27:03Z")

</div>

I appreciate you taking the time to follow up on this.

[...]

I wish our environment were that simple. However, here at the  
day job we have \>3 separate "silos", each of which will be running  
its own time server pool, which the clients in the silo will  
connect to.

So, with the guidance you gave me, I came up with the following  
structure I'm using in a Vagrant-created test environment to  
illustrate my idea:
