使用Ansible比较两个文件 [英] Compare two files with Ansible

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

问题描述

我正在努力找出如何比较两个文件.尝试了几种方法,其中包括错误的方法:

I am struggling to find out how to compare two files. Tried several methods including this one which errors out with:

失败! => {"msg":在已配置的模块路径中找不到模块差异.此外,核心模块丢失.如果这是一个 签出,运行'git pull --rebase'来解决此问题.}

FAILED! => {"msg": "The module diff was not found in configured module paths. Additionally, core modules are missing. If this is a checkout, run 'git pull --rebase' to correct this problem."}

这是比较两个文件并确保内容相同还是有更好方法的最佳实践?

Is this the best practice to compare two files and ensure the contents are the same or is there a better way?

谢谢.

我的剧本:

- name: Find out if cluster management protocol is in use
      ios_command:
        commands:
          - show running-config | include ^line vty|transport input
      register: showcmpstatus
 - local_action: copy content="{{ showcmpstatus.stdout_lines[0] }}" dest=/poc/files/{{ inventory_hostname }}.result
    - local_action: diff /poc/files/{{ inventory_hostname }}.result /poc/files/transport.results
      failed_when: "diff.rc > 1"
      register: diff
 - name: debug output
      debug: msg="{{ diff.stdout }}"

推荐答案

为什么不使用stat比较两个文件? 只是一个简单的例子:

Why not using stat to compare the two files? Just a simple example:

- name: Get cksum of my First file
  stat:
    path : "/poc/files/{{ inventory_hostname }}.result"
  register: myfirstfile

- name: Current SHA1
  set_fact:
    mf1sha1: "{{ myfirstfile.stat.checksum }}"

- name: Get cksum of my Second File (If needed you can jump this)
  stat:
    path : "/poc/files/transport.results"
  register: mysecondfile

- name: Current SHA1
  set_fact:
    mf2sha1: "{{ mysecondfile.stat.checksum }}"

- name: Compilation Changed
  debug:
    msg: "File Compare"
  failed_when:  mf2sha1 != mf1sha1

这篇关于使用Ansible比较两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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