AnsiblePlaybooksSetting the time zone

Setting the time zone

This examples sets the target time zone to Europe/Berlin.

Creating the playbook

Executed:

aadmin@uvm0:~/code/it-wiz/devops/ansible/projects/playbooks$ touch set_timezone.yml

Brought set_timezone.yml to the following form:

- name: Set timezone
  hosts: all
  tasks:
  - name: Get current timezone
    ansible.builtin.command: timedatectl show --property=Timezone --value
    register: timezone_output
    changed_when: false

  - name: Display current timezone
    ansible.builtin.debug:
      msg: "Current timezone is {{ timezone_output.stdout }}"

  - name: Set timezone to Europe/Berlin
    community.general.timezone:
      name: "Europe/Berlin"

  - name: Get updated timezone
    ansible.builtin.command: timedatectl show --property=Timezone --value
    register: timezone_output
    changed_when: false

  - name: Display updated timezone
    ansible.builtin.debug:
      msg: "Current timezone is {{ timezone_output.stdout }}"

Equivalent Bash commands

root@uvm26:~# timedatectl set-timezone Europe/Berlin
root@uvm27:~# timedatectl set-timezone Europe/Berlin

Execution of the playbook

Executed:

aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ ansible-playbook playbooks/set_timezone.yml –ask-vault-password

Details

    aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ ansible-playbook playbooks/set_timezone.yml –ask-vault-password
    Vault password:
 
    PLAY [Set timezone] **********************************************************************************************
 
    TASK [Gathering Facts] *******************************************************************************************
    ok: [uvm27]
    ok: [uvm26]
 
    TASK [Get current timezone] **************************************************************************************
    ok: [uvm27]
    ok: [uvm26]
 
    TASK [Display current timezone] **********************************************************************************
    ok: [uvm26] => {
        "msg": "Current timezone is Etc/UTC"
    }
    ok: [uvm27] => {
        "msg": "Current timezone is Etc/UTC"
    }
 
    TASK [Set timezone to Europe/Berlin] *****************************************************************************
    changed: [uvm27]
    changed: [uvm26]
 
    TASK [Get updated timezone] **************************************************************************************
    ok: [uvm26]
    ok: [uvm27]
 
    TASK [Display updated timezone] **********************************************************************************
    ok: [uvm26] => {
        "msg": "Current timezone is Europe/Berlin"
    }
    ok: [uvm27] => {
        "msg": "Current timezone is Europe/Berlin"
    }
 
    PLAY RECAP *******************************************************************************************************
    uvm26                      : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    uvm27                      : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Result

The time zone got updated.