Execute copy and set only if something changed

Nico Schlömer asked:

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.

My answer:


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

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.