1

我正在编写一个返回 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 宏中转义这些字符:*、/、...?

4

1 回答 1

3

您可能会感到惊讶,但无需逃避这些字符。

导致错误的罪魁祸首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

如果在and块-的末尾没有减号 ( ) ,它将被呈现为:setfor

/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 %}
于 2014-01-16T16:56:51.673 回答