Ansible:通过Ansible使用Diff命令 [英] Ansible: Use of Diff command using Ansible

查看:147
本文介绍了Ansible:通过Ansible使用Diff命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一项简单的任务,即找出两个文件之间的差异并将其存储在记事本中.我不能用命令以及外壳来做到这一点.请建议我要去哪里错了-

I am trying to one simple task which is find out the difference between the two files and store it in notepad. I am not able to do it with command as well as shell. Please suggest where i am going wrong-

---
- 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":

推荐答案

我不太确定您的输入格式如何.但是以下应该是一个解决方案:

I'm not quite sure what your input play looks like with your formatting. But the following should be a solution:

- 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 }}"

一些解释:

  • 如果diff命令失败,则返回代码为>1.我们通过"failed_when"进行评估.
  • 要获取命令的输出,我们打印".stdout"元素.
  • 为确保我们位于文件所在的文件夹中,我们使用"chdir".

这篇关于Ansible:通过Ansible使用Diff命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