From c266fc3b74665fd7313b84f2c0a050024151475c Mon Sep 17 00:00:00 2001 From: Andrey Klychkov Date: Thu, 5 Dec 2019 12:53:08 +0300 Subject: [PATCH] postgresql_db: add executed_commands returned value (#65542) * postgresql_db: add executed_commands returned value * add changelog * fix tests --- ...ql_db_add_executed_commands_return_val.yml | 2 ++ .../database/postgresql/postgresql_db.py | 25 ++++++++++++++++--- .../tasks/postgresql_db_general.yml | 1 + .../tasks/postgresql_db_initial.yml | 22 +++++++++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/65542-postgresql_db_add_executed_commands_return_val.yml diff --git a/changelogs/fragments/65542-postgresql_db_add_executed_commands_return_val.yml b/changelogs/fragments/65542-postgresql_db_add_executed_commands_return_val.yml new file mode 100644 index 00000000000..51b3a09fc4b --- /dev/null +++ b/changelogs/fragments/65542-postgresql_db_add_executed_commands_return_val.yml @@ -0,0 +1,2 @@ +minor_changes: +- postgresql_db - add the ``executed_commands`` returned value (https://github.com/ansible/ansible/pull/65542). diff --git a/lib/ansible/modules/database/postgresql/postgresql_db.py b/lib/ansible/modules/database/postgresql/postgresql_db.py index 2fdfe7bb1a7..2fd4ce95dce 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_db.py +++ b/lib/ansible/modules/database/postgresql/postgresql_db.py @@ -178,6 +178,16 @@ EXAMPLES = r''' tablespace: bar ''' +RETURN = r''' +executed_commands: + description: List of commands which tried to run. + returned: always + type: list + sample: ["CREATE DATABASE acme"] + version_added: '2.10' +''' + + import os import subprocess import traceback @@ -197,6 +207,8 @@ from ansible.module_utils.six import iteritems from ansible.module_utils.six.moves import shlex_quote from ansible.module_utils._text import to_native +executed_commands = [] + class NotSupportedError(Exception): pass @@ -210,6 +222,7 @@ def set_owner(cursor, db, owner): query = 'ALTER DATABASE %s OWNER TO "%s"' % ( pg_quote_identifier(db, 'database'), owner) + executed_commands.append(query) cursor.execute(query) return True @@ -218,6 +231,7 @@ def set_conn_limit(cursor, db, conn_limit): query = "ALTER DATABASE %s CONNECTION LIMIT %s" % ( pg_quote_identifier(db, 'database'), conn_limit) + executed_commands.append(query) cursor.execute(query) return True @@ -252,6 +266,7 @@ def db_exists(cursor, db): def db_delete(cursor, db): if db_exists(cursor, db): query = "DROP DATABASE %s" % pg_quote_identifier(db, 'database') + executed_commands.append(query) cursor.execute(query) return True else: @@ -277,6 +292,7 @@ def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_ if conn_limit: query_fragments.append("CONNECTION LIMIT %(conn_limit)s" % {"conn_limit": conn_limit}) query = ' '.join(query_fragments) + executed_commands.append(cursor.mogrify(query, params)) cursor.execute(query, params) return True else: @@ -459,6 +475,7 @@ def do_with_password(module, cmd, password): env = {} if password: env = {"PGPASSWORD": password} + executed_commands.append(cmd) rc, stderr, stdout = module.run_command(cmd, use_unsafe_shell=True, environ_update=env) return rc, stderr, stdout, cmd @@ -467,6 +484,7 @@ def set_tablespace(cursor, db, tablespace): query = "ALTER DATABASE %s SET TABLESPACE %s" % ( pg_quote_identifier(db, 'database'), pg_quote_identifier(tablespace, 'tablespace')) + executed_commands.append(query) cursor.execute(query) return True @@ -574,7 +592,7 @@ def main(): changed = db_exists(cursor, db) elif state == "present": changed = not db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn_limit, tablespace) - module.exit_json(changed=changed, db=db) + module.exit_json(changed=changed, db=db, executed_commands=executed_commands) if state == "absent": try: @@ -595,7 +613,8 @@ def main(): if rc != 0: module.fail_json(msg=stderr, stdout=stdout, rc=rc, cmd=cmd) else: - module.exit_json(changed=True, msg=stdout, stderr=stderr, rc=rc, cmd=cmd) + module.exit_json(changed=True, msg=stdout, stderr=stderr, rc=rc, cmd=cmd, + executed_commands=executed_commands) except SQLParseError as e: module.fail_json(msg=to_native(e), exception=traceback.format_exc()) @@ -607,7 +626,7 @@ def main(): except Exception as e: module.fail_json(msg="Database query failed: %s" % to_native(e), exception=traceback.format_exc()) - module.exit_json(changed=changed, db=db) + module.exit_json(changed=changed, db=db, executed_commands=executed_commands) if __name__ == '__main__': diff --git a/test/integration/targets/postgresql_db/tasks/postgresql_db_general.yml b/test/integration/targets/postgresql_db/tasks/postgresql_db_general.yml index f47c521127b..0733808d91d 100644 --- a/test/integration/targets/postgresql_db/tasks/postgresql_db_general.yml +++ b/test/integration/targets/postgresql_db/tasks/postgresql_db_general.yml @@ -89,6 +89,7 @@ - assert: that: - result is changed + - result.executed_commands == ['CREATE DATABASE "{{ db_name }}" TABLESPACE "{{ db_tablespace }}"'] - name: postgresql_db_tablespace - Check actual DB tablespace, rowcount must be 1 <<: *task_parameters diff --git a/test/integration/targets/postgresql_db/tasks/postgresql_db_initial.yml b/test/integration/targets/postgresql_db/tasks/postgresql_db_initial.yml index 16f5a66d29c..851c19f4fce 100644 --- a/test/integration/targets/postgresql_db/tasks/postgresql_db_initial.yml +++ b/test/integration/targets/postgresql_db/tasks/postgresql_db_initial.yml @@ -14,7 +14,8 @@ assert: that: - result is changed - - "result.db == db_name" + - result.db == "{{ db_name }}" + - result.executed_commands == ['CREATE DATABASE "{{ db_name }}"'] - name: Check that database created become_user: "{{ pg_user }}" @@ -53,6 +54,7 @@ assert: that: - result is changed + - result.executed_commands == ['DROP DATABASE "{{ db_name }}"'] - name: Check that database was destroyed become_user: "{{ pg_user }}" @@ -115,6 +117,12 @@ lc_ctype: 'es_ES{{ locale_latin_suffix }}' template: 'template0' login_user: "{{ pg_user }}" + register: result + +- assert: + that: + - result is changed + - result.executed_commands == ["CREATE DATABASE \"{{ db_name }}\" TEMPLATE \"template0\" ENCODING 'LATIN1' LC_COLLATE 'pt_BR{{ locale_latin_suffix }}' LC_CTYPE 'es_ES{{ locale_latin_suffix }}' CONNECTION LIMIT 100"] or result.executed_commands == ["CREATE DATABASE \"{{ db_name }}\" TEMPLATE \"template0\" ENCODING E'LATIN1' LC_COLLATE E'pt_BR{{ locale_latin_suffix }}' LC_CTYPE E'es_ES{{ locale_latin_suffix }}' CONNECTION LIMIT 100"] - name: Check that the DB has all of our options become_user: "{{ pg_user }}" @@ -186,6 +194,7 @@ - assert: that: - result is changed + - result.executed_commands == ['ALTER DATABASE "{{ db_name }}" CONNECTION LIMIT 200'] - name: Check that conn_limit has actually been set / updated to 200 become_user: "{{ pg_user }}" @@ -239,6 +248,12 @@ state: "present" owner: "{{ db_user1 }}" login_user: "{{ pg_user }}" + register: result + +- assert: + that: + - result is changed + - result.executed_commands == ['CREATE DATABASE "{{ db_name }}" OWNER "{{ db_user1 }}"'] - name: Check that the user owns the newly created DB become_user: "{{ pg_user }}" @@ -266,6 +281,11 @@ login_user: "{{ pg_user }}" register: result +- assert: + that: + - result is changed + - result.executed_commands == ['ALTER DATABASE "{{ db_name }}" OWNER TO "{{ db_user2 }}"'] + - name: Check the previous step become_user: "{{ pg_user }}" become: yes