Comparing two files registering the difference

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

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
        Racke

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

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