title: How to specify "if else" statements in Ansible Jinja2 (.j2) templates?
body: <p>I'm running an <code>if</code> statement in a bash script. And I need to deploy the bash script in a different server. Below is my if else statement.</p>
  <pre><code>DIR=&quot;/backup/db&quot;
  first_time=true
  {% if [ -d &quot;$DIR&quot; ]; 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 %}
  </code></pre>
  <p>I'm using Ansible to deploy to this <em>script.sh.j2</em> to the other server.   But it says</p>
  <blockquote>
  <p>&quot;&quot;msg&quot;: &quot;AnsibleError: template error while templating string: expected token ',', got 'string'.&quot;</p>
  </blockquote>
  <p>How to use if statements in j2 templates?</p>