Time Ansible Role¶
An Ansible role to set the system timezone (via timedatectl) and configure systemd-timesyncd by default, or chrony if explicitly selected, on Debian-based systems.
Features¶
- Install and manage
systemd-timesyncdorchrony - Configure primary and fallback NTP servers in
/etc/systemd/timesyncd.conf - Configure chrony sources in
/etc/chrony/sources.d/60-ansible.sources - Set timezone with
timedatectl(best-effort) - Optionally enable/disable time synchronization services
Default behavior¶
- The default daemon is
systemd-timesyncd. chronyis optional.- The role installs
chronyonly when you settime_sync_daemon: chrony.
Installation¶
Install the role with ansible-galaxy:
Role Variables¶
| Variable | Default Value | Type | Description |
|---|---|---|---|
time_timezone |
Europe/Berlin |
string | IANA timezone to apply (timedatectl set-timezone) |
time_ntp_enabled |
true |
boolean | Enable/disable systemd-timesyncd service |
time_sync_daemon |
timesyncd |
string | Time sync daemon to manage: timesyncd or chrony |
time_timesyncd_unit |
systemd-timesyncd |
string | Service unit name |
time_chrony_unit |
chrony |
string | Chrony service unit name |
time_packages |
['tzdata'] |
list[str] | Base packages to install before configuring time/NTP |
time_timesyncd_packages |
['systemd-timesyncd'] |
list[str] | Packages installed when time_sync_daemon: timesyncd |
time_chrony_packages |
['chrony'] |
list[str] | Packages installed when time_sync_daemon: chrony |
systemd_timesyncd_ntp_servers |
see below | str/list | Primary NTP servers written to /etc/systemd/timesyncd.conf |
systemd_timesyncd_fallback_ntp_servers |
see below | str/list | Fallback NTP servers written to /etc/systemd/timesyncd.conf |
time_chrony_pools |
['pool pool.ntp.org iburst maxsources 4'] |
list[str] | Raw chrony pool source lines |
time_chrony_servers |
[] |
list[str] | Chrony servers converted to server <host> iburst lines |
ansible_managed_warning |
"This file is managed by Ansible. Manual changes will be overwritten." |
string | Text to embed as a header comment when templating files |
ansible_role_repository |
https://gitlab.com/niclas-zone/tools/ansible/roles/time |
string | Repository URL for this role |
The role defaults to Europe/Berlin, but you should override time_timezone with the location needed in your environment (for example Europe/Zurich).
Default NTP servers¶
systemd_timesyncd_ntp_servers:
- "ntp.server.local"
systemd_timesyncd_fallback_ntp_servers:
- "0.de.pool.ntp.org"
- "1.de.pool.ntp.org"
- "2.de.pool.ntp.org"
- "4.de.pool.ntp.org"
Example Playbooks¶
Basic: set timezone + enable NTP¶
---
- hosts: servers
become: true
roles:
- role: time
vars:
time_timezone: "Europe/Zurich"
time_ntp_enabled: true
systemd_timesyncd_ntp_servers:
- "0.pool.ntp.org"
- "1.pool.ntp.org"
systemd_timesyncd_fallback_ntp_servers:
- "time.google.com"
- "time.cloudflare.com"
Custom NTP servers¶
---
- hosts: servers
become: true
roles:
- role: time
vars:
time_timezone: "Europe/Zurich"
time_ntp_enabled: true
systemd_timesyncd_ntp_servers:
- "0.pool.ntp.org"
- "1.pool.ntp.org"
systemd_timesyncd_fallback_ntp_servers:
- "time.google.com"
- "time.cloudflare.com"
Use chrony¶
---
- hosts: servers
become: true
roles:
- role: time
vars:
time_sync_daemon: chrony
time_chrony_pools:
- "pool pool.ntp.org iburst maxsources 4"
time_chrony_servers:
- "time.cloudflare.com"