Introduction
community.general.timezone sets the system timezone on Linux hosts. Consistent time configuration prevents authentication failures, log correlation issues, cron scheduling bugs, and certificate validation errors.
Basic Usage
- name: Set timezone to UTC
community.general.timezone:
name: UTC
become: true
- name: Set timezone to New York
community.general.timezone:
name: America/New_York
become: true
- name: Set timezone to Berlin
community.general.timezone:
name: Europe/Berlin
become: true
Parameters
| Parameter | Description |
|---|---|
name | Timezone name (e.g., UTC, America/New_York) |
hwclock | Hardware clock: UTC or local |
Common Timezones
| Region | Timezone |
|---|---|
| UTC | UTC |
| US East | America/New_York |
| US Central | America/Chicago |
| US Pacific | America/Los_Angeles |
| UK | Europe/London |
| Germany | Europe/Berlin |
| Japan | Asia/Tokyo |
| India | Asia/Kolkata |
| Australia | Australia/Sydney |
List all available timezones:
timedatectl list-timezones
Set Timezone from Variable
# Per-host timezone
- name: Set timezone
community.general.timezone:
name: "{{ host_timezone | default('UTC') }}"
become: true
Inventory:
[webservers]
web-us ansible_host=10.0.1.10 host_timezone=America/New_York
web-eu ansible_host=10.0.2.10 host_timezone=Europe/Berlin
web-ap ansible_host=10.0.3.10 host_timezone=Asia/Tokyo
Configure NTP (Time Synchronization)
Chrony (Modern — RHEL 8+, Ubuntu 20.04+)
- name: Install chrony
ansible.builtin.package:
name: chrony
state: present
- name: Configure chrony
ansible.builtin.template:
src: chrony.conf.j2
dest: /etc/chrony/chrony.conf
notify: Restart chrony
- name: Start chrony
ansible.builtin.service:
name: "{{ 'chronyd' if ansible_os_family == 'RedHat' else 'chrony' }}"
state: started
enabled: true
handlers:
- name: Restart chrony
ansible.builtin.service:
name: "{{ 'chronyd' if ansible_os_family == 'RedHat' else 'chrony' }}"
state: restarted
{# templates/chrony.conf.j2 #}
{% for server in ntp_servers | default(['pool.ntp.org']) %}
server {{ server }} iburst
{% endfor %}
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
systemd-timesyncd (Lightweight)
- name: Configure timesyncd
ansible.builtin.template:
src: timesyncd.conf.j2
dest: /etc/systemd/timesyncd.conf
notify: Restart timesyncd
- name: Enable NTP sync
ansible.builtin.command: timedatectl set-ntp true
changed_when: false
Complete Time Configuration Playbook
---
- name: Configure time
hosts: all
become: true
vars:
timezone: UTC
ntp_servers:
- time.google.com
- time.cloudflare.com
- pool.ntp.org
tasks:
- name: Set timezone
community.general.timezone:
name: "{{ timezone }}"
- name: Set hardware clock to UTC
community.general.timezone:
hwclock: UTC
- name: Install chrony
ansible.builtin.package:
name: chrony
state: present
- name: Configure NTP servers
ansible.builtin.template:
src: chrony.conf.j2
dest: "{{ '/etc/chrony.conf' if ansible_os_family == 'RedHat' else '/etc/chrony/chrony.conf' }}"
notify: Restart chrony
- name: Start chrony
ansible.builtin.service:
name: "{{ 'chronyd' if ansible_os_family == 'RedHat' else 'chrony' }}"
state: started
enabled: true
- name: Verify time sync
ansible.builtin.command: chronyc tracking
register: chrony_status
changed_when: false
- name: Show sync status
ansible.builtin.debug:
msg: "{{ chrony_status.stdout_lines[:3] }}"
handlers:
- name: Restart chrony
ansible.builtin.service:
name: "{{ 'chronyd' if ansible_os_family == 'RedHat' else 'chrony' }}"
state: restarted
Ansible Date/Time Facts
- name: Show time facts
debug:
msg:
- "Date: {{ ansible_date_time.date }}" # 2026-04-12
- "Time: {{ ansible_date_time.time }}" # 14:30:00
- "TZ: {{ ansible_date_time.tz }}" # UTC
- "Epoch: {{ ansible_date_time.epoch }}" # 1776192600
- "ISO: {{ ansible_date_time.iso8601 }}" # 2026-04-12T14:30:00Z
- "Weekday: {{ ansible_date_time.weekday }}" # Sunday
Windows Timezone
- name: Set Windows timezone
community.windows.win_timezone:
timezone: "Eastern Standard Time"
# Note: Windows uses different timezone names
# Configure Windows NTP
- name: Set NTP server
ansible.windows.win_command: >
w32tm /config /manualpeerlist:"time.google.com" /syncfromflags:manual /update
Troubleshooting
"Timezone not found"
# List valid timezones
timedatectl list-timezones | grep -i york
# America/New_York
Time Drift After Configuration
# Force immediate sync
- command: chronyc -a makestep
become: true
changed_when: false
Related Articles
Conclusion
Set timezone with community.general.timezone, then configure NTP with chrony for accurate time synchronization. Use UTC for servers (consistent logs, no DST issues) and local timezones for user-facing systems. Always pair timezone configuration with NTP — correct timezone without synchronized time still causes problems. The ansible_date_time facts give you access to the current time in playbooks for timestamps and scheduling logic.