本文介绍了使用 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' 来纠正这个问题."}

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

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 比较两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 16:59