Add option to activate the fix on systems with bash

This commit is contained in:
Laurent Indermuehle 2022-06-22 15:41:44 +02:00
commit 3fe6f9b73d
No known key found for this signature in database
GPG key ID: 93FA944C9F34DD09
2 changed files with 26 additions and 10 deletions

View file

@ -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,

View file

@ -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: