Skip to content

Instantly share code, notes, and snippets.

@garethrees
Last active October 14, 2020 19:44
Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save garethrees/5591027 to your computer and use it in GitHub Desktop.
Save garethrees/5591027 to your computer and use it in GitHub Desktop.
Ansible set timezone on Ubuntu host
- name: Group by Distribution
hosts: all
tasks:
- group_by: key=${ansible_distribution}
- name: Set Time Zone
hosts: Ubuntu
gather_facts: False
tasks:
- name: Set timezone variables
copy: content='America/Los_Angeles'
dest=/etc/timezone
owner=root
group=root
mode=0644
backup=yes
notify:
- update timezone
handlers:
- name: update timezone
command: dpkg-reconfigure --frontend noninteractive tzdata
vagrant@us:~$ ll /etc/ | grep timezone
-rw-r--r-- 1 root root 20 May 16 06:27 timezone
-rw-r--r-- 1 root root 20 May 16 06:22 timezone.2013-05-16@06:24~
-rw-r--r-- 1 root root 20 May 16 06:24 timezone.2013-05-16@06:27~
vagrant@us:~$ cat /etc/timezone
America/Los_Angeles
vagrant@us:~$ cat /etc/timezone.2013-05-16@06:24~
America/Los_Angeles
vagrant@us:~$ cat /etc/timezone.2013-05-16@06:27~
America/Los_Angeles
vagrant@us:~$
@garethrees
Copy link
Author

Each play:

PLAY [Set Time Zone] *********************

TASK: [Set timezone variables] *********************
changed: [dev-myapp-web-1]

NOTIFIED: [update timezone] *********************
changed: [dev-myapp-web-1]

@BennyH26
Copy link

BennyH26 commented Jul 6, 2013

Awesome, thanks so much.

Copy link

ghost commented Jan 5, 2014

Awesome, thanks so much too.

@garbagetown
Copy link

thanks!!

@dannyman
Copy link

dannyman commented Apr 7, 2014

Yeah, so, how to make this idempotent, as they say?

@jerm
Copy link

jerm commented Jun 3, 2014

Check the current zone, register it and compare:

https://gist.github.com/jerm/fc7f33f6a6d6534f6fde

@komita1981
Copy link

That's what I need 👍

@marcelotheodoro
Copy link

Just what I needed. Thanks man

@jeremyheiler
Copy link

Thank you!

@AlexeyKhristov
Copy link

thanks a lot :)

@vovansystems
Copy link

thanks! but I have slightly modified your code to avoid of getting 'changed' status every run.
i've used static file with timezone copy: src=timezone instead of copy: content='America/Los_Angeles'

@dsociative
Copy link

Just use \n,

- name: Set timezone variables
  copy: content='Europe/Moscow\n'
        dest=/etc/timezone
        owner=root
        group=root
        mode=0644
        backup=yes
TASK: [test | Set timezone variables] ************************************** 
ok: [0.1.2.3]

@jeffwidman
Copy link

For checking current timezone and making this idempotent, just use the ansible_date_time.tz built-in fact:

- name: Set timezone to UTC
  when: ansible_date_time.tz != 'UTC'
  command: timedatectl set-timezone UTC

@tetherit
Copy link

Doesn't work on Ubuntu 16.04 LTS (Xenial Xerus) :( - any workaround?

EDIT: This works: https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806

@chusiang
Copy link

chusiang commented Jul 21, 2016

This working on Ubuntu 14.04 (managed node), thank you!

@thbar
Copy link

thbar commented Dec 13, 2016

@xanview this ansible galaxy role worked for me today on Xenial https://galaxy.ansible.com/adriagalin/timezone/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment