9

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.

Scott Pack
  • 14,717
  • 10
  • 51
  • 83
Nico Schlömer
  • 205
  • 1
  • 2
  • 7
  • (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 08:05

4 Answers4

14

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
Henrik Pingel
  • 8,676
  • 2
  • 24
  • 38
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
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
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
Mars
  • 1,611
  • 1
  • 9
  • 2
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.

Ashley
  • 528
  • 1
  • 6
  • 14