Added support to create and delete multiple databases in MySQL (#58602)

* Added support to create/delete mulitiple databases in MySQL

Fixes: #58370

* Added additional tests cases and fixed documentation changes

* Code refactoring and added tests for better test coverage

- Removed db_exists usage from most of the code. Used existence_list
 and non_existence_list instead

- Added additional tests to cover all scenarios w.r.t creation and deletion
 on multiple databases

- Added tests for dump operations

* Minor fix

* Minor fix - create check mode test

* Added dump tests for better dump tests coverage

* Removed minor database connection details

* fixed error

* Added test case for import operations

* Code refactoring and review fixes

- Added dump all test case

* Fixed review comments

* Minor review comment fixes

* Altered db_create return value

* Removed db_list and altered "does exist" to just "exist"

* Kept db and db_list in module.exit_json

* Refactored tests

- Added removal of dump2 file

* Moved import tests to state_dump_import file

* Removed import tests from multi_db_create_delete

* Updated porting guide, added RETURN block

* Minor identation fix

* Added validation to check if databases are dumped
pull/59256/head
pratikgadiya12 5 years ago committed by Felix Fontein
parent cdf0947df0
commit 393e4a41d0

@ -0,0 +1,3 @@
minor_changes:
- mysql_db now supports creation and deletion of multiple databases (https://github.com/ansible/ansible/issues/58370)
- mysql_db - now behaves better w.r.t ``changed`` results in ``check_mode``

@ -68,6 +68,7 @@ Noteworthy module changes
* `vmware_dvswitch <vmware_dvswitch_module>` accepts `folder` parameter to place dvswitch in user defined folder. This option makes `datacenter` as an optional parameter. * `vmware_dvswitch <vmware_dvswitch_module>` accepts `folder` parameter to place dvswitch in user defined folder. This option makes `datacenter` as an optional parameter.
* `vmware_datastore_cluster <vmware_datastore_cluster_module>` accepts `folder` parameter to place datastore cluster in user defined folder. This option makes `datacenter` as an optional parameter. * `vmware_datastore_cluster <vmware_datastore_cluster_module>` accepts `folder` parameter to place datastore cluster in user defined folder. This option makes `datacenter` as an optional parameter.
* `mysql_db <mysql_db_module>` returns new `db_list` parameter in addition to `db` parameter. This `db_list` parameter refers to list of database names. `db` parameter will be deprecated in version `2.13`.
* The ``python_requirements_facts`` module was renamed to :ref:`python_requirements_info <python_requirements_info_module>`. * The ``python_requirements_facts`` module was renamed to :ref:`python_requirements_info <python_requirements_info_module>`.
* The ``jenkins_job_facts`` module was renamed to :ref:`jenkins_job_info <jenkins_job_info_module>`. * The ``jenkins_job_facts`` module was renamed to :ref:`jenkins_job_info <jenkins_job_info_module>`.

@ -24,7 +24,7 @@ options:
description: description:
- name of the database to add or remove. - name of the database to add or remove.
- I(name=all) May only be provided if I(state) is C(dump) or C(import). - I(name=all) May only be provided if I(state) is C(dump) or C(import).
- List of databases is provided with I(state=dump) only. - List of databases is provided with I(state=dump), I(state=present) and I(state=absent).
- if name=all Works like --all-databases option for mysqldump (Added in 2.0). - if name=all Works like --all-databases option for mysqldump (Added in 2.0).
required: true required: true
type: list type: list
@ -78,6 +78,13 @@ EXAMPLES = r'''
name: bobdata name: bobdata
state: present state: present
- name: Create new databases with names 'foo' and 'bar'
mysql_db:
name:
- foo
- bar
state: present
# Copy database dump file to remote host and restore it to database 'my_db' # Copy database dump file to remote host and restore it to database 'my_db'
- name: Copy database dump file - name: Copy database dump file
copy: copy:
@ -115,6 +122,32 @@ EXAMPLES = r'''
state: import state: import
name: all name: all
target: /tmp/dump.sql target: /tmp/dump.sql
- name: Delete database with name 'bobdata'
mysql_db:
name: bobdata
state: absent
- name: Make sure there is neither a database with name 'foo', nor one with name 'bar'
mysql_db:
name:
- foo
- bar
state: absent
'''
RETURN = r'''
db:
description: Database names in string format delimited by white space.
returned: always
type: str
sample: "foo bar"
db_list:
description: List of database names.
returned: always
type: list
sample: ["foo", "bar"]
version_added: '2.9'
''' '''
import os import os
@ -136,13 +169,16 @@ from ansible.module_utils._text import to_native
def db_exists(cursor, db): def db_exists(cursor, db):
res = 0 res = 0
for each_db in db: for each_db in db:
res += cursor.execute("SHOW DATABASES LIKE %s", (each_db.strip().replace("_", r"\_"),)) res += cursor.execute("SHOW DATABASES LIKE %s", (each_db.replace("_", r"\_"),))
return res == len(db) return res == len(db)
def db_delete(cursor, db): def db_delete(cursor, db):
query = "DROP DATABASE %s" % mysql_quote_identifier(''.join(db), 'database') if not db:
cursor.execute(query) return False
for each_db in db:
query = "DROP DATABASE %s" % mysql_quote_identifier(each_db, 'database')
cursor.execute(query)
return True return True
@ -248,15 +284,20 @@ def db_import(module, host, user, password, db_name, target, all_databases, port
def db_create(cursor, db, encoding, collation): def db_create(cursor, db, encoding, collation):
if not db:
return False
query_params = dict(enc=encoding, collate=collation) query_params = dict(enc=encoding, collate=collation)
query = ['CREATE DATABASE %s' % mysql_quote_identifier(''.join(db), 'database')] res = 0
if encoding: for each_db in db:
query.append("CHARACTER SET %(enc)s") query = ['CREATE DATABASE %s' % mysql_quote_identifier(each_db, 'database')]
if collation: if encoding:
query.append("COLLATE %(collate)s") query.append("CHARACTER SET %(enc)s")
query = ' '.join(query) if collation:
cursor.execute(query, query_params) query.append("COLLATE %(collate)s")
return True query = ' '.join(query)
res += cursor.execute(query, query_params)
return res > 0
# =========================================== # ===========================================
# Module execution. # Module execution.
@ -293,7 +334,8 @@ def main():
db = module.params["name"] db = module.params["name"]
if not db: if not db:
module.fail_json(msg="Please provide at least one database name") module.exit_json(changed=False, db=db, db_list=[])
db = [each_db.strip() for each_db in db]
encoding = module.params["encoding"] encoding = module.params["encoding"]
collation = module.params["collation"] collation = module.params["collation"]
@ -318,18 +360,16 @@ def main():
single_transaction = module.params["single_transaction"] single_transaction = module.params["single_transaction"]
quick = module.params["quick"] quick = module.params["quick"]
if len(db) > 1 and state != 'dump': if len(db) > 1 and state == 'import':
module.fail_json(msg="Multiple databases is only supported with state=dump") module.fail_json(msg="Multiple databases are not supported with state=import")
db_name = ' '.join(db) db_name = ' '.join(db)
all_databases = False
if state in ['dump', 'import']: if state in ['dump', 'import']:
if target is None: if target is None:
module.fail_json(msg="with state=%s target is required" % state) module.fail_json(msg="with state=%s target is required" % state)
if db == ['all']: if db == ['all']:
db = ['mysql']
all_databases = True all_databases = True
else:
all_databases = False
else: else:
if db == ['all']: if db == ['all']:
module.fail_json(msg="name is not allowed to equal 'all' unless state equals import, or dump.") module.fail_json(msg="name is not allowed to equal 'all' unless state equals import, or dump.")
@ -346,81 +386,65 @@ def main():
changed = False changed = False
if not os.path.exists(config_file): if not os.path.exists(config_file):
config_file = None config_file = None
if db_exists(cursor, db):
if state == "absent":
if module.check_mode:
module.exit_json(changed=True, db=db_name)
try:
changed = db_delete(cursor, db)
except Exception as e:
module.fail_json(msg="error deleting database: %s" % to_native(e))
module.exit_json(changed=changed, db=db_name)
elif state == "dump":
if module.check_mode:
module.exit_json(changed=True, db=db_name)
rc, stdout, stderr = db_dump(module, login_host, login_user,
login_password, db, target, all_databases,
login_port, config_file, socket, ssl_cert, ssl_key,
ssl_ca, single_transaction, quick, ignore_tables)
if rc != 0:
module.fail_json(msg="%s" % stderr)
else:
module.exit_json(changed=True, db=db_name, msg=stdout)
elif state == "import":
if module.check_mode:
module.exit_json(changed=True, db=db_name)
rc, stdout, stderr = db_import(module, login_host, login_user,
login_password, db, target,
all_databases,
login_port, config_file,
socket, ssl_cert, ssl_key, ssl_ca)
if rc != 0:
module.fail_json(msg="%s" % stderr)
else:
module.exit_json(changed=True, db=db_name, msg=stdout)
elif state == "present": existence_list = []
module.exit_json(changed=False, db=db_name) non_existence_list = []
else: if not all_databases:
if state == "present": for each_database in db:
if module.check_mode: if db_exists(cursor, [each_database]):
changed = True existence_list.append(each_database)
else:
try:
changed = db_create(cursor, db, encoding, collation)
except Exception as e:
module.fail_json(msg="error creating database: %s" % to_native(e),
exception=traceback.format_exc())
module.exit_json(changed=changed, db=db_name)
elif state == "import":
if module.check_mode:
module.exit_json(changed=True, db=db_name)
else: else:
try: non_existence_list.append(each_database)
changed = db_create(cursor, db, encoding, collation)
if changed: if state == "absent":
rc, stdout, stderr = db_import(module, login_host, login_user, if module.check_mode:
login_password, db, target, all_databases, module.exit_json(changed=bool(existence_list), db=db_name, db_list=db)
login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca) try:
if rc != 0: changed = db_delete(cursor, existence_list)
module.fail_json(msg="%s" % stderr) except Exception as e:
else: module.fail_json(msg="error deleting database: %s" % to_native(e))
module.exit_json(changed=True, db=db_name, msg=stdout) module.exit_json(changed=changed, db=db_name, db_list=db)
except Exception as e: elif state == "present":
module.fail_json(msg="error creating database: %s" % to_native(e), if module.check_mode:
exception=traceback.format_exc()) module.exit_json(changed=bool(non_existence_list), db=db_name, db_list=db)
changed = False
elif state == "absent": if non_existence_list:
module.exit_json(changed=False, db=db_name) try:
changed = db_create(cursor, non_existence_list, encoding, collation)
elif state == "dump": except Exception as e:
if module.check_mode: module.fail_json(msg="error creating database: %s" % to_native(e),
module.exit_json(changed=False, db=db_name) exception=traceback.format_exc())
module.fail_json(msg="Cannot dump database %r - not found" % (db_name)) module.exit_json(changed=changed, db=db_name, db_list=db)
elif state == "dump":
if non_existence_list and not all_databases:
module.fail_json(msg="Cannot dump database(s) %r - not found" % (', '.join(non_existence_list)))
if module.check_mode:
module.exit_json(changed=True, db=db_name, db_list=db)
rc, stdout, stderr = db_dump(module, login_host, login_user,
login_password, db, target, all_databases,
login_port, config_file, socket, ssl_cert, ssl_key,
ssl_ca, single_transaction, quick, ignore_tables)
if rc != 0:
module.fail_json(msg="%s" % stderr)
module.exit_json(changed=True, db=db_name, db_list=db, msg=stdout)
elif state == "import":
if module.check_mode:
module.exit_json(changed=True, db=db_name, db_list=db)
if non_existence_list and not all_databases:
try:
db_create(cursor, non_existence_list, encoding, collation)
except Exception as e:
module.fail_json(msg="error creating database: %s" % to_native(e),
exception=traceback.format_exc())
rc, stdout, stderr = db_import(module, login_host, login_user,
login_password, db, target,
all_databases,
login_port, config_file,
socket, ssl_cert, ssl_key, ssl_ca)
if rc != 0:
module.fail_json(msg="%s" % stderr)
module.exit_json(changed=True, db=db_name, db_list=db, msg=stdout)
if __name__ == '__main__': if __name__ == '__main__':

@ -243,3 +243,5 @@
- include: state_dump_import.yml format_type=gz file=dbdata.gz format_msg_type=gzip file2=dump2.gz file3=dump3.gz - include: state_dump_import.yml format_type=gz file=dbdata.gz format_msg_type=gzip file2=dump2.gz file3=dump3.gz
- include: state_dump_import.yml format_type=bz2 file=dbdata.bz2 format_msg_type=bzip2 file2=dump2.bz2 file3=dump3.bz2 - include: state_dump_import.yml format_type=bz2 file=dbdata.bz2 format_msg_type=bzip2 file2=dump2.bz2 file3=dump3.bz2
- include: multi_db_create_delete.yml

@ -0,0 +1,557 @@
# Copyright (c) 2019, Pratik Gadiya <pratikgadiya1@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- set_fact:
db1_name="database1"
db2_name="database2"
db3_name="database3"
db4_name="database4"
db5_name="database5"
dump1_file="/tmp/dump1_file.sql"
dump2_file="/tmp/all.sql"
# ============================== CREATE TEST ===============================
#
# ==========================================================================
# Initial check - To confirm that database does not exist before executing check mode tasks
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases does not exist
assert:
that:
- "'{{ db1_name }}' not in mysql_result.stdout"
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db3_name }}' not in mysql_result.stdout"
# ==========================================================================
# Create multiple databases that does not exists (check mode)
- name: Create multiple databases that does not exists (check mode)
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
state: present
login_unix_socket: '{{ mysql_socket }}'
register: check_mode_result
check_mode: yes
- name: assert successful completion of create database using check_mode since databases does not exist prior
assert:
that:
- check_mode_result.changed == true
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases does not exist (since created via check mode)
assert:
that:
- "'{{ db1_name }}' not in mysql_result.stdout"
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db3_name }}' not in mysql_result.stdout"
# ==========================================================================
# Create multiple databases
- name: Create multiple databases
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
state: present
login_unix_socket: '{{ mysql_socket }}'
register: result
- name: assert successful completion of create database
assert:
that:
- result.changed == true
- result.db_list == ['{{ db1_name }}', '{{ db2_name }}', '{{ db3_name }}']
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist after creation
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
# =========================================================================
# Recreate already existing databases (check mode)
- name: Recreate already existing databases (check mode)
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
state: present
login_unix_socket: '{{ mysql_socket }}'
register: check_mode_result
check_mode: yes
- name: assert that recreation of existing databases does not make change (since recreated using check mode)
assert:
that:
- check_mode_result.changed == false
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist (since performed recreation of existing databases via check mode)
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
# ==========================================================================
# Recreate same databases
- name: Recreate multiple databases
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
state: present
login_unix_socket: '{{ mysql_socket }}'
register: result
- name: assert that recreation of existing databases does not make change
assert:
that:
- result.changed == false
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases does priorly exist
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
# ==========================================================================
# Delete one of the databases (db2 here)
- name: Delete db2 database
mysql_db:
name:
- '{{ db2_name }}'
state: absent
login_unix_socket: '{{ mysql_socket }}'
register: result
- name: assert successful completion of deleting database
assert:
that:
- result.changed == true
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that only db2 database does not exist
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
# =========================================================================
# Recreate multiple databases in which few databases does not exists (check mode)
- name: Recreate multiple databases in which few databases does not exists (check mode)
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
state: present
login_unix_socket: '{{ mysql_socket }}'
register: check_mode_result
check_mode: yes
- name: assert successful completion of recreation of partially existing database using check mode
assert:
that:
- check_mode_result.changed == true
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that recreated non existing databases does not exist (since created via check mode)
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
# ==========================================================================
# Create multiple databases
- name: Create multiple databases
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
state: present
login_unix_socket: '{{ mysql_socket }}'
register: result
- name: assert successful completion of create database
assert:
that:
- result.changed == true
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
# ============================== DUMP TEST =================================
#
# ==========================================================================
# Check that dump file does not exist
- name: Dump file does not exist
file: name={{ dump1_file }} state=absent
# ==========================================================================
# Dump existing databases (check mode)
- name: Dump existing databases (check mode)
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db3_name }}'
state: dump
target: '{{ dump1_file }}'
login_unix_socket: '{{ mysql_socket }}'
register: check_mode_dump_result
check_mode: yes
- name: assert successful completion of dump operation using check mode
assert:
that:
- check_mode_dump_result.changed == true
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist (check mode)
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
- name: state dump - file name should not exist (since dumped via check mode)
file: name={{ dump1_file }} state=absent
# ==========================================================================
# Dump existing and non-existing databases (check mode)
- name: Dump existing and non-existing databases (check mode)
mysql_db:
name:
- "{{ db1_name }}"
- "{{ db4_name }}"
- "{{ db3_name }}"
state: dump
login_unix_socket: '{{ mysql_socket }}'
target: "{{ dump1_file }}"
register: check_mode_dump_result
ignore_errors: True
check_mode: yes
- name: assert that dump operation of existing and non existing databases does not make change (using check mode)
assert:
that:
- "'Cannot dump database' in check_mode_dump_result['msg']"
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist (since check mode)
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
- "'{{ db4_name }}' not in mysql_result.stdout"
- name: state dump - file name should not exist (since prior dump operation performed via check mode)
file: name={{ dump1_file }} state=absent
# ==========================================================================
# Dump non-existing databases (check mode)
- name: Dump non-existing databases (check mode)
mysql_db:
name:
- "{{ db4_name }}"
- "{{ db5_name }}"
state: dump
target: "{{ dump1_file }}"
login_unix_socket: '{{ mysql_socket }}'
register: check_mode_dump_result
ignore_errors: True
check_mode: yes
- name: assert successful completion of dump operation using check mode
assert:
that:
- "'Cannot dump database' in check_mode_dump_result['msg']"
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist (since delete via check mode)
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
- "'{{ db4_name }}' not in mysql_result.stdout"
- "'{{ db5_name }}' not in mysql_result.stdout"
- name: state dump - file name should not exist (since prior dump operation performed via check mode)
file: name={{ dump1_file }} state=absent
# ==========================================================================
# Dump existing databases
- name: Dump existing databases
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
state: dump
target: '{{ dump1_file }}'
login_unix_socket: '{{ mysql_socket }}'
register: dump_result
- name: assert successful completion of dump operation
assert:
that:
- dump_result.changed == true
- dump_result.db_list == ['{{ db1_name }}', '{{ db2_name }}', '{{ db3_name }}']
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
- name: state dump - file name should exist
file: name={{ dump1_file }} state=file
- name: Check if db1 database create command is present in the dumped file
shell: "grep -i 'CREATE DATABASE.*`{{ db1_name }}`' {{ dump1_file }}"
- name: Check if db2 database create command is present in the dumped file
shell: "grep -i 'CREATE DATABASE.*`{{ db2_name }}`' {{ dump1_file }}"
- name: Check if db3 database create command is present in the dumped file
shell: "grep -i 'CREATE DATABASE.*`{{ db3_name }}`' {{ dump1_file }}"
# ==========================================================================
# Dump all databases
- name: state dump - dump2 file name should not exist
file: name={{ dump2_file }} state=absent
- name: Dump existing databases
mysql_db:
name: all
state: dump
target: '{{ dump2_file }}'
login_unix_socket: '{{ mysql_socket }}'
register: dump_result
- name: assert successful completion of dump operation
assert:
that:
- dump_result.changed == true
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist
assert:
that:
- "'{{ db1_name }}' in mysql_result.stdout"
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
- "'{{ db4_name }}' not in mysql_result.stdout"
- "'{{ db5_name }}' not in mysql_result.stdout"
- name: state dump - file name should exist
file: name={{ dump2_file }} state=file
# ============================ DELETE TEST =================================
#
# ==========================================================================
# Delete multiple databases which already exists (check mode)
- name: Delete multiple databases which already exists (check mode)
mysql_db:
name:
- '{{ db2_name }}'
- '{{ db3_name }}'
state: absent
login_unix_socket: '{{ mysql_socket }}'
register: check_mode_result
check_mode: yes
- name: assert successful completion of delete databases which already exists using check mode
assert:
that:
- check_mode_result.changed == true
- name: run command to test state=absent for a database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases exist even after deleting (since deleted via check mode)
assert:
that:
- "'{{ db2_name }}' in mysql_result.stdout"
- "'{{ db3_name }}' in mysql_result.stdout"
# ==========================================================================
# Delete multiple databases
- name: Delete multiple databases
mysql_db:
name:
- '{{ db2_name }}'
- '{{ db3_name }}'
state: absent
login_unix_socket: '{{ mysql_socket }}'
register: result
- name: assert successful completion of deleting database
assert:
that:
- result.changed == true
- result.db_list == ['{{ db2_name }}', '{{ db3_name }}']
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases does not exist
assert:
that:
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db3_name }}' not in mysql_result.stdout"
# ==========================================================================
# Delete non existing databases (check mode)
- name: Delete non existing databases (check mode)
mysql_db:
name:
- '{{ db2_name }}'
- '{{ db4_name }}'
state: absent
login_unix_socket: '{{ mysql_socket }}'
register: check_mode_result
check_mode: yes
- name: assert that deletion of non existing databases does not make change (using check mode)
assert:
that:
- check_mode_result.changed == false
- name: run command to test state=absent for a database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases does not exist since were deleted priorly (check mode)
assert:
that:
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db4_name }}' not in mysql_result.stdout"
# ==========================================================================
# Delete already deleted databases
- name: Delete already deleted databases
mysql_db:
name:
- '{{ db2_name }}'
- '{{ db4_name }}'
state: absent
login_unix_socket: '{{ mysql_socket }}'
register: result
- name: assert that deletion of non existing databases does not make change
assert:
that:
- result.changed == false
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that databases does not exists
assert:
that:
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db4_name }}' not in mysql_result.stdout"
# ==========================================================================
# Delete all databases
- name: Delete all databases
mysql_db:
name:
- '{{ db1_name }}'
- '{{ db2_name }}'
- '{{ db3_name }}'
- '{{ db4_name }}'
- '{{ db5_name }}'
state: absent
login_unix_socket: '{{ mysql_socket }}'
register: result
- name: assert successful completion of deleting database
assert:
that:
- result.changed == true
- name: run command to list databases like specified database name
command: mysql "-e show databases like 'database%';"
register: mysql_result
- name: assert that specific databases does not exist
assert:
that:
- "'{{ db1_name }}' not in mysql_result.stdout"
- "'{{ db2_name }}' not in mysql_result.stdout"
- "'{{ db3_name }}' not in mysql_result.stdout"
- "'{{ db4_name }}' not in mysql_result.stdout"
- "'{{ db5_name }}' not in mysql_result.stdout"
- name: state dump - dump 1 file name should be removed
file: name={{ dump1_file }} state=absent
- name: state dump - dump 2 file name should be removed
file: name={{ dump2_file }} state=absent

