当前位置:   article > 正文

关于playbook中when条件过滤报The conditional check ‘result|failed‘ failed的问题

关于playbook中when条件过滤报The conditional check ‘result|failed‘ failed的问题

问题现象

在使用plabook中的when做过滤脚本如下:

  1. ---
  2. - hosts: realservers
  3. remote_user: root
  4. tasks:
  5. - name: Check if httpd service is running
  6. command: systemctl status httpd
  7. register: result
  8. ignore_errors: True
  9. - name: Handle failed service check
  10. debug:
  11. msg: "The service is not running."
  12. when: result | failed
  13. - name: Handle successful service check
  14. debug:
  15. msg: "The service is running."
  16. 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任务

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/235221
推荐阅读
相关标签
  

闽ICP备14008679号