0

I wanted to remove multiple databases by setting 'name' to all, and with 'when' condition regarding specific name. Unfortunately after this failed I checked in documentation:

name=all May only be provided if state is dump or import.

Is there a workaround to remove all (mysql) databases that match specific name (for example all with names starting with "temp_20200422_" ?

Frederigo
  • 226
  • 1
  • 4
  • 13

1 Answers1

0

There is option to use mysql_info module to get list of all databases, and than use it in loop in mysql_db

 - name: Get list of databases
      mysql_info:
        login_user: "{{ mysql_database.user }}"
        login_password: "{{ mysql_database.password }}"
        login_host: "{{ mysql_database.address }}"
        filter:
        - databases
      register: databases

- name: "Delete temp databases older than 1 day"
  mysql_db:
    name: "{{ db }}"
    state: absent
    login_user: "{{ mysql_database.user }}"
    login_password: "{{ mysql_database.password }}"
    login_host: "{{ mysql_database.address }}"
  loop_control:
    loop_var: db
  loop: "{{ databases.databases.keys() }}"
  when: "temp__" in db
Frederigo
  • 226
  • 1
  • 4
  • 13