首页 > 解决方案 > 如何在ansible playbook中比较两个不同主机的输出?

问题描述

- hosts: remote_1
  tasks:
    - name: first file check
      stat:
        path: /root/abc.txt
        #get_checksum: yes
        checksum_algorithm: sha256
      register: test_file_check_1

    - debug:
        var: test_file_check_1.stat.checksum
      when: test_file_check_1.stat.exists

- hosts: remote_2
  tasks: 
    - name: next check
      stat:
        path: /root/abc.txt
        #get_checksum: yes
        checksum_algorithm: sha256
      register: test_file_check_2

    - debug: 
        var: test_file_check_2.stat.checksum
      when: test_file_check_2.stat.exists

- name: Block run only if file has no changes
  command: /bin/true
  when: test_file_check_1.stat.checksum != test_file_check_2.stat.checksum

上面的代码给出了最后一个块的错误。错误!'when' 不是 Play 的有效属性

该错误似乎出现在“/root/two_file_stat.yml”中:第 27 行,第 3 列,但可能在文件中的其他位置,具体取决于确切的语法问题。

违规行似乎是:

标签: ansible

解决方案


您应该针对主机任务运行。在最后一部分中,您没有指定任何主机任务部分。

- name: Block run only if file has no changes
  hosts : localhost
  tasks:
  - command: /bin/true
    when: test_file_check_1.stat.checksum != test_file_check_2.stat.checksum

推荐阅读