Execute copy and set only if something changed

16,750

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
16,750

Related videos on Youtube

Nico Schlömer
Author by

Nico Schlömer

Updated on September 18, 2022

Comments

  • Nico Schlömer
    Nico Schlömer over 1 year

    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.

  • Nico Schlömer
    Nico Schlömer over 8 years
    It appears that timezone.changed is always true since the copy is unconditional.
  • Nico Schlömer
    Nico Schlömer over 8 years
    It appears that timezone.changed is always true since the copy is unconditional.
  • Tom Aac
    Tom Aac over 8 years
    It seems the old issue github.com/ansible/ansible/issues/5715. So you must put line break after Europe/Berlin:
  • Gert van den Berg
    Gert van den Berg over 5 years
    This will always change the timezone and never show "changed"