diff --git a/changelogs/fragments/66717-postgresql_db_add_dump_extra_args_param.yml b/changelogs/fragments/66717-postgresql_db_add_dump_extra_args_param.yml new file mode 100644 index 00000000000..84a5296e7e8 --- /dev/null +++ b/changelogs/fragments/66717-postgresql_db_add_dump_extra_args_param.yml @@ -0,0 +1,2 @@ +minor_changes: +- postgresql_db - add ``dump_extra_args`` parameter (https://github.com/ansible/ansible/pull/66717). diff --git a/lib/ansible/modules/database/postgresql/postgresql_db.py b/lib/ansible/modules/database/postgresql/postgresql_db.py index 2fd4ce95dce..40858d9974b 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_db.py +++ b/lib/ansible/modules/database/postgresql/postgresql_db.py @@ -107,6 +107,12 @@ options: explicitly set this to pg_default. type: path 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: - name: CREATE DATABASE reference description: Complete reference of the CREATE DATABASE command documentation. @@ -156,6 +162,13 @@ EXAMPLES = r''' state: dump 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) postgresql_db: 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="", db=None, + dump_extra_args=None, user=None, password=None, host=None, @@ -375,6 +389,10 @@ def db_dump(module, target, target_opts="", comp_prog_path = module.get_bin_path('xz', True) cmd += "".join(flags) + + if dump_extra_args: + cmd += " {0} ".format(dump_extra_args) + if target_opts: cmd += " {0} ".format(target_opts) @@ -509,6 +527,7 @@ def main(): session_role=dict(type='str'), conn_limit=dict(type='str', default=''), tablespace=dict(type='path', default=''), + dump_extra_args=dict(type='str', default=None), ) module = AnsibleModule( @@ -530,6 +549,7 @@ def main(): session_role = module.params["session_role"] conn_limit = module.params['conn_limit'] tablespace = module.params['tablespace'] + dump_extra_args = module.params['dump_extra_args'] raw_connection = state in ("dump", "restore") @@ -609,7 +629,11 @@ def main(): elif state in ("dump", "restore"): method = state == "dump" and db_dump or db_restore 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: module.fail_json(msg=stderr, stdout=stdout, rc=rc, cmd=cmd) else: diff --git a/test/integration/targets/postgresql_db/tasks/state_dump_restore.yml b/test/integration/targets/postgresql_db/tasks/state_dump_restore.yml index 882e5c60486..4cc86cce31e 100644 --- a/test/integration/targets/postgresql_db/tasks/state_dump_restore.yml +++ b/test/integration/targets/postgresql_db/tasks/state_dump_restore.yml @@ -78,6 +78,7 @@ login_host: '{{(test_fixture == "user")|ternary("localhost", omit)}}' login_password: '{{(test_fixture == "user")|ternary("password", omit)}}' state: dump + dump_extra_args: --exclude-table=fake register: result become_user: "{{ pg_user }}" become: yes @@ -86,6 +87,7 @@ assert: that: - result is changed + - result.executed_commands[0] is search("--exclude-table=fake") - name: assert database was backed up successfully command: file {{ db_file_name }}