postgresql_db: add dump_extra_args parameter (#66717)

* postgresql_db: add dump_extra_args parameter

* add changelog

* fix doc

* fix example
pull/66772/head
Andrew Klychkov 5 years ago committed by jctanner
parent 4d1e21bf18
commit 70017e2679

@ -0,0 +1,2 @@
minor_changes:
- postgresql_db - add ``dump_extra_args`` parameter (https://github.com/ansible/ansible/pull/66717).

@ -107,6 +107,12 @@ options:
explicitly set this to pg_default. explicitly set this to pg_default.
type: path type: path
version_added: '2.9' version_added: '2.9'
dump_extra_args:
description:
- Provides additional arguments when I(state) is C(dump).
- Cannot be used with dump-file-format-related arguments like ``--format=d``.
type: str
version_added: '2.10'
seealso: seealso:
- name: CREATE DATABASE reference - name: CREATE DATABASE reference
description: Complete reference of the CREATE DATABASE command documentation. description: Complete reference of the CREATE DATABASE command documentation.
@ -156,6 +162,13 @@ EXAMPLES = r'''
state: dump state: dump
target: /tmp/acme.sql target: /tmp/acme.sql
- name: Dump an existing database to a file excluding the test table
postgresql_db:
name: acme
state: dump
target: /tmp/acme.sql
dump_extra_args: --exclude-table=test
- name: Dump an existing database to a file (with compression) - name: Dump an existing database to a file (with compression)
postgresql_db: postgresql_db:
name: acme name: acme
@ -350,6 +363,7 @@ def db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype, conn
def db_dump(module, target, target_opts="", def db_dump(module, target, target_opts="",
db=None, db=None,
dump_extra_args=None,
user=None, user=None,
password=None, password=None,
host=None, host=None,
@ -375,6 +389,10 @@ def db_dump(module, target, target_opts="",
comp_prog_path = module.get_bin_path('xz', True) comp_prog_path = module.get_bin_path('xz', True)
cmd += "".join(flags) cmd += "".join(flags)
if dump_extra_args:
cmd += " {0} ".format(dump_extra_args)
if target_opts: if target_opts:
cmd += " {0} ".format(target_opts) cmd += " {0} ".format(target_opts)
@ -509,6 +527,7 @@ def main():
session_role=dict(type='str'), session_role=dict(type='str'),
conn_limit=dict(type='str', default=''), conn_limit=dict(type='str', default=''),
tablespace=dict(type='path', default=''), tablespace=dict(type='path', default=''),
dump_extra_args=dict(type='str', default=None),
) )
module = AnsibleModule( module = AnsibleModule(
@ -530,6 +549,7 @@ def main():
session_role = module.params["session_role"] session_role = module.params["session_role"]
conn_limit = module.params['conn_limit'] conn_limit = module.params['conn_limit']
tablespace = module.params['tablespace'] tablespace = module.params['tablespace']
dump_extra_args = module.params['dump_extra_args']
raw_connection = state in ("dump", "restore") raw_connection = state in ("dump", "restore")
@ -609,7 +629,11 @@ def main():
elif state in ("dump", "restore"): elif state in ("dump", "restore"):
method = state == "dump" and db_dump or db_restore method = state == "dump" and db_dump or db_restore
try: try:
rc, stdout, stderr, cmd = method(module, target, target_opts, db, **kw) if state == 'dump':
rc, stdout, stderr, cmd = method(module, target, target_opts, db, dump_extra_args, **kw)
else:
rc, stdout, stderr, cmd = method(module, target, target_opts, db, **kw)
if rc != 0: if rc != 0:
module.fail_json(msg=stderr, stdout=stdout, rc=rc, cmd=cmd) module.fail_json(msg=stderr, stdout=stdout, rc=rc, cmd=cmd)
else: else:

@ -78,6 +78,7 @@
login_host: '{{(test_fixture == "user")|ternary("localhost", omit)}}' login_host: '{{(test_fixture == "user")|ternary("localhost", omit)}}'
login_password: '{{(test_fixture == "user")|ternary("password", omit)}}' login_password: '{{(test_fixture == "user")|ternary("password", omit)}}'
state: dump state: dump
dump_extra_args: --exclude-table=fake
register: result register: result
become_user: "{{ pg_user }}" become_user: "{{ pg_user }}"
become: yes become: yes
@ -86,6 +87,7 @@
assert: assert:
that: that:
- result is changed - result is changed
- result.executed_commands[0] is search("--exclude-table=fake")
- name: assert database was backed up successfully - name: assert database was backed up successfully
command: file {{ db_file_name }} command: file {{ db_file_name }}

Loading…
Cancel
Save