0

In an Ansible role, how to define a variable depending on another one?

I am designing a role and want its interface to understand a playbook variable like framework_enable_java = yes or framework_enable_java = mysql tomcat and want to write a vars/main.yml files that defines boolean values

framework_enable_java_core
framework_enable_java_mysql
framework_enable_java_tomcat

according to the content of framework_enable_java. I tried the obvious definitions similar to

framework_enable_java_mysql: 'mysql' in framework_enable_java

and several more or less subtle approaches like

framework_enable_java_mysql: {{ 'mysql' in framework_enable_java }}

or

{% if 'mysql' in framework_enable_java %}
framework_enable_java_mysql: yes
{% else %}
framework_enable_java_mysql: no
{% endif %}

None of them turned out to be working. The similar looking question is unrelated as it is more like implementing variable indirection than variable deduction.

Is it at all possible to write the desired vars/main.yml for my role? How would it look like? If it is not possible, what would be the best way to make these deductions? (e.g. using a task include?)

Michaël Le Barbier
  • 6,103
  • 5
  • 28
  • 57

1 Answers1

1

Answer from the comments:

framework_enable_java_mysql: "{{ 'mysql' in framework_enable_java }}"

Double quotes are essential here because otherwise YAML parser tries to construct an object(dictionary) and not templated variable.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193