Mik*_* T. 5 template custom-errors ansible jinja
尽管 Ansible 本身有触发自定义错误的方法,但我找不到类似 Jinja 的任何内容。
我当前的方法使用语法错误:
{% if 'ansible_mounts' in hostvars[host] %}
# {{ host }} knows its mount-points
{% else %}
# {% error!! No ansible_mounts listed for host - fact-gathering must've failed %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
但是那些在运行时渲染得很差——需要查看模板文件内部并搜索错误(渲染甚至不包括行号!)。
有没有办法从 Jinja 模板内部输出整洁的失败消息?
Ansible 将强制过滤器添加到 Jinja,可用于执行以下操作:
{{ ('OK text' if condition_ok) | mandatory('Text of error message') }}
Run Code Online (Sandbox Code Playgroud)
给出失败:
fatal: [hostname]: FAILED! => {"msg": "Text of error message"}
Run Code Online (Sandbox Code Playgroud)
(替换condition_ok
为您需要进行的检查;如果没有任何内容可以替换,则'OK text'
可以是 just 。)''
归档时间: |
|
查看次数: |
5199 次 |
最近记录: |