title: Create and access dict in Ansible playbook
body: <p>I'm trying to set a variable in an Ansible playbook based on an existing variable's value which could be one of many different strings. It's basically a case/switch statement and this is the logic (with incorrect syntax):</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 can use a pretty slick technique with a dictionary that looks like this in Jinja:</p>
  
  <pre><code>{% set new_ansible_var = {"string1": "a", "string2": "b"}[existing_ansible_var] | default("") -%}
  </code></pre>
  
  <p>Can I use a similar dictionary construct to set a variable (<code>set_fact</code>) in a playbook?</p>