[英]how to compare two hosts config in ansible
我想看看遠程主機上的兩個文件是否有任何區別。
我的任務:
- 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": []
}
}
你只想比較 2 個文件,我建議你計算和比較校驗和:
tasks:
- command: sha256sum file1.txt file2.txt
register: cksum
- assert:
that: cksum.stdout_lines[0].split(' ')[0] == cksum.stdout_lines[1].split(' ')[0]
如果您想查看實際差異並根據您提供的描述,可以采用一種方法
- hosts: test.example.com
become: no
gather_facts: no
tasks:
- name: Only show difference between
shell:
cmd: "diff -u -bB /tmp/{{ provided_config }} /home/{{ ansible_user }}/{{ actual_config }}"
failed_when: "difference.rc > 1"
register: difference
- name: Show difference
debug:
var: difference.stdout
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.