如何在 ansible 中比较两个主机配置

问题描述 投票:0回答:2

我想看看远程主机上的两个文件是否有什么不同。

我的任务:

- name: compare files
  shell: diff -u -bB <(grep -vE '^@\s*(@#|$)' /tmp/{{myloopfile_list | basename }})  <(grep -vE '^@\s*(@#|$)' {{myloopfile_list}}) | awk '{if ($1 ~ /^-|+/ ) print $0;}'
  failed_when: "diff_output.rc > 1"
  register: diff_output

输出:

    "stderr": "/bin/sh: -c: line 0: syntax error near unexpected token `('\n/bin/sh: -c: line 0: `diff -u -bB <(grep -vE '^@\\s*(@#|$)' /tmp/nginx.conf)  <(grep -vE '^@\\s*(@#|$)' /home/local/nginx.conf) | awk '{if ($1 ~ /^-|+/ ) print $0;}''",
    "stderr_lines": [
        "/bin/sh: -c: line 0: syntax error near unexpected token `('",
        "/bin/sh: -c: line 0: `diff -u -bB <(grep -vE '^@\\s*(@#|$)' /tmp/nginx.conf)  <(grep -vE '^@\\s*(@#|$)' /home/local/nginx.conf) | awk '{if ($1 ~ /^-|+/ ) print $0;}''"
    ],
    "stdout": "",
    "stdout_lines": []
}

}

ansible diff
2个回答
0
投票

你只想比较2个文件,我建议你计算并比较校验和:

  tasks:
    - command: sha256sum file1.txt file2.txt
      register: cksum
    
    - assert:
        that: cksum.stdout_lines[0].split('  ')[0] == cksum.stdout_lines[1].split('  ')[0]

0
投票

如果您想了解实际的区别并根据您提供的描述,可以采用一种方法

- hosts: test.example.com
  become: no
  gather_facts: no

  tasks:

  - name: Only show what us the difference between
    shell:
      cmd: "diff -u -bB /tmp/{{ provided_config }} /home/{{ ansible_user }}/{{ actual_config }}"
    failed_when: difference.rc > 1
    changed_when: difference.rc == 1
    register: difference

  - name: Show difference
    debug:
      var: difference.stdout
© www.soinside.com 2019 - 2024. All rights reserved.