mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Allow user to specify maintenance DB (#32510)
This fix allows user to specify alternative maintenance DB required for initial connection in Postgresql_db module. Also, adds pep8 related fixes. Fixes: #30017 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
9a6615a905
commit
b5f1fc850b
2 changed files with 36 additions and 21 deletions
|
@ -72,6 +72,11 @@ options:
|
||||||
version_added: "2.4"
|
version_added: "2.4"
|
||||||
description:
|
description:
|
||||||
- Further arguments for pg_dump or pg_restore. Used when state is "dump" or "restore"
|
- Further arguments for pg_dump or pg_restore. Used when state is "dump" or "restore"
|
||||||
|
maintenance_db:
|
||||||
|
version_added: "2.5"
|
||||||
|
description:
|
||||||
|
- The value specifies the initial database (which is also called as maintenance DB) that Ansible connects to.
|
||||||
|
default: postgres
|
||||||
author: "Ansible Core Team"
|
author: "Ansible Core Team"
|
||||||
extends_documentation_fragment:
|
extends_documentation_fragment:
|
||||||
- postgres
|
- postgres
|
||||||
|
@ -112,18 +117,16 @@ EXAMPLES = '''
|
||||||
target_opts: "-n public"
|
target_opts: "-n public"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import os
|
||||||
|
import pipes
|
||||||
|
import subprocess
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
HAS_PSYCOPG2 = False
|
|
||||||
try:
|
try:
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extras
|
import psycopg2.extras
|
||||||
import pipes
|
|
||||||
import subprocess
|
|
||||||
import os
|
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
HAS_PSYCOPG2 = False
|
||||||
else:
|
else:
|
||||||
HAS_PSYCOPG2 = True
|
HAS_PSYCOPG2 = True
|
||||||
|
|
||||||
|
@ -149,11 +152,13 @@ def set_owner(cursor, db, owner):
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_encoding_id(cursor, encoding):
|
def get_encoding_id(cursor, encoding):
|
||||||
query = "SELECT pg_char_to_encoding(%(encoding)s) AS encoding_id;"
|
query = "SELECT pg_char_to_encoding(%(encoding)s) AS encoding_id;"
|
||||||
cursor.execute(query, {'encoding': encoding})
|
cursor.execute(query, {'encoding': encoding})
|
||||||
return cursor.fetchone()['encoding_id']
|
return cursor.fetchone()['encoding_id']
|
||||||
|
|
||||||
|
|
||||||
def get_db_info(cursor, db):
|
def get_db_info(cursor, db):
|
||||||
query = """
|
query = """
|
||||||
SELECT rolname AS owner,
|
SELECT rolname AS owner,
|
||||||
|
@ -165,11 +170,13 @@ def get_db_info(cursor, db):
|
||||||
cursor.execute(query, {'db': db})
|
cursor.execute(query, {'db': db})
|
||||||
return cursor.fetchone()
|
return cursor.fetchone()
|
||||||
|
|
||||||
|
|
||||||
def db_exists(cursor, db):
|
def db_exists(cursor, db):
|
||||||
query = "SELECT * FROM pg_database WHERE datname=%(db)s"
|
query = "SELECT * FROM pg_database WHERE datname=%(db)s"
|
||||||
cursor.execute(query, {'db': db})
|
cursor.execute(query, {'db': db})
|
||||||
return cursor.rowcount == 1
|
return cursor.rowcount == 1
|
||||||
|
|
||||||
|
|
||||||
def db_delete(cursor, db):
|
def db_delete(cursor, db):
|
||||||
if db_exists(cursor, db):
|
if db_exists(cursor, db):
|
||||||
query = "DROP DATABASE %s" % pg_quote_identifier(db, 'database')
|
query = "DROP DATABASE %s" % pg_quote_identifier(db, 'database')
|
||||||
|
@ -178,6 +185,7 @@ def db_delete(cursor, db):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype):
|
def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype):
|
||||||
params = dict(enc=encoding, collate=lc_collate, ctype=lc_ctype)
|
params = dict(enc=encoding, collate=lc_collate, ctype=lc_ctype)
|
||||||
if not db_exists(cursor, db):
|
if not db_exists(cursor, db):
|
||||||
|
@ -218,6 +226,7 @@ def db_create(cursor, db, owner, template, encoding, lc_collate, lc_ctype):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype):
|
def db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype):
|
||||||
if not db_exists(cursor, db):
|
if not db_exists(cursor, db):
|
||||||
return False
|
return False
|
||||||
|
@ -235,6 +244,7 @@ def db_matches(cursor, db, owner, template, encoding, lc_collate, lc_ctype):
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def db_dump(module, target, target_opts="",
|
def db_dump(module, target, target_opts="",
|
||||||
db=None,
|
db=None,
|
||||||
user=None,
|
user=None,
|
||||||
|
@ -270,6 +280,7 @@ def db_dump(module, target, target_opts="",
|
||||||
|
|
||||||
return do_with_password(module, cmd, password)
|
return do_with_password(module, cmd, password)
|
||||||
|
|
||||||
|
|
||||||
def db_restore(module, target, target_opts="",
|
def db_restore(module, target, target_opts="",
|
||||||
db=None,
|
db=None,
|
||||||
user=None,
|
user=None,
|
||||||
|
@ -321,6 +332,7 @@ def db_restore(module, target, target_opts="",
|
||||||
|
|
||||||
return do_with_password(module, cmd, password)
|
return do_with_password(module, cmd, password)
|
||||||
|
|
||||||
|
|
||||||
def login_flags(db, host, port, user, db_prefix=True):
|
def login_flags(db, host, port, user, db_prefix=True):
|
||||||
"""
|
"""
|
||||||
returns a list of connection argument strings each prefixed
|
returns a list of connection argument strings each prefixed
|
||||||
|
@ -344,6 +356,7 @@ def login_flags(db, host, port, user, db_prefix=True):
|
||||||
flags.append(' --username={0}'.format(user))
|
flags.append(' --username={0}'.format(user))
|
||||||
return flags
|
return flags
|
||||||
|
|
||||||
|
|
||||||
def do_with_password(module, cmd, password):
|
def do_with_password(module, cmd, password):
|
||||||
env = {}
|
env = {}
|
||||||
if password:
|
if password:
|
||||||
|
@ -355,6 +368,7 @@ def do_with_password(module, cmd, password):
|
||||||
# Module execution.
|
# Module execution.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = pgutils.postgres_common_argument_spec()
|
argument_spec = pgutils.postgres_common_argument_spec()
|
||||||
argument_spec.update(dict(
|
argument_spec.update(dict(
|
||||||
|
@ -367,6 +381,7 @@ def main():
|
||||||
state=dict(default="present", choices=["absent", "present", "dump", "restore"]),
|
state=dict(default="present", choices=["absent", "present", "dump", "restore"]),
|
||||||
target=dict(default="", type="path"),
|
target=dict(default="", type="path"),
|
||||||
target_opts=dict(default=""),
|
target_opts=dict(default=""),
|
||||||
|
maintenance_db=dict(default="postgres"),
|
||||||
))
|
))
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
@ -387,6 +402,7 @@ def main():
|
||||||
target_opts = module.params["target_opts"]
|
target_opts = module.params["target_opts"]
|
||||||
state = module.params["state"]
|
state = module.params["state"]
|
||||||
changed = False
|
changed = False
|
||||||
|
maintenance_db = module.params['maintenance_db']
|
||||||
|
|
||||||
# To use defaults values, keyword arguments must be absent, so
|
# To use defaults values, keyword arguments must be absent, so
|
||||||
# check which values are empty and don't include in the **kw
|
# check which values are empty and don't include in the **kw
|
||||||
|
@ -414,7 +430,7 @@ def main():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pgutils.ensure_libs(sslrootcert=module.params.get('ssl_rootcert'))
|
pgutils.ensure_libs(sslrootcert=module.params.get('ssl_rootcert'))
|
||||||
db_connection = psycopg2.connect(database="postgres", **kw)
|
db_connection = psycopg2.connect(database=maintenance_db, **kw)
|
||||||
|
|
||||||
# Enable autocommit so we can create databases
|
# Enable autocommit so we can create databases
|
||||||
if psycopg2.__version__ >= '2.4.2':
|
if psycopg2.__version__ >= '2.4.2':
|
||||||
|
|
|
@ -128,7 +128,6 @@ lib/ansible/modules/database/misc/riak.py
|
||||||
lib/ansible/modules/database/mongodb/mongodb_parameter.py
|
lib/ansible/modules/database/mongodb/mongodb_parameter.py
|
||||||
lib/ansible/modules/database/mongodb/mongodb_user.py
|
lib/ansible/modules/database/mongodb/mongodb_user.py
|
||||||
lib/ansible/modules/database/mssql/mssql_db.py
|
lib/ansible/modules/database/mssql/mssql_db.py
|
||||||
lib/ansible/modules/database/postgresql/postgresql_db.py
|
|
||||||
lib/ansible/modules/database/postgresql/postgresql_ext.py
|
lib/ansible/modules/database/postgresql/postgresql_ext.py
|
||||||
lib/ansible/modules/database/postgresql/postgresql_lang.py
|
lib/ansible/modules/database/postgresql/postgresql_lang.py
|
||||||
lib/ansible/modules/database/postgresql/postgresql_schema.py
|
lib/ansible/modules/database/postgresql/postgresql_schema.py
|
||||||
|
|
Loading…
Reference in a new issue