custom ntp setup with ansible

ansible-install | ansible | ansible-ntp | ansible-rhel

setup timezone, manually sync, configure ntp and hardware clock

mkdir /etc/ansible/group_vars/
cd /etc/ansible/group_vars/
vi hosts.yml

timezone: Europe/Paris
ntpservers:
  - ntp_address1
  - ntp_address2

get some clean ntp.conf sample for every target system and tune it accordingly

sed '/^$/d; /^#/d' /etc/ntp.conf > /etc/ansible/ntp.clean.conf
vi /etc/ansible/ntp.conf

{%for ntpserver in ntpservers %}
    server {{ ntpserver }} iburst
{% endfor %}

rhel

vi /etc/ansible/ntp.rhel.conf

driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

{%for ntpserver in ntpservers %}
    server {{ ntpserver }} iburst
{% endfor %}

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

debian/ubuntu

vi /etc/ansible/ntp.ubuntu.conf

driftfile /var/lib/ntp/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list
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

{%for ntpserver in ntpservers %}
    server {{ ntpserver }} iburst
{% endfor %}

restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited
restrict 127.0.0.1
restrict ::1
restrict source notrap nomodify noquery

slackware

vi /etc/ansible/ntp.slack.conf

server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

{%for ntpserver in ntpservers %}
    server {{ ntpserver }} iburst
{% endfor %}

statsdir /var/lib/ntp/stats
logfile /var/log/ntp
driftfile /var/lib/ntp/drift
pidfile /var/run/ntpd.pid
restrict default limited kod nomodify notrap nopeer noquery
restrict -6 default limited kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

playbooks

rhel

vi  /etc/ansible/ntp.rhel.yml

- hosts: hosts
  tasks:
  - name: timezone {{ timezone }}
    shell: timedatectl set-timezone {{ timezone }}

  - name: pkg ntpdate
    yum: name=ntpdate state=present

  - name: pkg ntp
    yum: name=ntp state=present

  - name: NTP down
    service: name=ntpd state=stopped enabled=yes

  - name: sync against {{ ntpservers[0] }}
    shell: ntpdate -u {{ ntpservers[0] }}

  - name: ntp conf pointing to {{ ntpservers }}
    template: src=ntp.rhel.conf dest=/etc/ntp.conf

  - name: NTP up
    service: name=ntpd state=started enabled=yes

  - name: hardware clock sync
    shell: hwclock --utc --systohc

slackware

draft – slackpkg check works

vi  /etc/ansible/ntp.slack.yml

- hosts: hosts
  tasks:
  - name: install ntp on slackware
    slackpkg: name=ntp state=present

  - name: NTP down
    service: name=ntpd state=stopped enabled=yes

  - name: sync against {{ ntpservers[0] }}
    shell: ntpdate -u {{ ntpservers[0] }}

  - name: ntp conf pointing to {{ ntpservers }}
    template: src=ntp.clean-slack.conf dest=/etc/ntp.conf

  - name: NTP up
    service: name=ntpd state=started enabled=yes

  - name: hardware clock sync
    shell: hwclock --utc --systohc

ready to go

apply and check on the target systems

ansible-playbook ntp.ubuntu.yml
ansible-playbook ntp.slack.yml

ansible hosts -m shell -a "ls -lhF /etc/localtime"
ansible hosts -m shell -a "ntpq -p"
ansible hosts -m shell -a "ntpdc -c sysinfo"
ansible hosts -m shell -a "grep ^server /etc/ntp.conf"
ansible hosts -m shell -a "date"

resources

http://www.opensourcerers.org/setting-up-ntp-via-ansible-in-my-private-lab/

https://github.com/ansible/ansible-examples/blob/master/lamp_simple/roles/common/templates/ntp.conf.j2

https://stackoverflow.com/questions/24798382/how-to-assign-an-array-to-a-variable-in-an-ansible-playbook

https://stackoverflow.com/questions/41610207/how-to-get-the-first-element-of-a-list-from-the-output-of-setup-module-in-ansibl

https://stackoverflow.com/questions/36667042/how-to-specify-an-array-or-list-element-fact-with-yaml

slackware

community.general.slackpkg – Package manager for Slackware >= 12.2 https://docs.ansible.com/ansible/latest/collections/community/general/slackpkg_module.html


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5 | CONTACT
Copyright © 2024 Pierre-Philipp Braun