2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansible プレイブックの j2(jinja2) テンプレートの中で条件分岐したいときの書き方

Posted at

やりたいこと

  • hoge.env.j2 ファイル内の環境変数を環境(ローカルとか Stg とか)ごとに設定したい
  • けどわざわざファイルを別に用意するのは少し大げさ
  • 今回はローカルのときだけ DB の環境変数を設定したかった

書き方の例

hoge.env.j2
{% if stage == "local" %}
DB_CONNECTION=mysql
DB_HOST={{ mysql_db_master_host }}
DB_SLAVE={{ mysql_db_slave_host }}
DB_PORT=3306
DB_DATABASE={{ mysql_db_name }}
DB_USERNAME={{ mysql_db_user }}
DB_PASSWORD={{ mail_db_password }}
{% endif %}

参考

if 文の他にも色々使える。
http://jinja.pocoo.org/docs/2.9/

2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?