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 '/'は... 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

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 に答える