@ -179,6 +179,36 @@
state: absent state: absent
login_unix_socket: '{{ mysql_socket }}' login_unix_socket: '{{ mysql_socket }}'
- name: run command to list databases
command: mysql "-e show databases like 'data%';"
register: mysql_result
- name: assert that db_name2 database does not exist
assert:
that:
- "'{{ db_name2 }}' not in mysql_result.stdout"
- name: test state=import to restore a database from dumped file2 (check mode)
mysql_db:
name: '{{ db_name2 }}'
state: import
target: '{{ dump_file2 }}'
login_unix_socket: '{{ mysql_socket }}'
register: check_import_result
check_mode: yes
- name: assert output message restored a database from dump file2 (check mode)
assert: { that: "check_import_result.changed == true" }
- name: run command to list databases
command: mysql "-e show databases like 'data%';"
register: mysql_result
- name: assert that db_name2 database does not exist (check mode)
assert:
that:
- "'{{ db_name2 }}' not in mysql_result.stdout"
- name: test state=import to restore a database from multiple database dumped file2 - name: test state=import to restore a database from multiple database dumped file2
mysql_db: mysql_db:
name: '{{ db_name2 }}' name: '{{ db_name2 }}'
@ -188,7 +218,19 @@
register: import_result2 register: import_result2
- name: assert output message restored a database from dump file2 - name: assert output message restored a database from dump file2
assert: { that: "import_result2.changed == true" } assert:
that:
- import_result2.changed == true
- import_result2.db_list == ['{{ db_name2 }}']
- name: run command to list databases
command: mysql "-e show databases like 'data%';"
register: mysql_result
- name: assert that db_name2 database does exist after import
assert:
that:
- "'{{ db_name2 }}' in mysql_result.stdout"
- name: test state=dump to backup the database of type {{ format_type }} (expect changed=true) - name: test state=dump to backup the database of type {{ format_type }} (expect changed=true)
mysql_db: mysql_db:

Loading…
Cancel
Save