| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Andrew Klychkov (@Andersson007) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: postgresql_set |
| 14 |
short_description: Change a PostgreSQL server configuration parameter |
| 15 |
deprecated: |
| 16 |
removed_in: "5.0.0" |
| 17 |
why: It has been replaced with a better-designed module. |
| 18 |
alternative: Use M(community.postgresql.postgresql_alter_system) instead. |
| 19 |
description: |
| 20 |
- "IMPORTANT: If your PostgreSQL server version is 14 or later, |
| 21 |
use the M(community.postgresql.postgresql_alter_system) module instead as it will |
| 22 |
replace this module in future." |
| 23 |
- Allows to change a PostgreSQL server configuration parameter. |
| 24 |
- The module uses ALTER SYSTEM command and applies changes by reload server configuration. |
| 25 |
- ALTER SYSTEM is used for changing server configuration parameters across the entire database cluster. |
| 26 |
- It can be more convenient and safe than the traditional method of manually editing the postgresql.conf file. |
| 27 |
- ALTER SYSTEM writes the given parameter setting to the $PGDATA/postgresql.auto.conf file, |
| 28 |
which is read in addition to postgresql.conf. |
| 29 |
- The module allows to reset parameter to boot_val (cluster initial value) by I(reset=true) or remove parameter |
| 30 |
string from postgresql.auto.conf and reload I(value=default) (for settings with postmaster context restart is required). |
| 31 |
- After change you can see in the ansible output the previous and |
| 32 |
the new parameter value and other information using returned values and M(ansible.builtin.debug) module. |
| 33 |
options: |
| 34 |
name: |
| 35 |
description: |
| 36 |
- Name of PostgreSQL server parameter. Pay attention that parameters are case sensitive (see examples below). |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
value: |
| 40 |
description: |
| 41 |
- Parameter value to set. |
| 42 |
- To remove parameter string from postgresql.auto.conf and |
| 43 |
reload the server configuration you must pass I(value=default). |
| 44 |
With I(value=default) the playbook always returns changed is true. |
| 45 |
type: str |
| 46 |
reset: |
| 47 |
description: |
| 48 |
- Restore parameter to initial state (boot_val). Mutually exclusive with I(value). |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
session_role: |
| 52 |
description: |
| 53 |
- Switch to session_role after connecting. The specified session_role must |
| 54 |
be a role that the current login_user is a member of. |
| 55 |
- Permissions checking for SQL commands is carried out as though |
| 56 |
the session_role were the one that had logged in originally. |
| 57 |
type: str |
| 58 |
login_db: |
| 59 |
description: |
| 60 |
- Name of database to connect. |
| 61 |
- The V(db) alias is deprecated and will be removed in version 5.0.0. |
| 62 |
type: str |
| 63 |
aliases: |
| 64 |
- db |
| 65 |
trust_input: |
| 66 |
description: |
| 67 |
- If C(false), check whether values of parameters are potentially dangerous. |
| 68 |
- It makes sense to use C(false) only when SQL injections are possible. |
| 69 |
type: bool |
| 70 |
default: true |
| 71 |
version_added: '0.2.0' |
| 72 |
|
| 73 |
notes: |
| 74 |
- Supported version of PostgreSQL is 9.4 and later. |
| 75 |
For PostgreSQL version 14 or later, the M(community.postgresql.postgresql_alter_system) module is recommended. |
| 76 |
- Pay attention, change setting with 'postmaster' context can return changed is true |
| 77 |
when actually nothing changes because the same value may be presented in |
| 78 |
several different form, for example, 1024MB, 1GB, etc. However in pg_settings |
| 79 |
system view it can be defined like 131072 number of 8kB pages. |
| 80 |
The final check of the parameter value cannot compare it because the server was |
| 81 |
not restarted and the value in pg_settings is not updated yet. |
| 82 |
- For some parameters restart of PostgreSQL server is required. |
| 83 |
See official documentation U(https://www.postgresql.org/docs/current/view-pg-settings.html). |
| 84 |
|
| 85 |
attributes: |
| 86 |
check_mode: |
| 87 |
support: full |
| 88 |
|
| 89 |
seealso: |
| 90 |
- module: community.postgresql.postgresql_alter_system |
| 91 |
- module: community.postgresql.postgresql_info |
| 92 |
- name: PostgreSQL server configuration |
| 93 |
description: General information about PostgreSQL server configuration. |
| 94 |
link: https://www.postgresql.org/docs/current/runtime-config.html |
| 95 |
- name: PostgreSQL view pg_settings reference |
| 96 |
description: Complete reference of the pg_settings view documentation. |
| 97 |
link: https://www.postgresql.org/docs/current/view-pg-settings.html |
| 98 |
- name: PostgreSQL ALTER SYSTEM command reference |
| 99 |
description: Complete reference of the ALTER SYSTEM command documentation. |
| 100 |
link: https://www.postgresql.org/docs/current/sql-altersystem.html |
| 101 |
|
| 102 |
author: |
| 103 |
- Andrew Klychkov (@Andersson007) |
| 104 |
- Daniele Giudice (@RealGreenDragon) |
| 105 |
|
| 106 |
extends_documentation_fragment: |
| 107 |
- community.postgresql.postgres |
| 108 |
''' |
| 109 |
|
| 110 |
EXAMPLES = r''' |
| 111 |
- name: Restore wal_keep_segments parameter to initial state |
| 112 |
community.postgresql.postgresql_set: |
| 113 |
name: wal_keep_segments |
| 114 |
reset: true |
| 115 |
|
| 116 |
# Set work_mem parameter to 32MB and show what's been changed and restart is required or not |
| 117 |
# (output example: "msg": "work_mem 4MB >> 64MB restart_req: False") |
| 118 |
- name: Set work mem parameter |
| 119 |
community.postgresql.postgresql_set: |
| 120 |
name: work_mem |
| 121 |
value: 32mb |
| 122 |
register: set |
| 123 |
|
| 124 |
- name: Print the result if the setting changed |
| 125 |
ansible.builtin.debug: |
| 126 |
msg: "{{ set.name }} {{ set.prev_val_pretty }} >> {{ set.value_pretty }} restart_req: {{ set.restart_required }}" |
| 127 |
when: set.changed |
| 128 |
# Ensure that the restart of PostgreSQL server must be required for some parameters. |
| 129 |
# In this situation you see the same parameter in prev_val_pretty and value_pretty, but 'changed=True' |
| 130 |
# (If you passed the value that was different from the current server setting). |
| 131 |
|
| 132 |
- name: Set log_min_duration_statement parameter to 1 second |
| 133 |
community.postgresql.postgresql_set: |
| 134 |
name: log_min_duration_statement |
| 135 |
value: 1s |
| 136 |
|
| 137 |
- name: Set wal_log_hints parameter to default value (remove parameter from postgresql.auto.conf) |
| 138 |
community.postgresql.postgresql_set: |
| 139 |
name: wal_log_hints |
| 140 |
value: default |
| 141 |
|
| 142 |
- name: Set TimeZone parameter (careful, case sensitive) |
| 143 |
community.postgresql.postgresql_set: |
| 144 |
name: TimeZone |
| 145 |
value: 'Europe/Paris' |
| 146 |
''' |
| 147 |
|
| 148 |
RETURN = r''' |
| 149 |
name: |
| 150 |
description: Name of PostgreSQL server parameter. |
| 151 |
returned: success |
| 152 |
type: str |
| 153 |
sample: 'shared_buffers' |
| 154 |
restart_required: |
| 155 |
description: Information about parameter current state. |
| 156 |
returned: success |
| 157 |
type: bool |
| 158 |
sample: true |
| 159 |
prev_val_pretty: |
| 160 |
description: Information about previous state of the parameter. |
| 161 |
returned: success |
| 162 |
type: str |
| 163 |
sample: '4MB' |
| 164 |
value_pretty: |
| 165 |
description: Information about current state of the parameter. |
| 166 |
returned: success |
| 167 |
type: str |
| 168 |
sample: '64MB' |
| 169 |
value: |
| 170 |
description: |
| 171 |
- Dictionary that contains the current parameter value (at the time of playbook finish). |
| 172 |
- Pay attention that for real change some parameters restart of PostgreSQL server is required. |
| 173 |
- Returns the current value in the check mode. |
| 174 |
returned: success |
| 175 |
type: dict |
| 176 |
sample: { "value": 67108864, "unit": "b" } |
| 177 |
context: |
| 178 |
description: |
| 179 |
- PostgreSQL setting context. |
| 180 |
returned: success |
| 181 |
type: str |
| 182 |
sample: user |
| 183 |
queries: |
| 184 |
description: |
| 185 |
- List of executed DML queries. |
| 186 |
returned: success |
| 187 |
type: list |
| 188 |
elements: str |
| 189 |
sample: ["ALTER SYSTEM SET shared_preload_libraries = ''"] |
| 190 |
version_added: '3.7.0' |
| 191 |
''' |
| 192 |
|
| 193 |
from copy import deepcopy |
| 194 |
|
| 195 |
from ansible.module_utils.common.text.converters import to_native |
| 196 |
from ansible.module_utils.basic import AnsibleModule |
| 197 |
from ansible_collections.community.postgresql.plugins.module_utils.database import \ |
| 198 |
check_input |
| 199 |
from ansible_collections.community.postgresql.plugins.module_utils.postgres import ( |
| 200 |
connect_to_db, |
| 201 |
ensure_required_libs, |
| 202 |
get_conn_params, |
| 203 |
get_server_version, |
| 204 |
pg_cursor_args, |
| 205 |
postgres_common_argument_spec, |
| 206 |
) |
| 207 |
|
| 208 |
executed_queries = [] |
| 209 |
|
| 210 |
PG_REQ_VER = 90400 |
| 211 |
|
| 212 |
# To allow to set value like 1mb instead of 1MB, etc: |
| 213 |
LOWERCASE_SIZE_UNITS = ("mb", "gb", "tb") |
| 214 |
|
| 215 |
# GUC_LIST_QUOTE parameters list for each version where they changed (from PG_REQ_VER). |
| 216 |
# It is a tuple of tuples as we need to iterate it in order. |
| 217 |
PARAMETERS_GUC_LIST_QUOTE = ( |
| 218 |
(140000, ( |
| 219 |
'local_preload_libraries', |
| 220 |
'search_path', |
| 221 |
'session_preload_libraries', |
| 222 |
'shared_preload_libraries', |
| 223 |
'temp_tablespaces', |
| 224 |
'unix_socket_directories' |
| 225 |
)), |
| 226 |
(90400, ( |
| 227 |
'local_preload_libraries', |
| 228 |
'search_path', |
| 229 |
'session_preload_libraries', |
| 230 |
'shared_preload_libraries', |
| 231 |
'temp_tablespaces' |
| 232 |
)), |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
# =========================================== |
| 237 |
# PostgreSQL module specific support methods. |
| 238 |
# |
| 239 |
|
| 240 |
|
| 241 |
def param_is_guc_list_quote(server_version, name): |
| 242 |
for guc_list_quote_ver, guc_list_quote_params in PARAMETERS_GUC_LIST_QUOTE: |
| 243 |
if server_version >= guc_list_quote_ver: |
| 244 |
return name in guc_list_quote_params |
| 245 |
return False |
| 246 |
|
| 247 |
|
| 248 |
def param_guc_list_unquote(value): |
| 249 |
# Unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) |
| 250 |
# Assume the parameter is GUC_LIST_QUOTE (check in param_is_guc_list_quote function) |
| 251 |
return ', '.join([v.strip('" ') for v in value.split(',')]) |
| 252 |
|
| 253 |
|
| 254 |
def param_get(cursor, module, name, is_guc_list_quote): |
| 255 |
query = ("SELECT name, setting, unit, context, boot_val " |
| 256 |
"FROM pg_settings WHERE name = %(name)s") |
| 257 |
try: |
| 258 |
cursor.execute(query, {'name': name}) |
| 259 |
info = cursor.fetchone() |
| 260 |
cursor.execute("SHOW %s" % name) |
| 261 |
val = cursor.fetchone() |
| 262 |
|
| 263 |
except Exception as e: |
| 264 |
module.fail_json(msg="Unable to get %s value due to : %s" % (name, to_native(e))) |
| 265 |
|
| 266 |
if not info: |
| 267 |
module.fail_json(msg="No such parameter: %s. " |
| 268 |
"Please check its spelling or presence in your PostgreSQL version " |
| 269 |
"(https://www.postgresql.org/docs/current/runtime-config.html)" % name) |
| 270 |
|
| 271 |
current_val = val[name] |
| 272 |
raw_val = info['setting'] |
| 273 |
unit = info['unit'] |
| 274 |
context = info['context'] |
| 275 |
boot_val = info['boot_val'] |
| 276 |
|
| 277 |
if current_val == 'True': |
| 278 |
current_val = 'on' |
| 279 |
elif current_val == 'False': |
| 280 |
current_val = 'off' |
| 281 |
|
| 282 |
if unit == 'kB': |
| 283 |
if int(raw_val) > 0: |
| 284 |
raw_val = int(raw_val) * 1024 |
| 285 |
if int(boot_val) > 0: |
| 286 |
boot_val = int(boot_val) * 1024 |
| 287 |
|
| 288 |
unit = 'b' |
| 289 |
|
| 290 |
elif unit == 'MB': |
| 291 |
if int(raw_val) > 0: |
| 292 |
raw_val = int(raw_val) * 1024 * 1024 |
| 293 |
if int(boot_val) > 0: |
| 294 |
boot_val = int(boot_val) * 1024 * 1024 |
| 295 |
|
| 296 |
unit = 'b' |
| 297 |
|
| 298 |
if is_guc_list_quote: |
| 299 |
current_val = param_guc_list_unquote(current_val) |
| 300 |
raw_val = param_guc_list_unquote(raw_val) |
| 301 |
|
| 302 |
return { |
| 303 |
'current_val': current_val, |
| 304 |
'raw_val': raw_val, |
| 305 |
'unit': unit, |
| 306 |
'boot_val': boot_val, |
| 307 |
'context': context, |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
def pretty_to_bytes(pretty_val): |
| 312 |
# The function returns a value in bytes |
| 313 |
# if the value contains 'B', 'kB', 'MB', 'GB', 'TB'. |
| 314 |
# Otherwise it returns the passed argument. |
| 315 |
|
| 316 |
# It's sometimes possible to have an empty values |
| 317 |
if not pretty_val: |
| 318 |
return pretty_val |
| 319 |
|
| 320 |
# If the first char is not a digit, it does not make sense |
| 321 |
# to parse further, so just return a passed value |
| 322 |
if not pretty_val[0].isdigit(): |
| 323 |
return pretty_val |
| 324 |
|
| 325 |
# If the last char is not an alphabetical symbol, it means that |
| 326 |
# it does not contain any suffixes, so no sense to parse further |
| 327 |
if not pretty_val[-1].isalpha(): |
| 328 |
try: |
| 329 |
pretty_val = int(pretty_val) |
| 330 |
|
| 331 |
except ValueError: |
| 332 |
try: |
| 333 |
pretty_val = float(pretty_val) |
| 334 |
|
| 335 |
except ValueError: |
| 336 |
return pretty_val |
| 337 |
|
| 338 |
return pretty_val |
| 339 |
|
| 340 |
# Extract digits |
| 341 |
num_part = [] |
| 342 |
for c in pretty_val: |
| 343 |
# When we reach the first non-digit element, |
| 344 |
# e.g. in 1024kB, stop iterating |
| 345 |
if not c.isdigit(): |
| 346 |
break |
| 347 |
else: |
| 348 |
num_part.append(c) |
| 349 |
|
| 350 |
num_part = int(''.join(num_part)) |
| 351 |
|
| 352 |
val_in_bytes = None |
| 353 |
|
| 354 |
if len(pretty_val) >= 2: |
| 355 |
if 'kB' in pretty_val[-2:]: |
| 356 |
val_in_bytes = num_part * 1024 |
| 357 |
|
| 358 |
elif 'MB' in pretty_val[-2:]: |
| 359 |
val_in_bytes = num_part * 1024 * 1024 |
| 360 |
|
| 361 |
elif 'GB' in pretty_val[-2:]: |
| 362 |
val_in_bytes = num_part * 1024 * 1024 * 1024 |
| 363 |
|
| 364 |
elif 'TB' in pretty_val[-2:]: |
| 365 |
val_in_bytes = num_part * 1024 * 1024 * 1024 * 1024 |
| 366 |
|
| 367 |
# For cases like "1B" |
| 368 |
if not val_in_bytes and 'B' in pretty_val[-1]: |
| 369 |
val_in_bytes = num_part |
| 370 |
|
| 371 |
if val_in_bytes is not None: |
| 372 |
return val_in_bytes |
| 373 |
else: |
| 374 |
return pretty_val |
| 375 |
|
| 376 |
|
| 377 |
def param_set(cursor, module, name, value, server_version): |
| 378 |
if isinstance(value, str) and ',' in value and \ |
| 379 |
not name.endswith(('_command', '_prefix')) and \ |
| 380 |
not (server_version < 140000 and name == 'unix_socket_directories'): |
| 381 |
# Issue https://github.com/ansible-collections/community.postgresql/issues/78 |
| 382 |
# Change value from 'one, two, three' -> "'one','two','three'" |
| 383 |
# PR https://github.com/ansible-collections/community.postgresql/pull/400 |
| 384 |
# Parameter names ends with '_command' or '_prefix' can contains commas but are not lists |
| 385 |
# PR https://github.com/ansible-collections/community.postgresql/pull/521 |
| 386 |
# unix_socket_directories up to PostgreSQL 13 lacks GUC_LIST_INPUT and |
| 387 |
# GUC_LIST_QUOTE options so it is a single value parameter |
| 388 |
value = ','.join(["'" + elem.strip() + "'" for elem in value.split(',')]) |
| 389 |
query = "ALTER SYSTEM SET %s = %s" % (name, value) |
| 390 |
else: |
| 391 |
query = "ALTER SYSTEM SET %s = '%s'" % (name, value) |
| 392 |
|
| 393 |
fail_msg = "Unable to set %s value due to : %s" |
| 394 |
return exec_set_sql(cursor, module, query, name, fail_msg) |
| 395 |
|
| 396 |
|
| 397 |
def param_set_default(cursor, module, name): |
| 398 |
query = "ALTER SYSTEM SET %s = DEFAULT" % name |
| 399 |
fail_msg = "Unable to set default value for %s due to : %s" |
| 400 |
return exec_set_sql(cursor, module, query, name, fail_msg) |
| 401 |
|
| 402 |
|
| 403 |
def param_reset(cursor, module, name): |
| 404 |
query = "ALTER SYSTEM RESET %s" % name |
| 405 |
fail_msg = "Unable to reset %s due to : %s" |
| 406 |
return exec_set_sql(cursor, module, query, name, fail_msg) |
| 407 |
|
| 408 |
|
| 409 |
def exec_set_sql(cursor, module, query, setting_name, fail_msg): |
| 410 |
try: |
| 411 |
executed_queries.append(query) |
| 412 |
cursor.execute(query) |
| 413 |
except Exception as e: |
| 414 |
module.fail_json(msg=fail_msg % (setting_name, to_native(e))) |
| 415 |
return True |
| 416 |
|
| 417 |
|
| 418 |
# =========================================== |
| 419 |
# Module execution. |
| 420 |
# |
| 421 |
|
| 422 |
|
| 423 |
def main(): |
| 424 |
argument_spec = postgres_common_argument_spec() |
| 425 |
argument_spec.update( |
| 426 |
name=dict(type='str', required=True), |
| 427 |
login_db=dict(type='str', aliases=['db'], deprecated_aliases=[ |
| 428 |
{ |
| 429 |
'name': 'db', |
| 430 |
'version': '5.0.0', |
| 431 |
'collection_name': 'community.postgresql', |
| 432 |
}], |
| 433 |
), |
| 434 |
value=dict(type='str'), |
| 435 |
reset=dict(type='bool', default=False), |
| 436 |
session_role=dict(type='str'), |
| 437 |
trust_input=dict(type='bool', default=True), |
| 438 |
) |
| 439 |
module = AnsibleModule( |
| 440 |
argument_spec=argument_spec, |
| 441 |
supports_check_mode=True, |
| 442 |
) |
| 443 |
|
| 444 |
name = module.params['name'] |
| 445 |
value = module.params['value'] |
| 446 |
reset = module.params['reset'] |
| 447 |
session_role = module.params['session_role'] |
| 448 |
trust_input = module.params['trust_input'] |
| 449 |
|
| 450 |
if not trust_input: |
| 451 |
# Check input for potentially dangerous elements: |
| 452 |
check_input(module, name, value, session_role) |
| 453 |
|
| 454 |
if value: |
| 455 |
# Convert a value like 1mb (Postgres does not support) to 1MB, etc: |
| 456 |
if len(value) > 2 and value[:-2].isdigit() and value[-2:] in LOWERCASE_SIZE_UNITS: |
| 457 |
value = value.upper() |
| 458 |
|
| 459 |
# Convert a value like 1b (Postgres does not support) to 1B: |
| 460 |
elif len(value) > 1 and ('b' in value[-1] and value[:-1].isdigit()): |
| 461 |
value = value.upper() |
| 462 |
|
| 463 |
if name == 'shared_preload_libraries' and value == '': |
| 464 |
msg = ("Due to a PostgreSQL bug in resetting shared_preload_libraries " |
| 465 |
"with ALTER SYSTEM SET, setting it as an empty string " |
| 466 |
"is not supported by the module to avoid crashes. " |
| 467 |
"Use the `value: default` or `reset: true` argument instead. " |
| 468 |
"If you think the bug has been fixed, please let us know.") |
| 469 |
module.fail_json(msg=msg) |
| 470 |
|
| 471 |
if value is not None and reset: |
| 472 |
module.fail_json(msg="%s: value and reset params are mutually exclusive" % name) |
| 473 |
|
| 474 |
if value is None and not reset: |
| 475 |
module.fail_json(msg="%s: at least one of value or reset param must be specified" % name) |
| 476 |
|
| 477 |
# Ensure psycopg libraries are available before connecting to DB: |
| 478 |
ensure_required_libs(module) |
| 479 |
conn_params = get_conn_params(module, module.params, warn_db_default=False) |
| 480 |
db_connection, dummy = connect_to_db(module, conn_params, autocommit=True) |
| 481 |
cursor = db_connection.cursor(**pg_cursor_args) |
| 482 |
|
| 483 |
kw = {} |
| 484 |
# Check server version (needs 9.4 or later): |
| 485 |
ver = get_server_version(db_connection) |
| 486 |
if ver < PG_REQ_VER: |
| 487 |
module.warn("PostgreSQL is %s version but %s or later is required" % (ver, PG_REQ_VER)) |
| 488 |
kw = dict( |
| 489 |
changed=False, |
| 490 |
restart_required=False, |
| 491 |
value_pretty="", |
| 492 |
prev_val_pretty="", |
| 493 |
value={"value": "", "unit": ""}, |
| 494 |
) |
| 495 |
kw['name'] = name |
| 496 |
db_connection.close() |
| 497 |
module.exit_json(**kw) |
| 498 |
|
| 499 |
# Check parameter is GUC_LIST_QUOTE (done once as depend only on server version) |
| 500 |
is_guc_list_quote = param_is_guc_list_quote(ver, name) |
| 501 |
|
| 502 |
# Set default returned values: |
| 503 |
restart_required = False |
| 504 |
changed = False |
| 505 |
kw['name'] = name |
| 506 |
kw['restart_required'] = False |
| 507 |
|
| 508 |
# Get info about param state: |
| 509 |
res = param_get(cursor, module, name, is_guc_list_quote) |
| 510 |
current_val = res['current_val'] |
| 511 |
raw_val = res['raw_val'] |
| 512 |
unit = res['unit'] |
| 513 |
boot_val = res['boot_val'] |
| 514 |
context = res['context'] |
| 515 |
|
| 516 |
if value == 'True': |
| 517 |
value = 'on' |
| 518 |
elif value == 'False': |
| 519 |
value = 'off' |
| 520 |
|
| 521 |
kw['prev_val_pretty'] = current_val |
| 522 |
kw['value_pretty'] = deepcopy(kw['prev_val_pretty']) |
| 523 |
kw['context'] = context |
| 524 |
|
| 525 |
# Do job |
| 526 |
if context == "internal": |
| 527 |
module.fail_json(msg="%s: cannot be changed (internal context). See " |
| 528 |
"https://www.postgresql.org/docs/current/runtime-config-preset.html" % name) |
| 529 |
|
| 530 |
if context == "postmaster": |
| 531 |
restart_required = True |
| 532 |
|
| 533 |
# If check_mode, just compare and exit: |
| 534 |
if module.check_mode: |
| 535 |
if pretty_to_bytes(value) == pretty_to_bytes(current_val): |
| 536 |
kw['changed'] = False |
| 537 |
|
| 538 |
else: |
| 539 |
kw['value_pretty'] = value |
| 540 |
kw['changed'] = True |
| 541 |
|
| 542 |
# Anyway returns current raw value in the check_mode: |
| 543 |
kw['value'] = dict( |
| 544 |
value=raw_val, |
| 545 |
unit=unit, |
| 546 |
) |
| 547 |
kw['restart_required'] = restart_required |
| 548 |
kw['queries'] = executed_queries |
| 549 |
module.exit_json(**kw) |
| 550 |
|
| 551 |
# Set param (value can be an empty string): |
| 552 |
if value is not None and value != current_val: |
| 553 |
if value == 'default': |
| 554 |
changed = param_set_default(cursor, module, name) |
| 555 |
else: |
| 556 |
changed = param_set(cursor, module, name, value, ver) |
| 557 |
|
| 558 |
kw['value_pretty'] = value |
| 559 |
|
| 560 |
# Reset param: |
| 561 |
elif reset: |
| 562 |
if raw_val == boot_val: |
| 563 |
# nothing to change, exit: |
| 564 |
kw['value'] = dict( |
| 565 |
value=raw_val, |
| 566 |
unit=unit, |
| 567 |
) |
| 568 |
kw['queries'] = executed_queries |
| 569 |
module.exit_json(**kw) |
| 570 |
|
| 571 |
changed = param_reset(cursor, module, name) |
| 572 |
|
| 573 |
# When the setting can be changed w/o restart, apply it |
| 574 |
if not module.check_mode and changed and context != 'postmaster': |
| 575 |
cursor.execute("SELECT pg_reload_conf()") |
| 576 |
|
| 577 |
cursor.close() |
| 578 |
db_connection.close() |
| 579 |
|
| 580 |
# Reconnect and recheck current value: |
| 581 |
if context in ('sighup', 'superuser-backend', 'backend', 'superuser', 'user'): |
| 582 |
db_connection, dummy = connect_to_db(module, conn_params, autocommit=True) |
| 583 |
cursor = db_connection.cursor(**pg_cursor_args) |
| 584 |
|
| 585 |
res = param_get(cursor, module, name, is_guc_list_quote) |
| 586 |
# f_ means 'final' |
| 587 |
f_value = res['current_val'] |
| 588 |
f_raw_val = res['raw_val'] |
| 589 |
|
| 590 |
if raw_val == f_raw_val: |
| 591 |
changed = False |
| 592 |
|
| 593 |
else: |
| 594 |
changed = True |
| 595 |
|
| 596 |
kw['value_pretty'] = f_value |
| 597 |
kw['value'] = dict( |
| 598 |
value=f_raw_val, |
| 599 |
unit=unit, |
| 600 |
) |
| 601 |
|
| 602 |
cursor.close() |
| 603 |
db_connection.close() |
| 604 |
|
| 605 |
kw['changed'] = changed |
| 606 |
kw['restart_required'] = restart_required |
| 607 |
kw['queries'] = executed_queries |
| 608 |
|
| 609 |
if restart_required and changed: |
| 610 |
module.warn("Restart of PostgreSQL is required for setting %s" % name) |
| 611 |
|
| 612 |
module.exit_json(**kw) |
| 613 |
|
| 614 |
|
| 615 |
if __name__ == '__main__': |
| 616 |
main() |