This is an Ansible Role to deploy and configure the ntpsec package to provide a Network Time Protocol server according to parameters held in my Ansible Inventory

Manual Process

Deploy the package using apt install ntps and then update the /etc/ntpsec/ntp.conf file with the required parameters.

Ansible Role: ntp-server

This was created for Building an additional Samba 4 Domain Controller 2

File Structure

This is a standard Ansible role with as single tasks .yaml file, a meta to keep Ansible happy and a README.md for my own notes.

host-statichosts  

  meta

    main.yaml

  tasks     

    main.yaml  

  templates

    ntp.conf.j2

  README.md

tasks/main.yaml

--- # Ansible Role Tasks

# Role for deploying NTP server

- name: Deploy package for NTP server
  become: true
  ansible.builtin.apt:
    update_cache: yes
    pkg: ntpsec

- name: Drop NTP config
  become: true
  ansible.builtin.template:
    src: ntp.conf.j2
    dest: /etc/ntpsec/ntp.conf
    owner: root
    group: root
    mode: '0755'

- name: Create NTP log file
  become: true
  ansible.builtin.file:
    path: /var/log/ntpsec
    state: directory
    owner: ntpsec
    group: ntpsec
    mode: '644'

# logrotate? Need to see logs in action first.

- name: Enable and start NTP
  become: true
  ansible.builtin.service:
    name: ntpsec
    enabled: true
    state: restarted

...

The first task deploys the ntpsec package using apt. We then deploy the standard NTP configuration over the top of the default installed by the ntpsec package with our configuation added by jinja-2 template. It then creates a directory for NTP log file, more to stop a warning than anything else and then finally restarts the ntpsec service so the changes take effect immediately as we need time to be properly set before we attempt to configure Samba 4.

templates/ntp.conf.j2

This is the standard config from the ntpsec package with the following changes:

  1. All existing pool and server config commented out since I have NTP servers already defined for each network and I don't need to have each server doing its own thing.
  2. The NTP server for the network is added according to the Ansible Inventory network_ntp parameter
  3. Using the hardware clock is enabled as a fallback should I lose internet connectivity. In a worst case scenario it doesn't matter what time the hosts think it is as long as they all agree to use the same time otherwise things like Kerberos break.
  4. ntpsignedsocket is enabled
  5. mssntp is enabled

# /etc/ntpsec/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntpsec/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list

# To enable Network Time Security support as a server, obtain a certificate
# (e.g. with Let's Encrypt), configure the paths below, and uncomment:
# nts cert CERT_FILE
# nts key KEY_FILE
# nts enable

# You must create /var/log/ntpsec (owned by ntpsec:ntpsec) to enable logging.
#statsdir /var/log/ntpsec/
#statistics loopstats peerstats clockstats
#filegen loopstats file loopstats type day enable
#filegen peerstats file peerstats type day enable
#filegen clockstats file clockstats type day enable

# This should be maxclock 7, but the pool entries count towards maxclock.
tos maxclock 11

# Comment this out if you have a refclock and want it to be able to discipline
# the clock by itself (e.g. if the system is not connected to the network).
tos minclock 4 minsane 3

# Specify one or more NTP servers.
server {{ network_ntp }} iburst prefer
# server-dc1 192.168.x.x iburst
# server-dc2 192.168.x.x iburst

# Public NTP servers supporting Network Time Security:
# server time.cloudflare.com nts
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See https://www.pool.ntp.org/join.html for
# more information.
#pool 0.ubuntu.pool.ntp.org iburst
#pool 1.ubuntu.pool.ntp.org iburst
#pool 2.ubuntu.pool.ntp.org iburst
#pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
#server ntp.ubuntu.com

# Use hardware clock if all else fails
server 127.127.1.1
fudge 127.127.1.1 stratum 15

# Socket for signing time for domain
ntpsigndsocket /var/lib/samba/ntp_signd/

# Access control configuration; see /usr/share/doc/ntpsec-doc/html/accopt.html
# for details.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict default kod nomodify nopeer noquery limited mssntp

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1