title: Case statement for setting var in Ansible/Jinja2
body: <p>I'm using Ansible with Jinja2 templates, and this is a scenario that I can't find a solution for in Ansible's documentation or googling around for Jinja2 examples. Here's the logic that I want to achieve in Ansible:</p>
  
  <pre><code>if {{ existing_ansible_var }} == "string1"
    new_ansible_var = "a"
  else if {{ existing_ansible_var }} == "string2"
    new_ansible_var = "b"
  &lt;...&gt;
  else
    new_ansible_var = ""
  </code></pre>
  
  <p>I could probably do this by combining several techniques, the variable assignment from here: <a href="https://stackoverflow.com/questions/3727045/set-variable-in-jinja">Set variable in jinja</a>, the conditional comparison here: <a href="http://jinja.pocoo.org/docs/dev/templates/#if-expression" rel="noreferrer">http://jinja.pocoo.org/docs/dev/templates/#if-expression</a>, and the defaulting filter here: <a href="https://docs.ansible.com/playbooks_filters.html#defaulting-undefined-variables" rel="noreferrer">https://docs.ansible.com/playbooks_filters.html#defaulting-undefined-variables</a> ,</p>
  
  <p>...but I feel like that's overkill. Is there a simpler way to do this?</p>