如何在ansible playbook中比较两个不同主机的输出?

如何在ansible playbook中比较两个不同主机的输出?,ansible,Ansible,上面的代码给出了最后一个块的错误。 错误when'不是一个有效的播放属性 错误似乎出现在“/root/two_file_stat.yml”中:第27行第3列,但可能是 根据确切的语法问题,在文件中的其他位置 令人不快的一行似乎是: 名称:仅当文件未更改时阻止运行 ^这里 您应该与主机和任务运行。在上一部分中,您没有指定任何主机和任务部分 - hosts: remote_1 tasks: - name: first file check stat: pat

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

错误似乎出现在“/root/two_file_stat.yml”中:第27行第3列,但可能是 根据确切的语法问题,在文件中的其他位置

令人不快的一行似乎是:

  • 名称:仅当文件未更改时阻止运行 ^这里

您应该与主机和任务运行。在上一部分中,您没有指定任何主机任务部分

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