postgresql_db: add executed_commands returned value (#65542)

* postgresql_db: add executed_commands returned value

* add changelog

* fix tests
This commit is contained in:
Andrey Klychkov 2019-12-05 12:53:08 +03:00 committed by Felix Fontein
parent 4352e39989
commit c266fc3b74
4 changed files with 46 additions and 4 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- postgresql_db - add the ``executed_commands`` returned value (https://github.com/ansible/ansible/pull/65542).

View file

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

View file

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

View file

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