How compare two file in ansible

56 views
Skip to first unread message

Karther

unread,
Aug 2, 2019, 5:52:07 AM8/2/19
to Ansible Project
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


Abhijeet Kasurde

unread,
Aug 2, 2019, 6:39:31 AM8/2/19
to ansible...@googlegroups.com
You may want to use `stat` module. Get checksum of files and perform action based upon that - 
```
---
- 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
```


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4ed027c2-bd90-4359-acca-e0960863a895%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Karther

unread,
Aug 2, 2019, 8:03:27 AM8/2/19
to Ansible Project
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
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.


--
Thanks,
Abhijeet Kasurde
Reply all
Reply to author
Forward
0 new messages