How to check if two files are identical on the same host with ansible ?

Antoine asked:

I use Ansible to manage nodes, and I want to check timezone on my nodes.

I find that on centos, I should replace /etc/localtime by according /usr/share/zoneinfo/{{ timezone }} file.

I write thi task in my ansible playbook :

- name: sync timezone file if different
  command: "rsync --itemize-changes --checksum --copy-links /usr/share/zoneinfo/{{ timezone }} /etc/localtime"

which works.

My goal is to perform action only if it’s necessary, so with this, localtime will be write only if something’s changed.

But my problem is that I also want to use Ansible to check any changed on my nodes. And when ansible perform a command, it always marked in report “changed=1”, even if there is no change.

I try to use a file modules of ansible, but whether it’s to copy remote to local or local to remote, but not remote to remote.

I also try to use a check with register option with this task :

- name: Copy timezone {{ timezone }} to /etc/localtime
  shell: "[[ $(md5sum /usr/share/zoneinfo/Europe/Paris | cut -d' ' -f1) = $(md5sum /etc/localtime | cut -d' ' -f1) ]]"
  register: timezone_check

But that’s the same problem, command is always execute, so changed is incremented.

Any idea how I could perform this ?

My answer:


Why don’t you just create the symlink directly?

- name: Set timezone
  file: src=/usr/share/zoneinfo/{{ timezone }} dest=/etc/localtime state=link

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.