Setting NTP on Ubuntu servers in AWS with ansible

A simple, but often overlooked task when running a bunch of servers is making sure they’re all time-synced. Thankfully AWS makes sure a reliable time source is always accessible from your servers. The documentation from AWS is linked below.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html

Here’s a simple ansible playbook to get you started:

- hosts: all
  vars:
  remote_user: youruser
  become: true
  become_user: root
  become_method: sudo
  become_flags: '-i'
  tasks:
  - name: install chrony
    apt:
            name: chrony
            state: latest
  - name: set chrony ntp config
    template:
            src: /etc/ansible/resources/chrony.conf
            dest: /etc/chrony/chrony.conf
            owner: root
            group: root
            mode: 0644
  - name: enable chrony             
    systemd:
            name: chrony.service
            state: started
            enabled: yes

Make sure you replace the source of the chrony.conf file with the appropriate file. My chrony.conf is linked below. The link-local address should be included to take advantage of AWS’s time servers.

server 169.254.169.123 prefer iburst minpoll 4 maxpoll 4
pool ntp.ubuntu.com        iburst maxsources 4
keyfile /etc/chrony/chrony.keys
driftfile /var/lib/chrony/chrony.drift
#log tracking measurements statistics
logdir /var/log/chrony
maxupdateskew 100.0
rtcsync
makestep 1 3

This should result in your servers having the correct time, assuming you set the time-zone correctly.

Good luck!

Leave a comment