问题原文
原帖链接 · Mikhail T. · CC BY-SA 3.0
Though Ansible itself has a way for triggering a custom error, I can not find anything similar for Jinja.
My current method uses a syntax error:
{% 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 %}
but those are rendered poorly at run-time -- one needs to look inside the template-file and search for the error (the rendering does not even include the line-number!).
Is there a way to output a neat failure message from inside Jinja-template?
已采纳解答
原帖链接 · Konstantin Suvorov · CC BY-SA 3.0
Answer from the comment:
There is no easy way to accomplish this – only via custom plugin. For details see: https://stackoverflow.com/questions/21778252/how-to-raise-an-exception-in-a-jinja2-macro
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
