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.
Requirements:
To follow along with this post, we assume that you have the following:
- Ansible controller
- Administrative permissions on the target machine
- Remote machines or servers where you wish to change the timezone. These should be accessible over SSH.
- 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:
ansible.builtin.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.
Next, edit the file:
Next, ensure that you have your target hosts configured. For example, you can run the list hosts command as follows:
This should return all the hosts configured in your Ansible inventory. An example output is as follows:
172.18.0.2
172.18.0.6
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
tasks:
- name: Set timezone to America/New_York
ansible.builtin.timezone:
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:
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:
For CentOS/RHEL/Fedora:
$ sudo dnf install tzdata
For Arch Linux:
In the ansible-playbook, we tell Ansible to ask for the “become” password using the -K parameter.
There you have it!
Conclusion
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.