Timezone change

18 views
Skip to first unread message

Vineeth Joseph Abraham

unread,
Sep 24, 2020, 12:04:03 PM9/24/20
to Ansible Project
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




Vineeth Joseph Abraham

unread,
Sep 24, 2020, 1:17:28 PM9/24/20
to Ansible Project
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 }}'



Vladimir Botka

unread,
Sep 24, 2020, 10:31:33 PM9/24/20
to Vineeth Joseph Abraham, ansible...@googlegroups.com
On Thu, 24 Sep 2020 09:04:03 -0700 (PDT)
Vineeth Joseph Abraham <vine...@gmail.com> wrote:

> ... Can someone help me with steps in ansible or
> shell command to replace the zone ?

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

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages