ansibleyamljinja2ansible-awxansible-tower

How do you compare values in two Ansible lists?


I've got two lists in Ansible that I build on the fly using the find module

- name: "Find all files in {{ backup_path }}"
  ansible.builtin.find:
    paths:
      - "{{ backup_path }}"
    get_checksum: true
    recurse: yes
    depth: 2
    patterns: "{{ file_patterns }}"
  register: backup_files

- name: "Find all files in {{ new_path }}"
  ansible.builtin.find:
    paths:
      - "{{ new_path }}"
    get_checksum: true
    recurse: yes
    depth: 2
    patterns: "{{ file_patterns }}"
  register: new_files

Once I register those variables, I want to run a diff between the old_files and new_files lists.

- name: diff files between new and old
  ansible.builtin.shell: 
    cmd: "diff {{ item.0 }} {{ item.1 }}"
  register: diff_files
  loop: 
    - "{{ backup_files }}"
    - "{{ new_files }}"

I know the above snippet is wrong, but I want to compare the 0th item from new_files to 0th item from old_files n number of times.

The purpose of this playbook is to compare all the files in two directories and replace those that are different.


Solution

  • Turns out I was making this far more complicated than it needed to be, here is what worked that was much simpler. Basically, use diff -rq \path1 \path2.

    - name: diff conf files between new and old
      ansible.builtin.shell: 
        cmd: "diff -rq {{ new_path }}conf {{ backup_path }}conf"
      register: diff_conf_files
      failed_when: diff_conf_files.rc not in [0,1]
      ignore_errors: yes
    
    - name: diff bin files between new and old
      ansible.builtin.shell: 
        cmd: "diff -rq {{ new_path }}bin {{ backup_path }}bin"
      register: diff_bin_files
      failed_when: diff_bin_files.rc not in [0,1]
      ignore_errors: yes