我正在编写一个返回 cronjob 语法的宏,如下所示:
{%- macro passive_check(state, service) -%}
{%- set state_checks = salt['monitoring.discover_checks_passive'](state) %}
{% for host in pillar['shinken_pollers'] %}
*/{{ state_checks[service]['passive_interval'] }} * * * * nagios output=$({{ state_checks[service]['check_command'] }}); return_code=$?; printf "%s\t%s\t%s\t%s\n" "{{ grains['id'] }}" "{{ service }}" "$return_code" "$output" | /usr/local/nagios/bin/py_send_nsca -H {{ host }} -c /etc/send_nsca.conf
{%- endfor -%}
{%- endmacro %}
然后在一个.sls
文件中它被称为:
{% from 'nrpe/passive.sls' import passive_check with context %}
{%- for state in pillar['monitoring']['states'] -%}
{%- for name in salt['monitoring.discover_checks_passive'](state) %}
/etc/cron.d/passive-checks:
file:
- append
- text: |
{{ passive_check(state, name)|safe }}
{%- endfor -%}
{%- endfor %}
但运行时出现以下错误:
Rendering SLS rsyslog.nrpe failed, render error: while scanning an alias
in "<unicode string>", line 29, column 1:
*/5 * * * * nagios output=$(/usr ...
^
expected alphabetic or numeric character, but found '/'
in "<unicode string>", line 29, column 2:
*/5 * * * * nagios output=$(/usr/ ...
^
手动转义|e
也返回相同的错误。
所以问题是:如何在 Jinja2 宏中转义这些字符:*、/、...?