Problem With Simple Template

(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

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
host2.example.com

[ntpservers_datacenter]
host1.example.com

Jon

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.

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

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
  roles:
    - ntp_server

- hosts: servers
  roles:
    - ntp_client
    - 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

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: