如何在 Ansible Jinja2 (.j2) 模板中指定“if else”语句?

Dus*_*sty 12 bash scripting jinja2 ansible

我正在ifbash 脚本中运行一条语句。我需要在不同的服务器上部署 bash 脚本。下面是我的 if else 语句。

DIR="/backup/db"
first_time=true
{% if [ -d "$DIR" ]; then %}
  first_time=false
  sudo -u tomcat ln -s /backup/app /opt/tomcat/latest/webapps/msales
  sudo -u tomcat ln -s /backup/store/logs /opt/tomcat/latest/logs
  .....etc
{% fi %}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Ansible 将此script.sh.j2部署到其他服务器。但它说

""msg": "AnsibleError: 模板化字符串时出现模板错误:预期标记 ',', 得到 'string'。"

如何在j2模板中使用if语句?

Kev*_*n C 17

如何在j2模板中使用if语句?

您应该阅读文档
这是一个简单的例子:

{% if my_var == 1 %}
some text here with an actual variable: {{ my_other_var }}
{% else %}
some other text here with nothing
{% endif %}
Run Code Online (Sandbox Code Playgroud)