| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, John Scalia (@jscalia), 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_slot |
| 14 |
short_description: Add or remove replication slots from a PostgreSQL database |
| 15 |
description: |
| 16 |
- Add or remove physical or logical replication slots from a PostgreSQL database. |
| 17 |
|
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- Name of the replication slot to add or remove. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
aliases: |
| 25 |
- slot_name |
| 26 |
slot_type: |
| 27 |
description: |
| 28 |
- Slot type. |
| 29 |
type: str |
| 30 |
default: physical |
| 31 |
choices: [ logical, physical ] |
| 32 |
state: |
| 33 |
description: |
| 34 |
- The slot state. |
| 35 |
- I(state=present) implies the slot must be present in the system. |
| 36 |
- I(state=absent) implies the I(groups) must be revoked from I(target_roles). |
| 37 |
type: str |
| 38 |
default: present |
| 39 |
choices: [ absent, present ] |
| 40 |
immediately_reserve: |
| 41 |
description: |
| 42 |
- Optional parameter that when C(true) specifies that the LSN for this replication slot be reserved |
| 43 |
immediately, otherwise the default, C(false), specifies that the LSN is reserved on the first connection |
| 44 |
from a streaming replication client. |
| 45 |
- Is available from PostgreSQL version 9.6. |
| 46 |
- Uses only with I(slot_type=physical). |
| 47 |
- Mutually exclusive with I(slot_type=logical). |
| 48 |
type: bool |
| 49 |
default: false |
| 50 |
output_plugin: |
| 51 |
description: |
| 52 |
- All logical slots must indicate which output plugin decoder they're using. |
| 53 |
- This parameter does not apply to physical slots. |
| 54 |
- It will be ignored with I(slot_type=physical). |
| 55 |
type: str |
| 56 |
default: "test_decoding" |
| 57 |
login_db: |
| 58 |
description: |
| 59 |
- Name of database to connect to. |
| 60 |
- The V(db) alias is deprecated and will be removed in version 5.0.0. |
| 61 |
type: str |
| 62 |
aliases: |
| 63 |
- db |
| 64 |
session_role: |
| 65 |
description: |
| 66 |
- Switch to session_role after connecting. |
| 67 |
The specified session_role must be a role that the current login_user is a member of. |
| 68 |
- Permissions checking for SQL commands is carried out as though |
| 69 |
the session_role were the one that had logged in originally. |
| 70 |
type: str |
| 71 |
trust_input: |
| 72 |
description: |
| 73 |
- If C(false), check the value of I(session_role) is potentially dangerous. |
| 74 |
- It makes sense to use C(false) only when SQL injections via I(session_role) are possible. |
| 75 |
type: bool |
| 76 |
default: true |
| 77 |
version_added: '0.2.0' |
| 78 |
|
| 79 |
notes: |
| 80 |
- Physical replication slots were introduced to PostgreSQL with version 9.4, |
| 81 |
while logical replication slots were added beginning with version 10.0. |
| 82 |
|
| 83 |
attributes: |
| 84 |
check_mode: |
| 85 |
support: full |
| 86 |
|
| 87 |
seealso: |
| 88 |
- name: PostgreSQL pg_replication_slots view reference |
| 89 |
description: Complete reference of the PostgreSQL pg_replication_slots view. |
| 90 |
link: https://www.postgresql.org/docs/current/view-pg-replication-slots.html |
| 91 |
- name: PostgreSQL streaming replication protocol reference |
| 92 |
description: Complete reference of the PostgreSQL streaming replication protocol documentation. |
| 93 |
link: https://www.postgresql.org/docs/current/protocol-replication.html |
| 94 |
- name: PostgreSQL logical replication protocol reference |
| 95 |
description: Complete reference of the PostgreSQL logical replication protocol documentation. |
| 96 |
link: https://www.postgresql.org/docs/current/protocol-logical-replication.html |
| 97 |
|
| 98 |
author: |
| 99 |
- John Scalia (@jscalia) |
| 100 |
- Andrew Klychkov (@Andersson007) |
| 101 |
- Thomas O'Donnell (@andytom) |
| 102 |
extends_documentation_fragment: |
| 103 |
- community.postgresql.postgres |
| 104 |
''' |
| 105 |
|
| 106 |
EXAMPLES = r''' |
| 107 |
- name: Create physical_one physical slot if doesn't exist |
| 108 |
become_user: postgres |
| 109 |
community.postgresql.postgresql_slot: |
| 110 |
slot_name: physical_one |
| 111 |
login_db: ansible |
| 112 |
|
| 113 |
- name: Remove physical_one slot if exists |
| 114 |
become_user: postgres |
| 115 |
community.postgresql.postgresql_slot: |
| 116 |
slot_name: physical_one |
| 117 |
db: ansible |
| 118 |
state: absent |
| 119 |
|
| 120 |
- name: Create logical_one logical slot to the database acme if doesn't exist |
| 121 |
community.postgresql.postgresql_slot: |
| 122 |
name: logical_slot_one |
| 123 |
slot_type: logical |
| 124 |
state: present |
| 125 |
output_plugin: custom_decoder_one |
| 126 |
login_db: acme |
| 127 |
|
| 128 |
- name: Remove logical_one slot if exists from the cluster running on another host and non-standard port |
| 129 |
community.postgresql.postgresql_slot: |
| 130 |
name: logical_one |
| 131 |
login_host: mydatabase.example.org |
| 132 |
login_port: 5433 |
| 133 |
login_user: ourSuperuser |
| 134 |
login_password: thePassword |
| 135 |
state: absent |
| 136 |
''' |
| 137 |
|
| 138 |
RETURN = r''' |
| 139 |
name: |
| 140 |
description: Name of the slot. |
| 141 |
returned: success |
| 142 |
type: str |
| 143 |
sample: "physical_one" |
| 144 |
queries: |
| 145 |
description: List of executed queries. |
| 146 |
returned: success |
| 147 |
type: str |
| 148 |
sample: [ "SELECT pg_create_physical_replication_slot('physical_one', False, False)" ] |
| 149 |
''' |
| 150 |
|
| 151 |
from ansible.module_utils.basic import AnsibleModule |
| 152 |
from ansible_collections.community.postgresql.plugins.module_utils.database import \ |
| 153 |
check_input |
| 154 |
from ansible_collections.community.postgresql.plugins.module_utils.postgres import ( |
| 155 |
connect_to_db, |
| 156 |
ensure_required_libs, |
| 157 |
exec_sql, |
| 158 |
get_conn_params, |
| 159 |
get_server_version, |
| 160 |
pg_cursor_args, |
| 161 |
postgres_common_argument_spec, |
| 162 |
) |
| 163 |
|
| 164 |
# =========================================== |
| 165 |
# PostgreSQL module specific support methods. |
| 166 |
# |
| 167 |
|
| 168 |
|
| 169 |
class PgSlot(object): |
| 170 |
def __init__(self, module, cursor, name): |
| 171 |
self.module = module |
| 172 |
self.cursor = cursor |
| 173 |
self.name = name |
| 174 |
self.exists = False |
| 175 |
self.kind = '' |
| 176 |
self.__slot_exists() |
| 177 |
self.changed = False |
| 178 |
self.executed_queries = [] |
| 179 |
|
| 180 |
def create(self, kind='physical', immediately_reserve=False, output_plugin=False, just_check=False): |
| 181 |
if self.exists: |
| 182 |
if self.kind == kind: |
| 183 |
return False |
| 184 |
else: |
| 185 |
self.module.warn("slot with name '%s' already exists " |
| 186 |
"but has another type '%s'" % (self.name, self.kind)) |
| 187 |
return False |
| 188 |
|
| 189 |
if just_check: |
| 190 |
return None |
| 191 |
|
| 192 |
if kind == 'physical': |
| 193 |
# Check server version (immediately_reserved needs 9.6+): |
| 194 |
if get_server_version(self.cursor.connection) < 90600: |
| 195 |
query = "SELECT pg_create_physical_replication_slot(%(name)s)" |
| 196 |
|
| 197 |
else: |
| 198 |
query = "SELECT pg_create_physical_replication_slot(%(name)s, %(i_reserve)s)" |
| 199 |
|
| 200 |
self.changed = exec_sql(self, query, |
| 201 |
query_params={'name': self.name, 'i_reserve': immediately_reserve}, |
| 202 |
return_bool=True) |
| 203 |
|
| 204 |
elif kind == 'logical': |
| 205 |
query = "SELECT pg_create_logical_replication_slot(%(name)s, %(o_plugin)s)" |
| 206 |
self.changed = exec_sql(self, query, |
| 207 |
query_params={'name': self.name, 'o_plugin': output_plugin}, return_bool=True) |
| 208 |
|
| 209 |
def drop(self): |
| 210 |
if not self.exists: |
| 211 |
return False |
| 212 |
|
| 213 |
query = "SELECT pg_drop_replication_slot(%(name)s)" |
| 214 |
self.changed = exec_sql(self, query, query_params={'name': self.name}, return_bool=True) |
| 215 |
|
| 216 |
def __slot_exists(self): |
| 217 |
query = "SELECT slot_type FROM pg_replication_slots WHERE slot_name = %(name)s" |
| 218 |
res = exec_sql(self, query, query_params={'name': self.name}, add_to_executed=False) |
| 219 |
if res: |
| 220 |
self.exists = True |
| 221 |
self.kind = res[0]["slot_type"] |
| 222 |
|
| 223 |
|
| 224 |
# =========================================== |
| 225 |
# Module execution. |
| 226 |
# |
| 227 |
|
| 228 |
|
| 229 |
def main(): |
| 230 |
argument_spec = postgres_common_argument_spec() |
| 231 |
argument_spec.update( |
| 232 |
login_db=dict(type='str', aliases=['db'], deprecated_aliases=[ |
| 233 |
{ |
| 234 |
'name': 'db', |
| 235 |
'version': '5.0.0', |
| 236 |
'collection_name': 'community.postgresql', |
| 237 |
}], |
| 238 |
), |
| 239 |
name=dict(type="str", required=True, aliases=["slot_name"]), |
| 240 |
slot_type=dict(type="str", default="physical", choices=["logical", "physical"]), |
| 241 |
immediately_reserve=dict(type="bool", default=False), |
| 242 |
session_role=dict(type="str"), |
| 243 |
output_plugin=dict(type="str", default="test_decoding"), |
| 244 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 245 |
trust_input=dict(type="bool", default=True), |
| 246 |
) |
| 247 |
|
| 248 |
module = AnsibleModule( |
| 249 |
argument_spec=argument_spec, |
| 250 |
supports_check_mode=True, |
| 251 |
) |
| 252 |
|
| 253 |
name = module.params["name"] |
| 254 |
slot_type = module.params["slot_type"] |
| 255 |
immediately_reserve = module.params["immediately_reserve"] |
| 256 |
state = module.params["state"] |
| 257 |
output_plugin = module.params["output_plugin"] |
| 258 |
|
| 259 |
if not module.params["trust_input"]: |
| 260 |
check_input(module, module.params['session_role']) |
| 261 |
|
| 262 |
if immediately_reserve and slot_type == 'logical': |
| 263 |
module.fail_json(msg="Module parameters immediately_reserve and slot_type=logical are mutually exclusive") |
| 264 |
|
| 265 |
# When slot_type is logical and parameter db is not passed, |
| 266 |
# the default database will be used to create the slot and |
| 267 |
# the user should know about this. |
| 268 |
# When the slot type is physical, |
| 269 |
# it doesn't matter which database will be used |
| 270 |
# because physical slots are global objects. |
| 271 |
if slot_type == 'logical': |
| 272 |
warn_db_default = True |
| 273 |
else: |
| 274 |
warn_db_default = False |
| 275 |
|
| 276 |
# Ensure psycopg libraries are available before connecting to DB: |
| 277 |
ensure_required_libs(module) |
| 278 |
conn_params = get_conn_params(module, module.params, warn_db_default=warn_db_default) |
| 279 |
db_connection, dummy = connect_to_db(module, conn_params, autocommit=True) |
| 280 |
cursor = db_connection.cursor(**pg_cursor_args) |
| 281 |
|
| 282 |
################################## |
| 283 |
# Create an object and do main job |
| 284 |
pg_slot = PgSlot(module, cursor, name) |
| 285 |
|
| 286 |
changed = False |
| 287 |
|
| 288 |
if module.check_mode: |
| 289 |
if state == "present": |
| 290 |
if not pg_slot.exists: |
| 291 |
changed = True |
| 292 |
|
| 293 |
pg_slot.create(slot_type, immediately_reserve, output_plugin, just_check=True) |
| 294 |
|
| 295 |
elif state == "absent": |
| 296 |
if pg_slot.exists: |
| 297 |
changed = True |
| 298 |
else: |
| 299 |
if state == "absent": |
| 300 |
pg_slot.drop() |
| 301 |
|
| 302 |
elif state == "present": |
| 303 |
pg_slot.create(slot_type, immediately_reserve, output_plugin) |
| 304 |
|
| 305 |
changed = pg_slot.changed |
| 306 |
|
| 307 |
db_connection.close() |
| 308 |
module.exit_json(changed=changed, name=name, queries=pg_slot.executed_queries) |
| 309 |
|
| 310 |
|
| 311 |
if __name__ == '__main__': |
| 312 |
main() |