Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing files using Ansible

Tags:

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.

like image 657
AbhinavK Avatar asked Jan 23 '26 07:01

AbhinavK


1 Answers

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
    
like image 114
gmoon Avatar answered Jan 26 '26 11:01

gmoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!