如何在Jinja2宏中转义特殊字符?

时间:2014-01-16 05:26:59

标签: escaping jinja2 salt-stack

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

1 个答案:

答案 0 :(得分:2)

你可能会感到惊讶,但不需要逃避这些角色。

导致错误expected alphabetic or numeric character, but found '/'的罪魁祸首是...... whitespace control。注意宏:

{%- 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 %}