# How compare two file in ansible

**URL:** <https://forum.ansible.com/t/how-compare-two-file-in-ansible/30937>\
**Category:** Ansible Project\
**Created:** [August 2, 2019, 9:52am UTC](https://forum.ansible.com/t/how-compare-two-file-in-ansible/30937 "2019-08-02T09:52:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [August 2, 2019, 9:52am UTC](https://forum.ansible.com/t/how-compare-two-file-in-ansible/30937/1 "2019-08-02T09:52:07Z")

</div>

Hey,

I want compare two file in ansible.

Exemple :

I have one file : /home/ansible/repertory\_1/file\_1.txt  
I have second file with the same file : /home/ansible/repertory\_2/file\_1.txt

I want that if my file file\_1 who is in repertory\_1 is the same content that my file (repertory\_1) ansible does not do anything.

And if my file file\_1 who is in repertory\_1 is different content that my file file\_1 in repertory\_2 , ansible start apache.

Exemple for task who not works :

- name: “Copie du fichier dans test\_d”  
copy:  
src: /home/ansible/repertory\_1/file\_1.txt  
dest: /home/ansible/repertory\_2/file\_1.txt  
delegate\_to: localhost

- set\_fact:  
myfile: “{{ lookup(‘file’, ‘/home/ansible/repertory\_1/file\_1.txt’) }}”

- name: “Start Apache”  
command: sudo systemctl start apache2

when: myfile != /fome/ansible/repertory\_2/file\_1

I think that this tasks not works because myfile content “\n” at each line break

Someone have any idea please for my problem ?!!!

Thank you very much community ansible !!! 🙂

Regards,

Karther

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [August 2, 2019, 10:39am UTC](https://forum.ansible.com/t/how-compare-two-file-in-ansible/30937/2 "2019-08-02T10:39:09Z")

</div>

You may want to use `stat` module. Get checksum of files and perform action based upon that -

```auto
---
- hosts: localhost
tasks:
- stat:
path: "./a"
register: a_stat

- stat:
path: "./b"
register: b_stat

- debug:
msg: "Files are same, perform something when they are same"
when: a_stat.stat.checksum == b_stat.stat.checksum

```

---

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [August 2, 2019, 12:03pm UTC](https://forum.ansible.com/t/how-compare-two-file-in-ansible/30937/3 "2019-08-02T12:03:27Z")

</div>

hey M.Kasurde,

I see this module “stat”, it’s very awesome !!! 😉

It’s working perfectly, you are very best M.Kasurde !!! ^^

I hope that you will work in Microsoft and you are DSI or CIO why not ?!! haha 🙂

Thank you very much !!! 😉

Regards,

Karther
