ansible - 如何在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 列,但可能在文件中的其他位置,具体取决于确切的语法问题。
违规行似乎是:
- 名称:仅当文件没有更改时才阻止运行 ^ 此处
解决方案
您应该针对主机和任务运行。在最后一部分中,您没有指定任何主机和任务部分。
- 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
推荐阅读
- postgresql - 在一个非常简单的查询中将 HAVING 与 WHERE 结合使用时遇到问题
- image - 使用批处理从文件夹中打开一个随机文件
- c++ - 与更改此变量的函数在同一行中打印参考变量
- python - 如何更改字符串列表的仅部分(切片)的值
- c - 循环 #3 中的 printf() 函数给出了意想不到的结果
- python - 随机森林中重要特征的统计意义?
- ios - 检查目录中是否存在 JSON 文件 - Swift
- web2py - 检索用户登录电子邮件并使用它来预填充提交表单
- angular - 角度 5:如何将数据导出到 csv 文件
- javascript - 从 JavaScript 禁用悬停规则