--- - name: "Create specified MySQL databases" community.mysql.mysql_db: login_unix_socket: /run/mysqld/mysqld.sock state: "{{ item.state | default('present') }}" name: "{{ item.name }}" loop: "{{ query('list', services.mariadb.databases)[0] }}" when: - services.mariadb.databases is defined - name: "Create specified Mysql-Users" community.mysql.mysql_user: login_unix_socket: /run/mysqld/mysqld.sock state: "{{ item.state | default('present') }}" name: "{{ item.username }}" password: "{{ item.password }}" priv: "{{ item.privileges }}" loop: "{{ query('list', services.mariadb.users)[0] }}" when: - services.mariadb.users is defined - community.mysql.mysql_info: login_unix_socket: /run/mysqld/mysqld.sock filter: - databases - users return_empty_dbs: yes register: mysql_info when: - services.mariadb.databases is defined - name: "Dump databases that are not meant to be deployed" community.mysql.mysql_db: login_unix_socket: /run/mysqld/mysqld.sock state: dump name: "{{ item }}" target: "/root/{{ item }}.sql" loop: "{{ mysql_info['databases'] | list }}" when: - services.mariadb.databases is defined - "item != 'mysql'" - "item != 'test'" - "item != 'information_schema'" - "item != 'performance_schema'" - "item not in (services.mariadb.databases | map(attribute='name') | list)" - name: "Delete databases that are not meant to be deployed" community.mysql.mysql_db: login_unix_socket: /run/mysqld/mysqld.sock state: absent name: "{{ item }}" loop: "{{ mysql_info['databases'] | list }}" when: - services.mariadb.databases is defined - "item != 'mysql'" - "item != 'test'" - "item != 'information_schema'" - "item != 'performance_schema'" - "item not in (services.mariadb.databases | map(attribute='name') | list)" - name: "Delete users that are not meant to be deployed" community.mysql.mysql_user: login_unix_socket: /run/mysqld/mysqld.sock state: absent user: "{{ item }}" loop: "{{ mysql_info['users']['localhost'] | list }}" when: - services.mariadb.databases is defined - "item != 'mysql'" - "item != 'mariadb.sys'" - "item != ''" - "item != 'root'" - "item not in (services.mariadb.users | map(attribute='username') | list)"