4

For Ansible, I have a role that sets the time zone and populates the setting the (Ubuntu) base system,

- name: set timezone
  copy: content='Europe/Berlin'
        dest=/etc/timezone
        owner=root
        group=root
        mode=0644
        backup=yes

- name: update timezone
  command: dpkg-reconfigure --frontend noninteractive tzdata

These two commands are executed no matter what. This means that when Ansible is run twice for the same target, one still gets a changed=2 in the result summary,

default                    : ok=41   changed=2    unreachable=0    failed=0

Ideally, everything should be ok in the second run.

While I'm guessing that the update timezone should have some sort of dependency on set timezone, I'm not too sure how to best achieve this.

  • (For the task shown here, the timezone module is probably a good idea to use. Answering with that does not answer the question though...) (On how to suppress unnecessary changes) – Gert van den Berg Aug 27 '18 at 8:05
10

You can do that by using register and when changed.

By using register the result of the copy command is saved into a variable. This variable can then be utilized to create a when conditional in the update timezone task.

Also, make sure to add a line break \n at the end of the timezone content, otherwise Ansible will always perform the copy.

- name: set timezone
  copy: content='Europe/Berlin\n'
        dest=/etc/timezone
        owner=root
        group=root
        mode=0644
        backup=yes
  register: timezone

- name: update timezone
  command: dpkg-reconfigure --frontend noninteractive tzdata
  when: timezone.changed

But you could also solve this problem by creating a handler for the dpkg-reconfigure command as described here :

tasks:
  - name: Set timezone variables
    copy: content='Europe/Berlin\n'
          dest=/etc/timezone
          owner=root
          group=root
          mode=0644
          backup=yes
    notify:
      - update timezone
handlers:
 - name: update timezone
   command: dpkg-reconfigure --frontend noninteractive tzdata
| improve this answer | |
3

You simply need to register a variable for the copy play, then check to see whether it has changed.

For instance:

- name: make a file
  copy: ...
  register: whatever

- name: run a command
  command: ...
  when: whatever.changed
| improve this answer | |
  • It appears that timezone.changed is always true since the copy is unconditional. – Nico Schlömer Sep 7 '15 at 11:38
2

Depending on the Ubuntu version used using systemd features might also be useful:

- name: Set timezone
  command: timedatectl set-timezone Europe/Berlin
  changed_when: false
| improve this answer | |
  • This will always change the timezone and never show "changed" – Gert van den Berg Aug 27 '18 at 8:03
1

To set a timezone with Ansible (>=1.6) on Ubuntu use the locale_gen command.

- name: set timezone
  locale_gen: name=Europe/Berlin state=present

Note: locale_gen is an extras module that currently ships with Ansible. It may be removed in future versions.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.