Set Timezone in Ansible

Ansible is a free and open-source automation tool that provides compelling features and a framework for managing and configuring machines, application deployment, and updates.

As a system administrator, one of the most common tasks you will encounter is configuring timezones in different machines. Timezone synchronization is essential across systems, especially when dealing with time-sensitive operations such as Kerberos, etc.

This tutorial explores configuring a timezone on a remote system using Ansible modules.


To follow along with this post, we assume that you have the following:

  1. Ansible controller
  2. Administrative permissions on the target machine
  3. Remote machines or servers where you wish to change the timezone. These should be accessible over SSH.
  4. Basic understanding of YAML and Ansible playbooks

With the given requirements met, we can proceed to the tutorial.

The Ansible Timezone Module

To configure timezones in Ansible, we use the timezone module. This module allows us to configure the time zones in remote machines by synchronizing the system and hardware clocks.

Depending on the target machine’s operating system, the module either uses the timedatectl or edit the /etc/sysconfig/clock or the /etc/timezone files. The tool uses the systemsetup and /etc/localtime for BSD systems on macOS.

Module Syntax

The following demonstrates the basic syntax of the timezone module in Ansible:

- name: Set timezone
    name: target_timezone

The name parameter defines the name of the timezone for the system clock. If you set it to default, the module keeps the current setting.

Example Usage

Let us look at some basic examples on how we can set up the timezone on our remote machines using the timezone module in an Ansible playbook.

Start by creating a playbook. For this, we create a new file called “timezones.yaml” and edit it with an editor of our choice.

$ touch ansible/timezone.yaml

Next, edit the file:

$ vim ansible/timezone.yaml

Next, ensure that you have your target hosts configured. For example, you can run the list hosts command as follows:

$ ansible <strong>all</strong> --list-hosts

This should return all the hosts configured in your Ansible inventory. An example output is as follows:

hosts (2):

In this case, we have two hosts in the inventory.

Once we verified that all the target hosts are configured correctly, we can learn how to configure the timezones on the target machines.

We can do this by editing the playbook and adding the configuration as follows:

- name: Set Timezone on Hosts in Lab1
  hosts: lab1
  become: yes
  - name: Set timezone to America/New_York
      name: 'America/New_York'

Let us breakdown the given playbook.

We start by setting the name of the playbook. This describes what the playbook is doing.

Next, the hosts module allows us to define the hosts that we wish to run the playbook. In this case, we want to run the playbook in the hosts in lab1 as described in the inventory. Replace this with your correct value. For example, setting the value to all will run the playbook in all the hosts that are defined in the inventory file.

Next, we use the “become: yes” module to allow Ansible to execute the playbook with administrative permissions.

Finally, we call the “ansible.builtin.timezone” module with the value of the timezone that we wish to configure. In this case, we wish to set the timezone on the remote machines to America/New_York.

Once satisfied with the configuration, save and close the file.

Run the playbook using the following command:

$ ansible-playbook ansible/timezone.yaml -K

The previous command uses the ansible-playbook command to run the instructions that are defined in the “timezone.yaml” file. In this case, we change the timezone on the target machine to America/New_York.

NOTE: You need to ensure that the timezones are installed on your system. If not, reference the commands as follows:

$ sudo apt-get install tzdata

For CentOS/RHEL/Fedora:

$ sudo yum install tzdata
$ sudo dnf install tzdata

For Arch Linux:

$ sudo pacman -Sy tzdata

In the ansible-playbook, we tell Ansible to ask for the “become” password using the -K parameter.

There you have it!


We learned how we can utilize the Ansible timezone module to manage and synchronize the timezones across multiple remote systems as configured in your Ansible inventory.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list