title: ansible conditional templates
body: <p>I want to have a simple if else condition in ansibles jinja templates. For plain python</p>
  
  <pre><code>cluster_name+'A' if isCondition is True else cluster_name +'B'
  </code></pre>
  
  <p>wors great if the following variables are defined:</p>
  
  <pre><code>isSingleNode = True
  cluster_name = 'example'
  </code></pre>
  
  <p>In ansible I see the following error:</p>
  
  <pre><code>fatal: [localhost]: FAILED! =&gt; {"changed": false, "failed": true, "msg": "AnsibleError: template error while templating string: no test named 'True'. String: {\n\n   \"key\" : \"{{ groups[cluster_name+'_mn01' if isSingleNode is True else cluster_name + '_mn02'] }}\"\n}\n"}
  </code></pre>
  
  <p>Here is a minimal example:</p>
  
  <p>file_1: variables</p>
  
  <pre><code>---
  isCondition: True
  </code></pre>
  
  <p>file_2: playbook.yml</p>
  
  <pre><code>---
  - hosts: all
    tasks:
      - include_vars: variables
      - debug: msg=" condition is {{ isCondition }} with cluster_name {{ cluster_name }}"
  
      - name: copy file
        template: src="bare_cluster.bp.j2" dest={{ cluster_name }}_blueprint.json backup=yes
  </code></pre>
  
  <p>file_4: inventory</p>
  
  <pre><code>[examplecluster:children]
  examplecluster_mn01
  
  [mn01:children]
  examplecluster_mn01
  
  [examplecluster_mn01]
  localhost ansible_connection=local
  </code></pre>
  
  <p>file_5: bare_cluster.bp.j2</p>
  
  <pre><code>{
  
     "key" : "{{ groups[cluster_name+'_mn01' if isSingleNode is True else cluster_name + '_mn02'] }}"
  }
  </code></pre>
  
  <p>command to execute the minimal example is <code>ansible-playbook -i inventory playbook.yml -e 'cluster_name=examplecluster'</code></p>