1

I would like to have a role that can deploy both ntp clients (hosts that point to internal ntp servers for time), and ntp server (hosts that point out to the internet for time).

There is a nice link for how to do this with multiple roles, but since the difference between the two is so small (server vs. client), I’m wondering if there is a way to simplify and use one role with some intelligence to configure the hosts as needed.

In my hosts inventory, I have a group [ntp] which are the servers, and everything else are the clients.

[web]
10.1.1.1
10.1.1.2

[app]
10.1.2.1
10.1.2.2

[db]
10.1.3.1
10.1.3.2

[ntp]
10.1.4.1
10.1.4.2

The role/task to deploy the server or the client is basically the same thing, except for the template that is copied over to /etc/ntp.conf . For all hosts, copy over the ntp-client.j2 template, unless the host is part of the [ntp] group, and then copy over the ntp-server.j2 template.

Thought #1: Set tags in a playbook?

Can the initial playbook set a tag variable for the role? I don't mean filter based on the tag, but set it for the role it will call. Something like:

- hosts:all,!ntp
 tags: client (set the server tag, don’t filter against the argument)
 roles: 
 - ntp

- hosts:ntp
 tags: server (set the server tag, don’t filter against the argument)
 roles: 
 - ntp

And then in the role /ntp/template/main.yml file define

- name: stuff all host do
 module: modulename attribute=value
 tags:
 - client
 - server

- name: stuff just for ntp server
 module: modulename attribute=value
 tags:
 - server

- name: stuff for all hosts (other then ntp servers)
 module: modulename attribute=value
 tags:
 - client

Thought #2: Use hosts in a task file

Or in the /ntp/tasks/main.yml file, could we use hosts filters to control what runs on what hosts?

- hosts: all
- name: stuff all host do
 module: modulename attribute=value

- hosts: ntp
- name: stuff just for ntp server
 module: modulename attribute=value

- hosts: all,!ntp
- name: stuff for all hosts (other then ntp servers)
 module: modulename attribute=value

Should this be done with conditionals instead somehow?

chuckbag
  • 11
  • 2

1 Answers1

2

If they differ only by that one template, then you can move that assignment into the inventory, or use group_vars (an equivalent outcome, but doesn't require polluting your inventory file)

[all:vars]
ntp_conf_template=ntp-client.j2
; ...etc...

[ntp]
10.1.4.1
10.1.4.2

[ntp:vars]
ntp_conf_template=ntp-server.j2 

then, in the task:

- name: generate ntp.conf
  template:
    src: '{{ ntp_conf_template }}'
    dest: /etc/ntp.conf
mdaniel
  • 31,240
  • 5
  • 55
  • 58