问题现象
在使用plabook中的when做过滤脚本如下:
---
- hosts: realservers
remote_user: root
tasks:
- name: Check if httpd service is running
command: systemctl status httpd
register: result
ignore_errors: True
- name: Handle failed service check
debug:
msg: "The service is not running."
when: result | failed
- name: Handle successful service check
debug:
msg: "The service is running."
when: result | success
使用when: result | success这种过滤条件发现报错如下:
"msg": "The conditional check 'result|failed' failed. The error was: template error while templating string: no filter named 'failed'. String: {% if result|failed %} True {% else %} False {% endif %}\n\nThe error appears to be in '/root/when_judge.yml': line 11, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Handle failed service check\n ^ here\n"
解决方案
后经过google发现官方已经不使用此种表达方式做过滤, 将|更改为is即可。
参考文章:devops - Ansible: Conditional Statement wierd Error - Stack Overflow
测试结果
修改后测试如下:
启动被控端httpd服务后,主控端查看状态变更为success任务
停止被控端httpd服务后,主控端查看状态变更为failed任务