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):
if {{ existing_ansible_var }} == "string1"
new_ansible_var = "a"
else if {{ existing_ansible_var }} == "string2"
new_ansible_var = "b"
<...>
else
new_ansible_var = ""
I can use a pretty slick technique with a dictionary that looks like this in Jinja:
{% set new_ansible_var = {"string1": "a", "string2": "b"}[existing_ansible_var] | default("") -%}
Can I use a similar dictionary construct to set a variable (set_fact
) in a playbook?