Help us understand the problem. What is going on with this article?

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

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

一旦これで。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした