使用ansible模块的两个文件的区别

Dev*_*ner 6 ansible

我想看看本地和远程主机上的文件是否有任何更改。如果有任何区别,它应该在屏幕上可见,使用 Ansible 执行此操作的最佳方法是什么

例如:

src : /tmp/abc.txt
dest : hostname:/tmp/cde.txt
Run Code Online (Sandbox Code Playgroud)

seb*_*ert 8

您还可以使用check_mode: yesdiff: yes任务选项来显示差异:

---
- hosts: localhost
  gather_facts: no

  tasks:
    - name: "Only show diff between test1.txt & test2.txt" 
      copy:
        src: /tmp/test2.txt
        dest: /tmp/test1.txt
      check_mode: yes
      diff: yes
Run Code Online (Sandbox Code Playgroud)

例子:

# cat /tmp/test1.txt
test1

# cat /tmp/test2.txt
test1
test2

# ansible-playbook diff.yaml
PLAY [localhost] ***********************************************************************************************************************************

TASK [Only show diff between test1.txt & test2.txt] ************************************************************************************************
--- before: /tmp/test1.txt
+++ after: /tmp/test2.txt
@@ -1 +1,2 @@
 test1
+test2

changed: [localhost]

PLAY RECAP *****************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0
Run Code Online (Sandbox Code Playgroud)

更多信息check_mode&diff 在这里


小智 0

这听起来像是您可以使用调试模块完成的任务。使用diff获取两个文件的差异。注册输出并使用 debug 进行显示:

- name: Generate diff
  command: diff  /tmp/abc.txt  /tmp/def.txt
  register: diff_result

- name: Show diff result
  debug:
    var: diff_result
Run Code Online (Sandbox Code Playgroud)