LoginSignup
0

More than 3 years have passed since last update.

[jinja2] .j2テンプレートファイルのifはスペースを空けないとエラーになる

Last updated at Posted at 2019-11-14

rundeck用のymlファイルを書いていて、通知のところだけ環境ごとに分けたいなーって思ってこんな感じに設定しました。

{{ rundeck.jobs.is_notification }} に、本番環境ならtrueが、その他ならfalseが返ってきて通知の分を出し分けたいって感じです

- description: '15分毎にhogeを実施'
  name: hoge_job
  nodesSelectedByDefault: true
{% if rundeck.jobs.is_notification %}
  notification:
    onfailure:
      plugin:
        configuration:
          webhook_url: {{ rundeck.jobs.notification.slack_webhook }}
        type: SlackNotification
  notifyAvgDurationThreshold: null
{% endif %}
  schedule:
    month: '*'
    time:
      hour: '*'
      minute: '*/15'
      seconds: '0'
    weekday:
      day: '*'
    year: '*'

実行してみるとエラー。 なんだか前の行と一緒にされているみたいです。

TASK [hoge_jobs : exec hoge] ***************************************
fatal: [hoge_ip]: FAILED! => {
  ...
  "stderr": "Error: Jobs Document was invalid for format yaml: mapping values are not allowed here\n in \"<reader>\", line 3, column 3:\n
  "Request failed: 400 Bad Request"
  ...
}

解決方法

結構悩んだのですが、1行空けるだけで解決します。

- description: '15分毎にhogeを実施'
  name: hoge_job
  nodesSelectedByDefault: true

{% if rundeck.jobs.is_notification %}
  notification:
    onfailure:
      plugin:
        configuration:
          webhook_url: {{ rundeck.jobs.notification.slack_webhook }}
        type: SlackNotification
  notifyAvgDurationThreshold: null
{% endif %}

  schedule:
    month: '*'
    time:
      hour: '*'
      minute: '*/15'
      seconds: '0'
    weekday:
      day: '*'
    year: '*'

ymlは改行が挟まっていても読み込めるので見栄えが悪くなる以外は問題ないです。本当にこの解決方法しかないのかな。。。。

一旦これで。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0