# Comparing two files registering the difference

**URL:** <https://forum.ansible.com/t/comparing-two-files-registering-the-difference/32994>\
**Category:** Ansible Project\
**Created:** [May 21, 2020, 7:01am UTC](https://forum.ansible.com/t/comparing-two-files-registering-the-difference/32994 "2020-05-21T07:01:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sivakumar\_Saravanamu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Sivakumar\_Saravanamu](https://forum.ansible.com/u/Sivakumar_Saravanamu)\
**Post date:** [May 21, 2020, 7:01am UTC](https://forum.ansible.com/t/comparing-two-files-registering-the-difference/32994/1 "2020-05-21T07:01:46Z")

</div>

hi

I have requirement to compare local file with remote file. Example I have a copy of standard /etc/hosts file and I need to compare that file with remote host /etc/hosts file to check the difference. If there is difference it should register it and proceed with next task. I tried various option like diff and stat but unable to get expected result.

Is there any way I can execute that.

Regards,  
Siva

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 21, 2020, 8:31am UTC](https://forum.ansible.com/t/comparing-two-files-registering-the-difference/32994/2 "2020-05-21T08:31:54Z")

</div>

> hi
> 
> I have requirement to compare local file with remote file. Example I have a copy of standard /etc/hosts file and I need  
> to compare that file with remote host /etc/hosts file to check the difference. If there is difference it should register  
> it and proceed with next task. I tried various option like diff and stat but unable to get expected result.
> 
> Is there any way I can execute that.

With the stat module you should be able to establish whether the files have different checksums. What did you try  
exactly?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 21, 2020, 5:01pm UTC](https://forum.ansible.com/t/comparing-two-files-registering-the-difference/32994/3 "2020-05-21T17:01:39Z")

</div>

If you want to 'see the diff' , just use copy in check and diff mode

---

<div class="post-metadata">

**Author:** ![Sivakumar\_Saravanamu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Sivakumar\_Saravanamu](https://forum.ansible.com/u/Sivakumar_Saravanamu)\
**Post date:** [May 22, 2020, 3:10am UTC](https://forum.ansible.com/t/comparing-two-files-registering-the-difference/32994/4 "2020-05-22T03:10:57Z")

</div>

I resolved it

tasks:

- name: copy hosts for validation file  
copy:  
src: ./templates/hosts  
dest: /tmp/hosts  
owner: root  
group: root  
mode: ‘0644’  
backup: yes

- name: compare md5 local copy host file  
stat:  
path: /tmp/hosts  
checksum\_algorithm: md5  
get\_checksum: yes  
register: originalfile

- name: compare md5 remote hosts file  
stat:  
path: /etc/hosts  
checksum\_algorithm: md5  
get\_checksum: yes  
register: remotefile

- name: Verify host files md5 value  
debug:  
msg: “File Compare hash value of {{ remotefile.stat.checksum }} is same as {{ originalfile.stat.checksum }}.”  
failed\_when: remotefile.stat.checksum != originalfile.stat.checksum
