Introduction
Managing timezones on multiple Linux systems can be a challenging task. However, with an Ansible timezone playbook, you can easily automate this process. In this blog post, we’ll guide you through the steps to create an efficient Ansible timezone playbook to set the timezone on your Linux machines. We will also provide a list of timezones, a link to the latest timezone list, and additional resources to help you learn more about Ansible.
Creating an Ansible Timezone Playbook:
- Install Ansible on your control node. You can find installation instructions for your system in the official Ansible documentation.
- Create an inventory file, typically named
hosts.ini
, to list the target systems you want to configure. Replace the placeholders with your actual target system IP addresses or hostnames.

[targets]
target1 ansible_host=192.168.1.10
target2 ansible_host=192.168.1.20
- Create a new Ansible timezone playbook file, e.g.,
set_timezone.yml
, and add the following content to it. ReplaceAmerica/New_York
with the desired timezone:
---
- name: Set timezone on Linux machines
hosts: targets
become: yes
tasks:
- name: Set the timezone
ansible.builtin.timezone:
name: America/New_York
- Run the playbook using the following command
ansible-playbook -i hosts.ini set_timezone.yml
Sample Output
PLAY [Set timezone on Linux machines] *****************************************
TASK [Gathering Facts] *********************************************************
ok: [target1]
ok: [target2]
TASK [Set the timezone] ********************************************************
changed: [target1]
changed: [target2]
PLAY RECAP *********************************************************************
target1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
target2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Timezone List:
You can find the list of available timezones on your target systems by checking the /usr/share/zoneinfo
directory. For a full list of IANA timezones, please visit the IANA Time Zone Database.
Helpful Resources:
YouTube Video:
To learn more about the Ansible timezone playbook and its various modules, check out this helpful YouTube video: Ansible Beginner to Pro
Setting timezone using a var in playbook
Here are some additional tips and best practices that you can include in the blog post to provide more value to your readers:
- Tagging tasks: Using tags in your Ansible timezone playbook can help you manage and execute specific parts of your playbook more efficiently. For example:
- name: Set the timezone
ansible.builtin.timezone:
name: America/New_York
tags:
- timezone
You can then run the playbook with only the tasks containing the specified tag:
ansible-playbook -i hosts.ini set_timezone.yml --tags timezone
- Using variables: Encourage the use of variables to make the playbook more flexible and reusable. For example, you can define a variable for the timezone in the playbook or use an external variable file:
---
- name: Set timezone on Linux machines using Ansible timezone playbook
hosts: targets
become: yes
vars:
desired_timezone: America/New_York
tasks:
- name: Set the timezone
ansible.builtin.timezone:
name: "{{ desired_timezone }}"
- Implementing conditionals: Sometimes, you may want to apply different timezones to different groups of servers. In such cases, you can use Ansible’s conditional statements like
when
:
tasks:
- name: Set the timezone for Ubuntu machines
ansible.builtin.timezone:
name: America/New_York
when: "'ubuntu' in ansible_distribution"
- name: Set the timezone for Red Hat machines
ansible.builtin.timezone:
name: America/Los_Angeles
when: "'RedHat' in ansible_distribution"
- Error handling: To make your Ansible timezone playbook more resilient, use
block
,rescue
, andalways
statements to handle errors and perform cleanup tasks:
tasks:
- block:
- name: Set the timezone
ansible.builtin.timezone:
name: America/New_York
rescue:
- name: Handle timezone setting failure
debug:
msg: "Failed to set timezone."
always:
- name: Always executed task
debug:
msg: "This task is always executed, regardless of success or failure."
- Testing your playbook: Encourage users to run their playbooks in “check mode” before applying changes to the target systems. Check mode allows you to see what changes would be made without actually making them:
ansible-playbook -i hosts.ini set_timezone.yml --check
By incorporating these tips and best practices into your blog post, you can provide readers with more comprehensive guidance on using the Ansible timezone playbook effectively.
Conclusion:
In this blog post, we’ve demonstrated how to create an Ansible timezone playbook to set the timezone on multiple Linux systems. By following these steps, you can efficiently manage and automate timezone settings across your infrastructure. This Ansible timezone playbook works seamlessly with both Ubuntu and Red Hat-based Linux distributions, making it a powerful tool for managing timezones on your Linux machines.
To learn more about Ansible, check out our Devops and SRE section.
7 Comments On “Mastering the ansible timezone playbook for linux systems in 5 easy steps”
Leave a Reply
You must be logged in to post a comment.
7 responses to “Mastering the ansible timezone playbook for linux systems in 5 easy steps”
Wow, marvelous blog format! How long have you ever been blogging
for? you make running a blog look easy. The entire look of your site is wonderful, let alone the content!If some one desires expert view regarding running
a blog after that i recommend him/her to pay a quick visit this blog, Keep up the pleasant job.I seriously love your site.. Pleasant colors & theme.
Did you create this website yourself? Please reply back as I’m planning to create my own blog
and would like to know where you got this from or exactly what the theme is called.
Thanks!Wonderful, what a webpage it is! This web site gives useful
information to us, keep it up.This blog was… how do you say it? Relevant!!
Finally I have found something that helped me. Kudos!
I want to to thank you for this wonderful read!!
I certainly enjoyed every bit of it. I’ve got you saved as a favorite to look
at new stuff you post…Hi, thanks for the comments. https://hostinger.com?REFERRALCODE=1RON645
Leave a Reply
You must be logged in to post a comment.
Hi, thanks for the comments. https://hostinger.com?REFERRALCODE=1RON645
I want to to thank you for this wonderful read!!
I certainly enjoyed every bit of it. I’ve got you saved as a favorite to look
at new stuff you post…
This blog was… how do you say it? Relevant!!
Finally I have found something that helped me. Kudos!
Wonderful, what a webpage it is! This web site gives useful
information to us, keep it up.
I seriously love your site.. Pleasant colors & theme.
Did you create this website yourself? Please reply back as I’m planning to create my own blog
and would like to know where you got this from or exactly what the theme is called.
Thanks!
If some one desires expert view regarding running
a blog after that i recommend him/her to pay a quick visit this blog, Keep up the pleasant job.
Wow, marvelous blog format! How long have you ever been blogging
for? you make running a blog look easy. The entire look of your site is wonderful, let alone the content!