【发布时间】:2018-12-17 16:52:41
【问题描述】:
我想看看本地和远程主机上的文件是否有任何更改。如果有任何差异,它应该在屏幕上可见 使用 Ansible 的最佳方法应该是什么
例如:
src : /tmp/abc.txt
dest : hostname:/tmp/cde.txt
【问题讨论】:
标签: ansible
我想看看本地和远程主机上的文件是否有任何更改。如果有任何差异,它应该在屏幕上可见 使用 Ansible 的最佳方法应该是什么
例如:
src : /tmp/abc.txt
dest : hostname:/tmp/cde.txt
【问题讨论】:
标签: ansible
您还可以使用check_mode: yes 和diff: 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
例子:
# 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
更多关于check_mode和diffhere的信息。
【讨论】:
copy 从 ansible 机器复制文件到远程机器
从命令行,
ansible <host-pattern> -m copy -CD -a "src=<your local file> dest=<remote file or location>"
-m copy 选项使 Ansible 调用 copy 模块
-C 选项使 Ansible 检查是否会发生更改,而不是执行复制
-D 选项使 Ansible 报告如果要进行复制会发生哪些更改
输出类似于 UNIX diff 命令产生的内容,只是它报告本地文件和远程副本之间的差异。
【讨论】:
---
- hosts: localhost
gather_facts: yes
become: yes
become_method: sudo
tasks:
- name: Get ansible date/time facts
setup:
filter: "ansible_date_time"
gather_subset: "!all"
- name: Store DTG as fact
set_fact:
DTG: "{{ ansible_date_time.date }}"
- name: Changes of the USCORE_Switch
copy:
src: /home/thilan_widearea_cloud/network-programmability/curentconfig/backups/{{hostvars.localhost.DTG}}/USNYCL3SW-{{hostvars.localhost.DTG}}-config.txt
dest: /home/thilan_widearea_cloud/network-programmability/curentconfig/USBaseconfig.txt
check_mode: yes
diff: yes
register: output
【讨论】: