Comparing two files registering the difference

295 views
Skip to first unread message

Sivakumar Saravanamuthu

unread,
May 21, 2020, 3:01:46 AM5/21/20
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
May 21, 2020, 4:32:29 AM5/21/20
to ansible...@googlegroups.com
With the stat module you should be able to establish whether the files have different checksums. What did you try
exactly?

Regards
Racke

>
> Regards,
> Siva
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/469d7a64-a6be-427f-9de9-539ce5ee09f1%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/469d7a64-a6be-427f-9de9-539ce5ee09f1%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Brian Coca

unread,
May 21, 2020, 1:02:19 PM5/21/20
to Ansible Project
If you want to 'see the diff' , just use copy in check and diff mode



--
----------
Brian Coca

Sivakumar Saravanamuthu

unread,
May 21, 2020, 11:11:55 PM5/21/20
to ansible...@googlegroups.com
I resolved it 

 tasks:
  - namecopy hosts for validation file
    copy:
      src./templates/hosts
      dest/tmp/hosts
      ownerroot
      grouproot
      mode'0644'
      backupyes

  - namecompare md5 local copy host file
    stat:
      path/tmp/hosts
      checksum_algorithmmd5
      get_checksumyes
    registeroriginalfile

  - namecompare md5 remote hosts file
    stat:
      path/etc/hosts
      checksum_algorithmmd5
      get_checksumyes
    registerremotefile

  - nameVerify host files md5 value
    debug:
      msg"File Compare hash value of {{ remotefile.stat.checksum }} is same as {{ originalfile.stat.checksum }}."
    failed_whenremotefile.stat.checksum != originalfile.stat.checksum
Thank you.
Regards,
Siva


To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/dc439995-263e-3795-6cf9-1682dfc85be6%40linuxia.de.
Reply all
Reply to author
Forward
0 new messages