Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added connect_params to allow any additional parameters that libpg understands #329

Merged
merged 19 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- postgresql_* - Add ``connect_params`` parameter dict to allow any additional libpg connection parameters.
Boosai marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions plugins/doc_fragments/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class ModuleDocFragment(object):
- If the file exists, the server's certificate will be verified to be signed by one of these authorities.
type: str
aliases: [ ssl_rootcert ]
connect_params:
description:
- Any additional parameters to be passed to libpg as kwargs.
hunleyd marked this conversation as resolved.
Show resolved Hide resolved
type: dict
Boosai marked this conversation as resolved.
Show resolved Hide resolved
notes:
- The default authentication assumes that you are either logging in as or sudo'ing to the C(postgres) account on the host.
- To avoid "Peer authentication failed for user postgres" error,
Expand Down
7 changes: 6 additions & 1 deletion plugins/module_utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def postgres_common_argument_spec():
port=dict(type='int', default=5432, aliases=['login_port']),
ssl_mode=dict(default='prefer', choices=['allow', 'disable', 'prefer', 'require', 'verify-ca', 'verify-full']),
ca_cert=dict(aliases=['ssl_rootcert']),
connect_params=dict(default={}, type='dict'),
)


Expand Down Expand Up @@ -185,7 +186,7 @@ def get_conn_params(module, params_dict, warn_db_default=True):
"login_password": "password",
"port": "port",
"ssl_mode": "sslmode",
"ca_cert": "sslrootcert"
"ca_cert": "sslrootcert",
}

# Might be different in the modules:
Expand Down Expand Up @@ -223,6 +224,10 @@ def get_conn_params(module, params_dict, warn_db_default=True):
if is_localhost and params_dict["login_unix_socket"] != "":
kw["host"] = params_dict["login_unix_socket"]

# If connect_params is specified, merge it together
if params_dict.get("connect_params"):
kw.update(params_dict["connect_params"])

return kw


Expand Down
11 changes: 11 additions & 0 deletions plugins/modules/postgresql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@
db: test_db
query: INSERT INTO test_table (id, story) VALUES (2, 'my_long_story')

- name: Use connect_params to add any additional connection parameters that libpg supports
community.postgresql.postgresql_query:
connect_params:
target_session_attrs: "read-write"
Boosai marked this conversation as resolved.
Show resolved Hide resolved
connect_timeout: "10"
Boosai marked this conversation as resolved.
Show resolved Hide resolved
login_host: "host1,host2"
login_user: "test"
login_password: "test1234"
db: 'test'
query: 'insert into test (test) values (now())'


# WARNING: The path_to_script and as_single_query options have been deprecated
# and will be removed in community.postgresql 3.0.0, please
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/plugins/module_utils/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_postgres_common_argument_spec(self):
choices=['allow', 'disable', 'prefer', 'require', 'verify-ca', 'verify-full']
),
ca_cert=dict(aliases=['ssl_rootcert']),
connect_params=dict(default={}, type='dict'),
)
assert pg.postgres_common_argument_spec() == expected_dict

Expand Down Expand Up @@ -112,7 +113,7 @@ def __init__(self):
self.extensions = Extensions()

def connect(self, host=None, port=None, user=None,
password=None, sslmode=None, sslrootcert=None):
password=None, sslmode=None, sslrootcert=None, connect_params=None):
if user == 'Exception':
raise Exception()

Expand Down Expand Up @@ -189,7 +190,14 @@ def m_ansible_module():
"""Return an object of dummy AnsibleModule class."""
class DummyAnsibleModule():
def __init__(self):
self.params = pg.postgres_common_argument_spec()

# take default params from argument spec
spec = pg.postgres_common_argument_spec()
params = dict()
for k in spec.keys():
params[k] = spec[k].get('default')

self.params = params
self.err_msg = ''
self.warn_msg = ''

Expand Down