我正在尝试一个简单的任务,找出两个文件之间的区别并将其存储在记事本中。我不能用命令和shell来做。请告诉我出错的地方 -
---
- hosts: myserver
tasks:
- name: get the difference
command: diff hosts.new hosts.mod
register: diff
- debug: var=diff.cmd
错误 -
fatal: [zlp12037]: FAILED! => {"changed": true, "cmd": ["diff", "hosts.new", "hosts.mod"], "delta": "0:00:00.003102", "end": "2017-03-29 10:17:34.448063", "failed": true, "rc": 1, "start": "2017-03-29 10:17:34.444961", "stderr": "", "stdout":
答案 0 :(得分:6)
我不太确定您的输入效果与格式相符。但以下应该是一个解决方案:
- name: "Get difference from two files"
command: diff filea fileb
args:
chdir: "/home/user/"
failed_when: "diff.rc > 1"
register: diff
- name: debug output
debug: msg="{{ diff.stdout }}"
一些解释:
答案 1 :(得分:1)
我会将hosts.new或hosts.mod移动到ansible控制机器。
使用src作为hosts.new运行复制模块,使用--check和--diff运行dest作为hosts.mod。我发现这种方法对于发现大型企业中文件的差异非常有用。
执行命令
ansible all -m copy -a "src=hosts.new dest=/tmp/hosts.mod" --check --diff -i hosts
输出:
--- before: /tmp/hosts.mod
+++ after: /home/ansible/hosts.new
@@ -1,5 +1,5 @@
host1
+host2
host3
host4
-host6
-host99
+host5
test10 | SUCCESS => {
"changed": true,
"failed": false
}