Written By :

Category :

Ops

Posted On :

Share This :

Mastering the ansible timezone playbook for linux systems in 5 easy steps

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:

  1. Install Ansible on your control node. You can find installation instructions for your system in the official Ansible documentation.
  2. 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.
ansible timezone playbook
ansible timezone playbook
[targets]
target1 ansible_host=192.168.1.10
target2 ansible_host=192.168.1.20
  1. Create a new Ansible timezone playbook file, e.g., set_timezone.yml, and add the following content to it. Replace America/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
  1. 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:

  1. Ansible Documentation
  2. Ansible Timezone Module
  3. Ansible Best Practices

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:

  1. 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
  1. 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 }}"
  1. 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"
  1. Error handling: To make your Ansible timezone playbook more resilient, use block, rescue, and always 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."
  1. 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”

  1. 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!

Leave a Reply

7 responses to “Mastering the ansible timezone playbook for linux systems in 5 easy steps”

  1. консультация юриста Avatar

    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!

  2. equiti login Avatar

    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.

  3. xm trading Avatar

    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!

  4. pocket option Avatar

    Wonderful, what a webpage it is! This web site gives useful
    information to us, keep it up.

  5. exness Avatar

    This blog was… how do you say it? Relevant!!

    Finally I have found something that helped me. Kudos!

  6. traf partners Avatar

    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…

Leave a Reply