Skip to content

Instantly share code, notes, and snippets.

@jerm
Last active August 2, 2021 19:59
Show Gist options
  • Save jerm/fc7f33f6a6d6534f6fde to your computer and use it in GitHub Desktop.
Save jerm/fc7f33f6a6d6534f6fde to your computer and use it in GitHub Desktop.
---
- name: Group by Distribution
hosts: all
tasks:
- group_by: key={{ansible_distribution}}
- name: Set Time Zone
hosts: Ubuntu
gather_facts: False
vars:
- my_zone: 'America/New_York'
tasks:
- name: check current timezone
shell: cat /etc/timezone
register: current_zone
- name: Set timezone variables
copy: content={{my_zone}}
dest=/etc/timezone
owner=root
group=root
mode=0644
backup=yes
when: current_zone.stdout != my_zone
notify:
- update timezone
handlers:
- name: update timezone
command: dpkg-reconfigure --frontend noninteractive tzdata
@brayrobert201
Copy link

Thanks for those two comments. I've found them useful for my setup.

@rPawel
Copy link

rPawel commented Apr 6, 2015

"check current timezone" needs

changed_when: current_zone.stdout != "{{my_zone}}"

to stop nagging user about changed status!

@HeikoBornholdt
Copy link

When running ansible with --check it will cause this error:

fatal: [test] => error while evaluating conditional: current_zone.stdout != timezone

Any idea to solve this problem?

@HeikoBornholdt
Copy link

I found the solution: always_run: yes

@Seegras
Copy link

Seegras commented May 10, 2017

Doesn't work. The ansible part works, but "dpkg-reconfigure --frontend noninteractive tzdata" will reset it to what it was before.

I added this task here

  • name: ensure correct timezone link is set
    file:
    src: '/usr/share/zoneinfo/{{my_zone}}'
    dest: /etc/localtime
    owner: root
    group: root
    state: link

After that, the handler will do the right thing.

@jasenmichael
Copy link

here is my solution

  • name: Check current timezone
    shell: cat /etc/timezone
    register: current_timezone
    changed_when: false

  • name: Set Timezone
    timezone:
    name: America/Chicago
    when: current_timezone.stdout != "America/Chicago"

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