0

I have the following dictionary :

mariadb_custom_cnf: { mysqld: { log-bin: '', server_id: '{{ groups[host_group_name].index(inventory_hostname) | int + 1 }}', replicate-do-db: 'replicate' }}

What I want to do is for it to be created when a condition is met (e.g. my_var: "{{ 'foo' if my_condition }}" ) but I can't find the right syntax to get it to work.

I've tried several ways but with no success :

mariadb_custom_cnf: { mysqld: { log-bin: '', server_id: '{{ groups[host_group_name].index(inventory_hostname) | int + 1 }}', replicate-do-db: 'replicate' }} if mariadb_replication_user is defined
mariadb_custom_cnf: "{ mysqld: { log-bin: '', server_id: '{{ groups[host_group_name].index(inventory_hostname) | int + 1 }}', replicate-do-db: 'replicate' }} if mariadb_replication_user is defined"
mariadb_custom_cnf: "{{ { mysqld: { log-bin: '', server_id: '{{ groups[host_group_name].index(inventory_hostname) | int + 1 }}', replicate-do-db: 'replicate' }} if mariadb_replication_user is defined }}"

Any idea(s)?

Juntao
  • 11
  • 5

1 Answers1

0

You can definitively create your dict in a tasks set_fact with a when clause:

- name: Define mariadb_custom_cnf
  set_fact:
    mariadb_custom_cnf: 
      mysqld:
        log-bin: ""
        server_id: "{{ groups[host_group_name].index(inventory_hostname) | int + 1 }}"
        replicate-do-db: "replicate"
  when: mariadb_replication_user is defined
xenlo
  • 761
  • 1
  • 7
  • 21
  • I had already created a set_fact task with the one liner as a workaround but I hadn't thought of declaring the dictionary your way. Thanks for that! I hope someone has the answer to my question and will share it here, I'm curious to know if it's feasible to create a dictionary on condition in a vars.yml file. Thanks again for your help. – Juntao Sep 04 '19 at 09:39