Ansible:使用 Ansible 使用 Diff 命令

标签 ansible

我正在尝试一项简单的任务,即找出两个文件之间的差异并将其存储在记事本中。我无法使用命令和 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":

最佳答案

我不太确定你的输入格式和你的格式是什么样的。但以下应该是一个解决方案:

- 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 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43096404/

    相关文章:

    ansible - 如何仅在 *.web 文件存在时删除它们

    vagrant - 使用 Vagrant 和 Ansible 在本地主机上运行任务

    ansible - 有什么方法可以让 Ansible 完全按照我给出的命令执行命令吗?

    ubuntu - 如何使用 ansible playbook 重启 ubuntu 服务器?

    linux - Ansible azure 模块 azure.azcollection.azure_rm_adgroup_info 的 Azure AD 应用程序权限

    nginx - 在 http {} 内的 nginx 配置文件 ansible 中插入行

    ansible - 我可以在 Ansible 模板中使用主机变量吗?

    ansible - ansible apt 中的 state=installed 是什么意思?

    ansible - 如何实现 sudo su - <user> 并在ansible中运行所有命令

    如果 VM 未标记,Azure Ansible 插件无法生成 list