Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Conditional task parameter

Tags:

ansible

jinja2

I need to pass ssl_ca to mysql_db module only if mysql_use_ssl is defined. Is this possible using one task, like:

mysql_db: name=mydb state=import target=/tmp/mysql.sql login_host="mydbhost" login_user="root" login_password="password" {% if mysql_use_ssl %}ssl_ca=/path/to/cert.pem{% endif %}

?

This actual snippet does not work, result:

{"failed": true, "msg": "template error while templating string: Encountered unknown tag 'endif'.. String: /path/to/cert.pem{% endif %}"}

When moving the conditional as:

mysql_db: name=mydb state=import target=/tmp/mysql.sql login_host="mydbhost" login_user="root" login_password="password" ssl_ca="{% if mysql_use_ssl %}/path/to/cert.pem{% else %}none{% endif %}"

Then it "works" but none is not a supported parameter for turning off mysql ssl connection, so it does not turn off ssl.

like image 673
Misko Avatar asked Jan 18 '17 16:01

Misko


1 Answers

There is omit keyword/variable for this:

mysql_db:
  name: mydb
  state: import
  target: /tmp/mysql.sql
  login_host: mydbhost
  login_user: root
  login_password: password
  ssl_ca: "{{ '/path/to/cert.pem' if mysql_use_ssl | default(false) else omit }}"
like image 54
Konstantin Suvorov Avatar answered Dec 16 '22 05:12

Konstantin Suvorov