Timezone change

Hi

Following is the script to change the timezone in ec2-instance

vars:

  • timezone: Canada/Eastern

  • name: Check current timezone

shell: awk -F" ‘{ print $2}’ /etc/sysconfig/clock

register: current_zone

changed_when: False

  • name: Set EST timezone

file: src=/usr/share/zoneinfo/{{ timezone }} dest=/etc/localtime state=link force=yes

when: current_zone.stdout != ‘{{ timezone }}’

i wanted to replace the following /etc/sysconfig/clock UTC entry with variable value defined above. Can someone help me with steps in ansible or shell command to replace the zone ? Thanks in advance.

$ cat /etc/sysconfig/clock
ZONE=“UTC” # UTC to be replaced with Canada/Eastern
UTC=true

got it resolved.

i used the lineinfile module to replace with the timezone

  • lineinfile:
    path: /etc/sysconfig/clock
    regexp: ‘ZONE=’
    line: ‘ZONE=“{{ timezone }}”’
    state: present
    backup: yes
    when: current_zone.stdout != ‘{{ timezone }}’

Use module "community.general.timezone"
https://docs.ansible.com/ansible/latest/collections/community/general/timezone_module.html

  - name: Set timezone to Canada/Eastern
    community.general.timezone:
      name: Canada/Eastern