Abhinav.K
Abhinav.K

Reputation: 179

Comparing files using Ansible

I need to find a way where I can compare elasticsearch template folders in given elasticsearch Data host group. Meaning if the directory is /usr/local/elasticsearch/config/templates/, I need to make sure all the files inside that directory in that ansible host group is same.

No extra template files or difference version template files. I haven't been able to figure out how to do this.

Upvotes: 3

Views: 5349

Answers (2)

gmoon
gmoon

Reputation: 810

Try combining ansible with rsync dry-run using the shell module:

ansible -i production data_hosts -l '!~host1' -f 1 -m shell \
  -a 'rsync --checksum --delete --dry-run -r -v host1.example.com:/usr/local/elasticsearch/config/templates/ /usr/local/elasticsearch/config/templates'

Explanation

  • Compares the /usr/local/elasticsearch/config/templates directory on all hosts in the [data_hosts] group to host1
  • Excludes host1.example.com using the -l limit argument: -l '!~host1'
  • Uses -f 1 to only run one compare at a time. Optional, but helped in my case because the directories contained large numbers of files (>10K)
  • Uses --dry-run to prevent rsync from actually sync'ing the directories
  • Uses --delete to list extraneous files in the destination directory
  • Uses --checksum to compare files based on checksum rather than mod-time and size

Notes

  • You could modify this one-liner to perform the sync by removing --dry-run. Consider adding -z to compress file data during transfer, and -a for archive mode.
  • The production inventory file would look like this:

    [data_hosts]
    host1.example.com
    host2.example.com
    host3.example.com
    host4.example.com
    

Upvotes: 1

Abhinav.K
Abhinav.K

Reputation: 179

I did it by first comparing the number of files in all hosts in that given group under the template folder, then getting the list of files and their respective md5sum values and exporting them to current playbook using include_vars. Then I compared each files md5sum against the one exported with include_vars and with_items.

Upvotes: 0

Related Questions