mysql_db: add the executed_commands returned value (#65498)
* mysql_db: add the executed_commands returned value * add changelog
This commit is contained in:
parent
03c16096d7
commit
651dff6a2e
3 changed files with 44 additions and 11 deletions
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- mysql_db - add the ``executed_commands`` returned value (https://github.com/ansible/ansible/pull/65498).
|
|
@ -166,6 +166,12 @@ db_list:
|
|||
type: list
|
||||
sample: ["foo", "bar"]
|
||||
version_added: '2.9'
|
||||
executed_commands:
|
||||
description: List of commands which tried to run.
|
||||
returned: if executed
|
||||
type: list
|
||||
sample: ["CREATE DATABASE acme"]
|
||||
version_added: '2.10'
|
||||
'''
|
||||
|
||||
import os
|
||||
|
@ -178,6 +184,7 @@ from ansible.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver
|
|||
from ansible.module_utils.six.moves import shlex_quote
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
executed_commands = []
|
||||
|
||||
# ===========================================
|
||||
# MySQL module specific support methods.
|
||||
|
@ -196,6 +203,7 @@ def db_delete(cursor, db):
|
|||
return False
|
||||
for each_db in db:
|
||||
query = "DROP DATABASE %s" % mysql_quote_identifier(each_db, 'database')
|
||||
executed_commands.append(query)
|
||||
cursor.execute(query)
|
||||
return True
|
||||
|
||||
|
@ -249,6 +257,7 @@ def db_dump(module, host, user, password, db_name, target, all_databases, port,
|
|||
else:
|
||||
cmd += " > %s" % shlex_quote(target)
|
||||
|
||||
executed_commands.append(cmd)
|
||||
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
|
||||
return rc, stdout, stderr
|
||||
|
||||
|
@ -291,6 +300,8 @@ def db_import(module, host, user, password, db_name, target, all_databases, port
|
|||
elif os.path.splitext(target)[-1] == '.xz':
|
||||
comp_prog_path = module.get_bin_path('xz', required=True)
|
||||
if comp_prog_path:
|
||||
# The line above is for returned data only:
|
||||
executed_commands.append('%s -dc %s | %s' % (comp_prog_path, target, ' '.join(cmd)))
|
||||
p1 = subprocess.Popen([comp_prog_path, '-dc', target], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
p2 = subprocess.Popen(cmd, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
(stdout2, stderr2) = p2.communicate()
|
||||
|
@ -304,6 +315,7 @@ def db_import(module, host, user, password, db_name, target, all_databases, port
|
|||
else:
|
||||
cmd = ' '.join(cmd)
|
||||
cmd += " < %s" % shlex_quote(target)
|
||||
executed_commands.append(cmd)
|
||||
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
|
||||
return rc, stdout, stderr
|
||||
|
||||
|
@ -321,6 +333,12 @@ def db_create(cursor, db, encoding, collation):
|
|||
query.append("COLLATE %(collate)s")
|
||||
query = ' '.join(query)
|
||||
res += cursor.execute(query, query_params)
|
||||
try:
|
||||
executed_commands.append(cursor.mogrify(query, query_params))
|
||||
except AttributeError:
|
||||
executed_commands.append(cursor._executed)
|
||||
except Exception:
|
||||
executed_commands.append(query)
|
||||
return res > 0
|
||||
|
||||
|
||||
|
@ -431,7 +449,7 @@ def main():
|
|||
changed = db_delete(cursor, existence_list)
|
||||
except Exception as e:
|
||||
module.fail_json(msg="error deleting database: %s" % to_native(e))
|
||||
module.exit_json(changed=changed, db=db_name, db_list=db)
|
||||
module.exit_json(changed=changed, db=db_name, db_list=db, executed_commands=executed_commands)
|
||||
elif state == "present":
|
||||
if module.check_mode:
|
||||
module.exit_json(changed=bool(non_existence_list), db=db_name, db_list=db)
|
||||
|
@ -442,7 +460,7 @@ def main():
|
|||
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, db_list=db)
|
||||
module.exit_json(changed=changed, db=db_name, db_list=db, executed_commands=executed_commands)
|
||||
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)))
|
||||
|
@ -454,7 +472,8 @@ def main():
|
|||
ssl_ca, single_transaction, quick, ignore_tables, hex_blob, encoding)
|
||||
if rc != 0:
|
||||
module.fail_json(msg="%s" % stderr)
|
||||
module.exit_json(changed=True, db=db_name, db_list=db, msg=stdout)
|
||||
module.exit_json(changed=True, db=db_name, db_list=db, msg=stdout,
|
||||
executed_commands=executed_commands)
|
||||
elif state == "import":
|
||||
if module.check_mode:
|
||||
module.exit_json(changed=True, db=db_name, db_list=db)
|
||||
|
@ -471,7 +490,8 @@ def main():
|
|||
socket, ssl_cert, ssl_key, ssl_ca, encoding)
|
||||
if rc != 0:
|
||||
module.fail_json(msg="%s" % stderr)
|
||||
module.exit_json(changed=True, db=db_name, db_list=db, msg=stdout)
|
||||
module.exit_json(changed=True, db=db_name, db_list=db, msg=stdout,
|
||||
executed_commands=executed_commands)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -38,8 +38,9 @@
|
|||
- name: assert output message that database exist
|
||||
assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "result.db =='{{ db_name }}'"
|
||||
- result is changed
|
||||
- result.db == '{{ db_name }}'
|
||||
- result.executed_commands == ["CREATE DATABASE `{{ db_name }}`"]
|
||||
|
||||
- name: run command to test state=present for a database name (expect db_name in stdout)
|
||||
command: mysql "-e show databases like '{{ db_name }}';"
|
||||
|
@ -59,8 +60,9 @@
|
|||
- name: assert output message that database does not exist
|
||||
assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "result.db =='{{ db_name }}'"
|
||||
- result is changed
|
||||
- result.db == '{{ db_name }}'
|
||||
- result.executed_commands == ["DROP DATABASE `{{ db_name }}`"]
|
||||
|
||||
- name: run command to test state=absent for a database name (expect db_name not in stdout)
|
||||
command: mysql "-e show databases like '{{ db_name }}';"
|
||||
|
@ -96,7 +98,10 @@
|
|||
register: result
|
||||
|
||||
- name: assert output message created a database
|
||||
assert: { that: "result.changed == true" }
|
||||
assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.executed_commands == ["CREATE DATABASE `en{{ db_name }}` CHARACTER SET 'utf8'"]
|
||||
|
||||
- name: test database was created
|
||||
command: mysql "-e SHOW CREATE DATABASE en{{ db_name }};"
|
||||
|
@ -121,7 +126,10 @@
|
|||
register: result
|
||||
|
||||
- name: assert output message that database was created
|
||||
assert: { that: "result.changed == true" }
|
||||
assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.executed_commands == ["CREATE DATABASE `en{{ db_name }}` CHARACTER SET 'binary'"]
|
||||
|
||||
- name: run command to test database was created
|
||||
command: mysql "-e SHOW CREATE DATABASE en{{ db_name }};"
|
||||
|
@ -228,7 +236,10 @@
|
|||
ignore_errors: true
|
||||
|
||||
- name: assert output message that database was deleted using user1
|
||||
assert: { that: "result.changed == true" }
|
||||
assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.executed_commands == ["DROP DATABASE `{{ db_user1 }}`"]
|
||||
|
||||
- name: run command to test database was deleted using user1
|
||||
command: mysql "-e show databases like '{{ db_name }}';"
|
||||
|
|
Loading…
Reference in a new issue