# Comparing files

**URL:** <https://forum.ansible.com/t/comparing-files/12969>\
**Category:** Ansible Project\
**Created:** [June 16, 2013, 3:24am UTC](https://forum.ansible.com/t/comparing-files/12969 "2013-06-16T03:24:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![celine](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@celine](https://forum.ansible.com/u/celine)\
**Post date:** [June 16, 2013, 3:24am UTC](https://forum.ansible.com/t/comparing-files/12969/1 "2013-06-16T03:24:27Z")

</div>

Hi, I wish to compare the file in client machine with the file in server machine. I wish to know if the contents are the same. How do I do it?

tasks:

- name: Compare 2 files

action:

How do I input my action statement?

Many thanks.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 17, 2013, 4:02am UTC](https://forum.ansible.com/t/comparing-files/12969/2 "2013-06-17T04:02:07Z")

</div>

you can use copy: to compare if both files are differnt, using --check to not overrite.  
and --diff will do an actual diff.

---

<div class="post-metadata">

**Author:** ![celine](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@celine](https://forum.ansible.com/u/celine)\
**Post date:** [June 17, 2013, 7:02am UTC](https://forum.ansible.com/t/comparing-files/12969/3 "2013-06-17T07:02:01Z")

</div>

Hi Brian,

Thank you for your reply. I am new to ansible so i am sorry if the question i am asking in of low level.

I wish to compare file new1 on client computer with file newfile on server computer. I tried this:

tasks:

- name: Compare 2 files  
action: copy --check src=/home/client/Desktop/new1 dest=/home/server/Desktop/newfile  
.

I got this error: msg: this module requires key=value arguments.

How can i know which key or value it requires?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Kavin\_Kankeshwar](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kavin\_Kankeshwar](https://forum.ansible.com/u/Kavin_Kankeshwar)\
**Post date:** [June 17, 2013, 7:39am UTC](https://forum.ansible.com/t/comparing-files/12969/4 "2013-06-17T07:39:48Z")

</div>

As far as i know, --check should be passed when running the playbook, Not within the playbook.

e.g. from Doc site.

```auto

ansible-playbook foo.yml --check

```

[http://www.ansibleworks.com/docs/playbooks2.html#check-mode-dry-run-check](http://www.ansibleworks.com/docs/playbooks2.html#check-mode-dry-run-check)

Regards,

---

<div class="post-metadata">

**Author:** ![celine](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@celine](https://forum.ansible.com/u/celine)\
**Post date:** [June 17, 2013, 8:18am UTC](https://forum.ansible.com/t/comparing-files/12969/5 "2013-06-17T08:18:37Z")

</div>

Yes, i saw this too. But how does Ansbile knows which file i want it to compare to? My objective is to compare a file on client with another file on the server.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [June 17, 2013, 10:33am UTC](https://forum.ansible.com/t/comparing-files/12969/6 "2013-06-17T10:33:07Z")

</div>

> Yes, i saw this too. But how does Ansbile knows which file i want it to  
> compare to? My objective is to compare a file on client with another file  
> on the server.

I don't think there's a module that will do that for you. I suggest you  
look at the `fetch' module with which you can retrieve a file, then do  
the comparison manually using `command' module and md5sum program.  
Alternatively, you can `copy' the file to the remote and do the  
checksumming there. Either way, I think you've have to transfer the  
file.

Check the documentation of the modules (ansible.cc/docs/modules) which  
detail all the options or use the `ansible-doc' command on an individual  
module to find supported options.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [June 17, 2013, 12:04pm UTC](https://forum.ansible.com/t/comparing-files/12969/7 "2013-06-17T12:04:05Z")

</div>

Create a Playbook with copy task:

> * * *
> 
> - name: test  
> hosts: all  
> sudo: True  
> tasks:
> - name: whatever  
> action: copy src=/var/repository/smb.j2 dest=/etc/samba/smb.conf

Run Playbook with --check and --diff:

> ansible-playbook diff.yml -kK -u admin --limit=HostA --check --diff

This will not actually copy your file to remote host, but will do diff comparison between those files

Edgars

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 17, 2013, 12:22pm UTC](https://forum.ansible.com/t/comparing-files/12969/8 "2013-06-17T12:22:06Z")

</div>

for copy and template modules, src= is on the machine you execute ansible from, dest= is on the target machine, that is how you can get them to compare (using --check and --diff options on the command line).

---

<div class="post-metadata">

**Author:** ![celine](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@celine](https://forum.ansible.com/u/celine)\
**Post date:** [June 20, 2013, 1:30am UTC](https://forum.ansible.com/t/comparing-files/12969/9 "2013-06-20T01:30:35Z")

</div>

Thanks everyone for your help 🙂
