我正在编写一个返回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宏中?
发布于 2014-01-16 16:56:51
您可能会感到惊讶,但不需要转义这些角色。
导致错误expected alphabetic or numeric character, but found '/'
的罪魁祸首是..。白空间控制。请注意宏:
{%- 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
如果没有减号(-
)在set
和for
块的末尾,它将呈现为如下所示:
/etc/cron.d/passive-checks:
file:
- append
- text: |
*/5 * * * * nagios output=$/usr/lib/nagios/plugins/check_procs -c 1:1 -C rsyslogd -u syslog; return_code=$?; printf "%s\t%s\t%s\t%s\n" "q-mail" "rsyslogd_proc
s" "$return_code" "$output" | /usr/local/nagios/bin/py_send_nsca -H host1 -c /etc/send_nsca.conf
*/5 * * * * nagios output=$/usr/lib/nagios/plugins/check_procs -c 1:1 -C rsyslogd -u syslog; return_code=$?; printf "%s\t%s\t%s\t%s\n" "q-mail" "rsyslogd_proc
s" "$return_code" "$output" | /usr/local/nagios/bin/py_send_nsca -H host2 -c /etc/send_nsca.conf
因此,您必须删除空白行:
{%- 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 %}
(请注意,为了使它在crontab中工作,我必须转义百分比符号)
并在调用file.append
状态时放置缩进:
{%- 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)|indent(8) }}
- require:
- file: touch_cron
{%- endfor -%}
{%- endfor %}
https://stackoverflow.com/questions/21153986
复制相似问题