• +43 660 1453541
  • contact@germaniumhq.com

Synchronizing Time With NTP and Ansible


Synchronizing Time With NTP and Ansible

When having multiple machines it makes sense to have them all using the same time. Otherwise it’s pretty hard to track what happened where, especially when looking at logs. This is managed using the Network Time Protocol (NTP), and there’s a package for that named ntp.

NTP is a protocol that synchronizes time against a time server. This ensures that all the nodes in our infrastructure use the same time, without us manually fiddling with the time on each one. When analyzing logs that span over multiple hosts it becomes evident why we need to have the times in sync, since otherwise it’s difficult to find out what happened.

The good news is that after the ntp installation, a service is created and automatically enabled, so we only install the package.

sudo apt install ntp

This also translates that in Ansbile we need only to ensure the package is there, and the service is enabled. Since in my case the systems are running Ubuntu, I am doing:

- name: Install the NTP daemon to synchronize the clocks to ms.
  apt: name=ntp

- name: Make sure ntp is started, and is enabled on restart.
  service: name=ntp state=started enabled=yes

An extra touch that I like is to have a specific timezone - in my case CET, but configurable via a variable:

- name: Set the timezone to the {{new_host_timezone}} one
  timezone: "name={{new_host_timezone}}"

That’s it. Enjoy sane times on logs!