From b9a315a2c07fbfd6b6d6024bf739453a3cc1d068 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Tue, 25 Feb 2020 22:48:32 +0300 Subject: [PATCH] mysql_db: add dump_extra_args parameter (#67747) * mysql_db: add dump_extra_args parameter * add changelog fragment --- ...747-mysql_db_add_dump_extra_args_param.yml | 2 ++ .../modules/database/mysql/mysql_db.py | 25 +++++++++++++++++-- .../mysql_db/tasks/state_dump_import.yml | 3 ++- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/67747-mysql_db_add_dump_extra_args_param.yml diff --git a/changelogs/fragments/67747-mysql_db_add_dump_extra_args_param.yml b/changelogs/fragments/67747-mysql_db_add_dump_extra_args_param.yml new file mode 100644 index 00000000000..95fc934a35e --- /dev/null +++ b/changelogs/fragments/67747-mysql_db_add_dump_extra_args_param.yml @@ -0,0 +1,2 @@ +minor_changes: +- mysql_db - add the ``dump_extra_args`` parameter (https://github.com/ansible/ansible/pull/67747). diff --git a/lib/ansible/modules/database/mysql/mysql_db.py b/lib/ansible/modules/database/mysql/mysql_db.py index 346ab84206a..19cc283b34f 100644 --- a/lib/ansible/modules/database/mysql/mysql_db.py +++ b/lib/ansible/modules/database/mysql/mysql_db.py @@ -111,6 +111,13 @@ options: type: bool default: no version_added: '2.10' + dump_extra_args: + description: + - Provide additional arguments for mysqldump. + Used when I(state=dump) only, ignored otherwise. + required: no + type: str + version_added: '2.10' seealso: - module: mysql_info - module: mysql_variables @@ -229,6 +236,15 @@ EXAMPLES = r''' - foo - bar state: absent + +# Dump database with argument not directly supported by this module +# using dump_extra_args parameter +- name: Dump databases without including triggers + mysql_db: + state: dump + name: foo + target: /tmp/dump.sql + dump_extra_args: --skip-triggers ''' RETURN = r''' @@ -288,7 +304,7 @@ def db_delete(cursor, db): def db_dump(module, host, user, password, db_name, target, all_databases, port, config_file, socket=None, ssl_cert=None, ssl_key=None, ssl_ca=None, single_transaction=None, quick=None, ignore_tables=None, hex_blob=None, - encoding=None, force=False, master_data=0, skip_lock_tables=False): + encoding=None, force=False, master_data=0, skip_lock_tables=False, dump_extra_args=None): cmd = module.get_bin_path('mysqldump', True) # If defined, mysqldump demands --defaults-extra-file be the first option if config_file: @@ -328,6 +344,8 @@ def db_dump(module, host, user, password, db_name, target, all_databases, port, cmd += " --hex-blob" if master_data: cmd += " --master-data=%s" % master_data + if dump_extra_args is not None: + cmd += " " + dump_extra_args path = None if os.path.splitext(target)[-1] == '.gz': @@ -459,6 +477,7 @@ def main(): force=dict(type='bool', default=False), master_data=dict(type='int', default=0, choices=[0, 1, 2]), skip_lock_tables=dict(type='bool', default=False), + dump_extra_args=dict(type='str'), ), supports_check_mode=True, ) @@ -497,6 +516,7 @@ def main(): force = module.params["force"] master_data = module.params["master_data"] skip_lock_tables = module.params["skip_lock_tables"] + dump_extra_args = module.params["dump_extra_args"] if len(db) > 1 and state == 'import': module.fail_json(msg="Multiple databases are not supported with state=import") @@ -563,7 +583,8 @@ def main(): login_password, db, target, all_databases, login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca, single_transaction, quick, ignore_tables, - hex_blob, encoding, force, master_data, skip_lock_tables) + hex_blob, encoding, force, master_data, skip_lock_tables, + dump_extra_args) if rc != 0: module.fail_json(msg="%s" % stderr) module.exit_json(changed=True, db=db_name, db_list=db, msg=stdout, diff --git a/test/integration/targets/mysql_db/tasks/state_dump_import.yml b/test/integration/targets/mysql_db/tasks/state_dump_import.yml index fbd82f8d18c..c1bb3674ce8 100644 --- a/test/integration/targets/mysql_db/tasks/state_dump_import.yml +++ b/test/integration/targets/mysql_db/tasks/state_dump_import.yml @@ -67,13 +67,14 @@ force: yes master_data: 1 skip_lock_tables: yes + dump_extra_args: --skip-triggers register: result - name: assert successful completion of dump operation assert: that: - result is changed - - result.executed_commands[0] is search("mysqldump --force --socket={{ mysql_socket }} --databases {{ db_name }} --skip-lock-tables --quick --ignore-table={{ db_name }}.department --master-data=1") + - result.executed_commands[0] is search("mysqldump --force --socket={{ mysql_socket }} --databases {{ db_name }} --skip-lock-tables --quick --ignore-table={{ db_name }}.department --master-data=1 --skip-triggers") - name: state dump/import - file name should exist file: name={{ db_file_name }} state=file