# Timezone change

**URL:** <https://forum.ansible.com/t/timezone-change/33889>\
**Category:** Ansible Project\
**Created:** [September 24, 2020, 4:04pm UTC](https://forum.ansible.com/t/timezone-change/33889 "2020-09-24T16:04:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vineeth\_Joseph\_Abrah](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@Vineeth\_Joseph\_Abrah](https://forum.ansible.com/u/Vineeth_Joseph_Abrah)\
**Post date:** [September 24, 2020, 4:04pm UTC](https://forum.ansible.com/t/timezone-change/33889/1 "2020-09-24T16:04:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vineeth\_Joseph\_Abrah](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@Vineeth\_Joseph\_Abrah](https://forum.ansible.com/u/Vineeth_Joseph_Abrah)\
**Post date:** [September 24, 2020, 5:17pm UTC](https://forum.ansible.com/t/timezone-change/33889/2 "2020-09-24T17:17:28Z")

</div>

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 }}’

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 25, 2020, 2:31am UTC](https://forum.ansible.com/t/timezone-change/33889/3 "2020-09-25T02:31:01Z")

</div>

Use module "community.general.timezone"  
[https://docs.ansible.com/ansible/latest/collections/community/general/timezone\_module.html](https://docs.ansible.com/ansible/latest/collections/community/general/timezone_module.html)

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