Sample Header Ad - 728x90

ansible comparing all files in 2 directories and printing the difference

1 vote
1 answer
2924 views
I have 2 directories $ tree dir{1..2} dir1 ├── file1 └── file2 dir2 ├── file1 └── file2 I want to compare all files in dir1 with all files in dir2 using ansible and print differences like this output: ${file1} from ${dir1}: diff content ${file1} from ${dir2}: diff content and it will loop through all files to print their differences below is ansible snippet that needs modification --- - name: Compare files in two directories hosts: localhost gather_facts: false tasks: - name: Find files in directory 1 find: paths: ~/dir1 file_type: file register: dir1_files - name: Find files in directory 2 find: paths: ~/dir2 file_type: file register: dir2_files - name: Compare files shell: diff dir1/file1 dir2/file1 ## how can I make sure path changes but filenames stay same using variables loop: "{{ dir1_files.files }}" register: diff_output changed_when: false failed_when: diff_output.rc == 1 - name: Print differences debug: msg: | {{ item.item.path }} from dir1: {{ item.stdout }} {{ item.item.path }} from dir2: {{ item.stdout }} loop: "{{ diff_output.results }}" when: item.stdout_lines | length > 0 For suggested code in Vladimir's answer, I get below output TASK [debug] ***************************************************************************************************************************************** ok: [localhost] => { "msg": "file2 from dir1: \n 1,2c1,2\n abc101\n > def111\nfile2 from dir2: \n 1,2c1,2\n abc101\n > def111\nfile1 from dir1: \n 1,2c1,2\n 101abc\n > 111def\nfile1 from dir2: \n 1,2c1,2\n 101abc\n > 111def\n" }
Asked by Sollosa (1993 rep)
May 8, 2023, 08:32 AM
Last activity: May 9, 2023, 07:05 AM