diff --git a/plugins/modules/mysql_db.py b/plugins/modules/mysql_db.py index 355b19f..06a7ccc 100644 --- a/plugins/modules/mysql_db.py +++ b/plugins/modules/mysql_db.py @@ -156,6 +156,14 @@ options: - Can be useful, for example, when I(state=import) and a dump file contains relative paths. type: path version_added: '3.4.0' + pipefail: + description: + - Use bash instead of sh and add -o pipefail to catch errors from the + mysql_dump command when compression is used. The default is false to + prevent issue on system without bash. The default may change in a future release + type: bool + default: no + version_added: '3.3.1' seealso: - module: community.mysql.mysql_info @@ -355,7 +363,7 @@ def db_dump(module, host, user, password, db_name, target, all_databases, port, single_transaction=None, quick=None, ignore_tables=None, hex_blob=None, encoding=None, force=False, master_data=0, skip_lock_tables=False, dump_extra_args=None, unsafe_password=False, restrict_config_file=False, - check_implicit_admin=False): + check_implicit_admin=False, pipefail=False): cmd = module.get_bin_path('mysqldump', True) # If defined, mysqldump demands --defaults-extra-file be the first option if config_file: @@ -422,13 +430,20 @@ def db_dump(module, host, user, password, db_name, target, all_databases, port, elif os.path.splitext(target)[-1] == '.xz': path = module.get_bin_path('xz', True) - if path: + if pipefail: cmd = 'set -o pipefail && %s | %s > %s' % (cmd, path, shlex_quote(target)) + elif path: + cmd = '%s | %s > %s' % (cmd, path, shlex_quote(target)) else: cmd += " > %s" % shlex_quote(target) executed_commands.append(cmd) - rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True, executable='bash') + + if pipefail: + rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True, executable='bash') + else: + rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True) + return rc, stdout, stderr @@ -569,6 +584,7 @@ def main(): check_implicit_admin=dict(type='bool', default=False), config_overrides_defaults=dict(type='bool', default=False), chdir=dict(type='path'), + pipefail=dict(type='bool', default=False), ) module = AnsibleModule( @@ -618,6 +634,7 @@ def main(): check_implicit_admin = module.params['check_implicit_admin'] config_overrides_defaults = module.params['config_overrides_defaults'] chdir = module.params['chdir'] + pipefail = module.params['pipefail'] if chdir: try: @@ -704,7 +721,7 @@ def main(): ssl_ca, single_transaction, quick, ignore_tables, hex_blob, encoding, force, master_data, skip_lock_tables, dump_extra_args, unsafe_login_password, restrict_config_file, - check_implicit_admin) + check_implicit_admin, pipefail) 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/tests/integration/targets/test_mysql_db/tasks/issue_256_mysqldump_errors.yml b/tests/integration/targets/test_mysql_db/tasks/issue_256_mysqldump_errors.yml index 9c26022..61d7e75 100644 --- a/tests/integration/targets/test_mysql_db/tasks/issue_256_mysqldump_errors.yml +++ b/tests/integration/targets/test_mysql_db/tasks/issue_256_mysqldump_errors.yml @@ -97,11 +97,11 @@ that: - full_dump_without_t1 is failed - full_dump_without_t1.msg is search( - "View 'db2.v1' references invalid table(s)") + 'references invalid table') - full_dump_without_t1_gz_without_fix is changed - full_dump_without_t1_gz_with_fix is failed - full_dump_without_t1_gz_with_fix.msg is search( - "View 'db2.v1' references invalid table(s)") + 'references invalid table') - name: Dumps errors | Distinct dump after drop t1 without compression community.mysql.mysql_db: @@ -128,17 +128,16 @@ register: dump_db2_without_t1_gz_with_fix ignore_errors: true - - name: Dumps errors | Check full dump + - name: Dumps errors | Check distinct dump ansible.builtin.assert: that: - dump_db2_without_t1 is failed - dump_db2_without_t1.msg is search( - "View 'db2.v1' references invalid table(s)") + 'references invalid table') - dump_db2_without_t1_gz_without_fix is changed - dump_db2_without_t1_gz_with_fix is failed - dump_db2_without_t1_gz_with_fix.msg is search( - "View 'db2.v1' references invalid table(s)") - + 'references invalid table') - name: Dumps errors | Cleanup community.mysql.mysql_db: name: