简体   繁体   English

使用 Ansible 比较两个文件

[英]Compare two files with Ansible

I am struggling to find out how to compare two files.我正在努力找出如何比较两个文件。 Tried several methods including this one which errors out with:尝试了几种方法,包括这个错误的方法:

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."} => {"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?这是比较两个文件并确保内容相同的最佳做法还是有更好的方法?

Thanks in advance.提前致谢。

My playbook:我的剧本:

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

Why not using stat to compare the two files?为什么不使用stat来比较两个文件? 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

your "diff" task is missing the shell keyword, Ansible thinks you want to use the diff module instead.您的“diff”任务缺少shell关键字,Ansible 认为您想改用diff模块。

also i think diff (as name of the variable to register the tasks result) leads ansible to confusion, change to diff_result or something.我还认为diff (作为注册任务结果的变量名称)会导致混淆,更改为diff_result或其他什么。

code (example):代码(示例):

  tasks:
  - local_action: shell diff /etc/hosts /etc/fstab
    failed_when: "diff_output.rc > 1"
    register: diff_output

  - debug:
      var: diff_output

hope it helps希望能帮助到你

A slightly shortened version of 'imjoseangel' answer which avoids setting facts: 'imjoseangel' 答案的略微缩短版本,可避免设定事实:

  vars:
    file_1: cats.txt
    file_2: dogs.txt

  tasks:
  - name: register the first file
    stat:
      path: "{{ file_1 }}"
      checksum: sha1
      get_checksum: yes
    register: file_1_checksum

  - name: register the second file
    stat:
      path: "{{ file_2 }}"
      checksum: sha1
      get_checksum: yes
    register: file_2_checksum

  - name: Check if the files are the same
    debug: msg="The {{ file_1 }} and {{ file_2 }} are identical"
    failed_when: file_1_checksum.stat.checksum != file_2_checksum.stat.checksum
    ignore_errors: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM