mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-14 12:19:10 -07:00
Relocating extras into lib/ansible/modules/ after merge
This commit is contained in:
parent
c65ba07d2c
commit
011ea55a8f
596 changed files with 0 additions and 266 deletions
198
lib/ansible/modules/database/postgresql/postgresql_ext.py
Normal file
198
lib/ansible/modules/database/postgresql/postgresql_ext.py
Normal file
|
@ -0,0 +1,198 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
ANSIBLE_METADATA = {'status': ['preview'],
|
||||
'supported_by': 'community',
|
||||
'version': '1.0'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: postgresql_ext
|
||||
short_description: Add or remove PostgreSQL extensions from a database.
|
||||
description:
|
||||
- Add or remove PostgreSQL extensions from a database.
|
||||
version_added: "1.9"
|
||||
options:
|
||||
name:
|
||||
description:
|
||||
- name of the extension to add or remove
|
||||
required: true
|
||||
default: null
|
||||
db:
|
||||
description:
|
||||
- name of the database to add or remove the extension to/from
|
||||
required: true
|
||||
default: null
|
||||
login_user:
|
||||
description:
|
||||
- The username used to authenticate with
|
||||
required: false
|
||||
default: null
|
||||
login_password:
|
||||
description:
|
||||
- The password used to authenticate with
|
||||
required: false
|
||||
default: null
|
||||
login_host:
|
||||
description:
|
||||
- Host running the database
|
||||
required: false
|
||||
default: localhost
|
||||
port:
|
||||
description:
|
||||
- Database port to connect to.
|
||||
required: false
|
||||
default: 5432
|
||||
state:
|
||||
description:
|
||||
- The database extension state
|
||||
required: false
|
||||
default: present
|
||||
choices: [ "present", "absent" ]
|
||||
notes:
|
||||
- The default authentication assumes that you are either logging in as or sudo'ing to the C(postgres) account on the host.
|
||||
- This module uses I(psycopg2), a Python PostgreSQL database adapter. You must ensure that psycopg2 is installed on
|
||||
the host before using this module. If the remote host is the PostgreSQL server (which is the default case), then PostgreSQL must also be installed on the remote host. For Ubuntu-based systems, install the C(postgresql), C(libpq-dev), and C(python-psycopg2) packages on the remote host before using this module.
|
||||
requirements: [ psycopg2 ]
|
||||
author: "Daniel Schep (@dschep)"
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Adds postgis to the database "acme"
|
||||
- postgresql_ext:
|
||||
name: postgis
|
||||
db: acme
|
||||
'''
|
||||
|
||||
try:
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
except ImportError:
|
||||
postgresqldb_found = False
|
||||
else:
|
||||
postgresqldb_found = True
|
||||
|
||||
class NotSupportedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
# ===========================================
|
||||
# PostgreSQL module specific support methods.
|
||||
#
|
||||
|
||||
def ext_exists(cursor, ext):
|
||||
query = "SELECT * FROM pg_extension WHERE extname=%(ext)s"
|
||||
cursor.execute(query, {'ext': ext})
|
||||
return cursor.rowcount == 1
|
||||
|
||||
def ext_delete(cursor, ext):
|
||||
if ext_exists(cursor, ext):
|
||||
query = "DROP EXTENSION \"%s\"" % ext
|
||||
cursor.execute(query)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def ext_create(cursor, ext):
|
||||
if not ext_exists(cursor, ext):
|
||||
query = 'CREATE EXTENSION "%s"' % ext
|
||||
cursor.execute(query)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
# ===========================================
|
||||
# Module execution.
|
||||
#
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
login_user=dict(default="postgres"),
|
||||
login_password=dict(default="", no_log=True),
|
||||
login_host=dict(default=""),
|
||||
port=dict(default="5432"),
|
||||
db=dict(required=True),
|
||||
ext=dict(required=True, aliases=['name']),
|
||||
state=dict(default="present", choices=["absent", "present"]),
|
||||
),
|
||||
supports_check_mode = True
|
||||
)
|
||||
|
||||
if not postgresqldb_found:
|
||||
module.fail_json(msg="the python psycopg2 module is required")
|
||||
|
||||
db = module.params["db"]
|
||||
ext = module.params["ext"]
|
||||
port = module.params["port"]
|
||||
state = module.params["state"]
|
||||
changed = False
|
||||
|
||||
# To use defaults values, keyword arguments must be absent, so
|
||||
# check which values are empty and don't include in the **kw
|
||||
# dictionary
|
||||
params_map = {
|
||||
"login_host":"host",
|
||||
"login_user":"user",
|
||||
"login_password":"password",
|
||||
"port":"port"
|
||||
}
|
||||
kw = dict( (params_map[k], v) for (k, v) in module.params.iteritems()
|
||||
if k in params_map and v != '' )
|
||||
try:
|
||||
db_connection = psycopg2.connect(database=db, **kw)
|
||||
# Enable autocommit so we can create databases
|
||||
if psycopg2.__version__ >= '2.4.2':
|
||||
db_connection.autocommit = True
|
||||
else:
|
||||
db_connection.set_isolation_level(psycopg2
|
||||
.extensions
|
||||
.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||
cursor = db_connection.cursor(
|
||||
cursor_factory=psycopg2.extras.DictCursor)
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="unable to connect to database: %s" % e)
|
||||
|
||||
try:
|
||||
if module.check_mode:
|
||||
if state == "present":
|
||||
changed = not ext_exists(cursor, ext)
|
||||
elif state == "absent":
|
||||
changed = ext_exists(cursor, ext)
|
||||
else:
|
||||
if state == "absent":
|
||||
changed = ext_delete(cursor, ext)
|
||||
|
||||
elif state == "present":
|
||||
changed = ext_create(cursor, ext)
|
||||
except NotSupportedError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="Database query failed: %s" % e)
|
||||
|
||||
module.exit_json(changed=changed, db=db, ext=ext)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.pycompat24 import get_exception
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
294
lib/ansible/modules/database/postgresql/postgresql_lang.py
Normal file
294
lib/ansible/modules/database/postgresql/postgresql_lang.py
Normal file
|
@ -0,0 +1,294 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# (c) 2014, Jens Depuydt <http://www.jensd.be>
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
ANSIBLE_METADATA = {'status': ['preview'],
|
||||
'supported_by': 'community',
|
||||
'version': '1.0'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: postgresql_lang
|
||||
short_description: Adds, removes or changes procedural languages with a PostgreSQL database.
|
||||
description:
|
||||
- Adds, removes or changes procedural languages with a PostgreSQL database.
|
||||
- This module allows you to add a language, remote a language or change the trust
|
||||
relationship with a PostgreSQL database. The module can be used on the machine
|
||||
where executed or on a remote host.
|
||||
- When removing a language from a database, it is possible that dependencies prevent
|
||||
the database from being removed. In that case, you can specify casade to
|
||||
automatically drop objects that depend on the language (such as functions in the
|
||||
language). In case the language can't be deleted because it is required by the
|
||||
database system, you can specify fail_on_drop=no to ignore the error.
|
||||
- Be carefull when marking a language as trusted since this could be a potential
|
||||
security breach. Untrusted languages allow only users with the PostgreSQL superuser
|
||||
privilege to use this language to create new functions.
|
||||
version_added: "1.7"
|
||||
options:
|
||||
lang:
|
||||
description:
|
||||
- name of the procedural language to add, remove or change
|
||||
required: true
|
||||
default: null
|
||||
trust:
|
||||
description:
|
||||
- make this language trusted for the selected db
|
||||
required: false
|
||||
default: no
|
||||
choices: [ "yes", "no" ]
|
||||
db:
|
||||
description:
|
||||
- name of database where the language will be added, removed or changed
|
||||
required: false
|
||||
default: null
|
||||
force_trust:
|
||||
description:
|
||||
- marks the language as trusted, even if it's marked as untrusted in pg_pltemplate.
|
||||
- use with care!
|
||||
required: false
|
||||
default: no
|
||||
choices: [ "yes", "no" ]
|
||||
fail_on_drop:
|
||||
description:
|
||||
- if C(yes), fail when removing a language. Otherwise just log and continue
|
||||
- in some cases, it is not possible to remove a language (used by the db-system). When dependencies block the removal, consider using C(cascade).
|
||||
required: false
|
||||
default: 'yes'
|
||||
choices: [ "yes", "no" ]
|
||||
cascade:
|
||||
description:
|
||||
- when dropping a language, also delete object that depend on this language.
|
||||
- only used when C(state=absent).
|
||||
required: false
|
||||
default: no
|
||||
choices: [ "yes", "no" ]
|
||||
port:
|
||||
description:
|
||||
- Database port to connect to.
|
||||
required: false
|
||||
default: 5432
|
||||
login_user:
|
||||
description:
|
||||
- User used to authenticate with PostgreSQL
|
||||
required: false
|
||||
default: postgres
|
||||
login_password:
|
||||
description:
|
||||
- Password used to authenticate with PostgreSQL (must match C(login_user))
|
||||
required: false
|
||||
default: null
|
||||
login_host:
|
||||
description:
|
||||
- Host running PostgreSQL where you want to execute the actions.
|
||||
required: false
|
||||
default: localhost
|
||||
state:
|
||||
description:
|
||||
- The state of the language for the selected database
|
||||
required: false
|
||||
default: present
|
||||
choices: [ "present", "absent" ]
|
||||
notes:
|
||||
- The default authentication assumes that you are either logging in as or
|
||||
sudo'ing to the postgres account on the host.
|
||||
- This module uses psycopg2, a Python PostgreSQL database adapter. You must
|
||||
ensure that psycopg2 is installed on the host before using this module. If
|
||||
the remote host is the PostgreSQL server (which is the default case), then
|
||||
PostgreSQL must also be installed on the remote host. For Ubuntu-based
|
||||
systems, install the postgresql, libpq-dev, and python-psycopg2 packages
|
||||
on the remote host before using this module.
|
||||
requirements: [ psycopg2 ]
|
||||
author: "Jens Depuydt (@jensdepuydt)"
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Add language pltclu to database testdb if it doesn't exist:
|
||||
- postgresql_lang db=testdb lang=pltclu state=present
|
||||
|
||||
# Add language pltclu to database testdb if it doesn't exist and mark it as trusted:
|
||||
# Marks the language as trusted if it exists but isn't trusted yet
|
||||
# force_trust makes sure that the language will be marked as trusted
|
||||
- postgresql_lang:
|
||||
db: testdb
|
||||
lang: pltclu
|
||||
state: present
|
||||
trust: yes
|
||||
force_trust: yes
|
||||
|
||||
# Remove language pltclu from database testdb:
|
||||
- postgresql_lang:
|
||||
db: testdb
|
||||
lang: pltclu
|
||||
state: absent
|
||||
|
||||
# Remove language pltclu from database testdb and remove all dependencies:
|
||||
- postgresql_lang:
|
||||
db: testdb
|
||||
lang: pltclu
|
||||
state: absent
|
||||
cascade: yes
|
||||
|
||||
# Remove language c from database testdb but ignore errors if something prevents the removal:
|
||||
- postgresql_lang:
|
||||
db: testdb
|
||||
lang: pltclu
|
||||
state: absent
|
||||
fail_on_drop: no
|
||||
'''
|
||||
|
||||
try:
|
||||
import psycopg2
|
||||
except ImportError:
|
||||
postgresqldb_found = False
|
||||
else:
|
||||
postgresqldb_found = True
|
||||
|
||||
def lang_exists(cursor, lang):
|
||||
"""Checks if language exists for db"""
|
||||
query = "SELECT lanname FROM pg_language WHERE lanname='%s'" % lang
|
||||
cursor.execute(query)
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def lang_istrusted(cursor, lang):
|
||||
"""Checks if language is trusted for db"""
|
||||
query = "SELECT lanpltrusted FROM pg_language WHERE lanname='%s'" % lang
|
||||
cursor.execute(query)
|
||||
return cursor.fetchone()[0]
|
||||
|
||||
def lang_altertrust(cursor, lang, trust):
|
||||
"""Changes if language is trusted for db"""
|
||||
query = "UPDATE pg_language SET lanpltrusted = %s WHERE lanname=%s"
|
||||
cursor.execute(query, (trust, lang))
|
||||
return True
|
||||
|
||||
def lang_add(cursor, lang, trust):
|
||||
"""Adds language for db"""
|
||||
if trust:
|
||||
query = 'CREATE TRUSTED LANGUAGE "%s"' % lang
|
||||
else:
|
||||
query = 'CREATE LANGUAGE "%s"' % lang
|
||||
cursor.execute(query)
|
||||
return True
|
||||
|
||||
def lang_drop(cursor, lang, cascade):
|
||||
"""Drops language for db"""
|
||||
cursor.execute("SAVEPOINT ansible_pgsql_lang_drop")
|
||||
try:
|
||||
if cascade:
|
||||
cursor.execute("DROP LANGUAGE \"%s\" CASCADE" % lang)
|
||||
else:
|
||||
cursor.execute("DROP LANGUAGE \"%s\"" % lang)
|
||||
except:
|
||||
cursor.execute("ROLLBACK TO SAVEPOINT ansible_pgsql_lang_drop")
|
||||
cursor.execute("RELEASE SAVEPOINT ansible_pgsql_lang_drop")
|
||||
return False
|
||||
cursor.execute("RELEASE SAVEPOINT ansible_pgsql_lang_drop")
|
||||
return True
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
login_user=dict(default="postgres"),
|
||||
login_password=dict(default="", no_log=True),
|
||||
login_host=dict(default=""),
|
||||
db=dict(required=True),
|
||||
port=dict(default='5432'),
|
||||
lang=dict(required=True),
|
||||
state=dict(default="present", choices=["absent", "present"]),
|
||||
trust=dict(type='bool', default='no'),
|
||||
force_trust=dict(type='bool', default='no'),
|
||||
cascade=dict(type='bool', default='no'),
|
||||
fail_on_drop=dict(type='bool', default='yes'),
|
||||
),
|
||||
supports_check_mode = True
|
||||
)
|
||||
|
||||
db = module.params["db"]
|
||||
port = module.params["port"]
|
||||
lang = module.params["lang"]
|
||||
state = module.params["state"]
|
||||
trust = module.params["trust"]
|
||||
force_trust = module.params["force_trust"]
|
||||
cascade = module.params["cascade"]
|
||||
fail_on_drop = module.params["fail_on_drop"]
|
||||
|
||||
if not postgresqldb_found:
|
||||
module.fail_json(msg="the python psycopg2 module is required")
|
||||
|
||||
params_map = {
|
||||
"login_host":"host",
|
||||
"login_user":"user",
|
||||
"login_password":"password",
|
||||
"port":"port",
|
||||
"db":"database"
|
||||
}
|
||||
kw = dict( (params_map[k], v) for (k, v) in module.params.iteritems()
|
||||
if k in params_map and v != "" )
|
||||
try:
|
||||
db_connection = psycopg2.connect(**kw)
|
||||
cursor = db_connection.cursor()
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="unable to connect to database: %s" % e)
|
||||
changed = False
|
||||
lang_dropped = False
|
||||
kw = dict(db=db,lang=lang,trust=trust)
|
||||
|
||||
if state == "present":
|
||||
if lang_exists(cursor, lang):
|
||||
lang_trusted = lang_istrusted(cursor, lang)
|
||||
if (lang_trusted and not trust) or (not lang_trusted and trust):
|
||||
if module.check_mode:
|
||||
changed = True
|
||||
else:
|
||||
changed = lang_altertrust(cursor, lang, trust)
|
||||
else:
|
||||
if module.check_mode:
|
||||
changed = True
|
||||
else:
|
||||
changed = lang_add(cursor, lang, trust)
|
||||
if force_trust:
|
||||
changed = lang_altertrust(cursor, lang, trust)
|
||||
|
||||
else:
|
||||
if lang_exists(cursor, lang):
|
||||
if module.check_mode:
|
||||
changed = True
|
||||
kw['lang_dropped'] = True
|
||||
else:
|
||||
changed = lang_drop(cursor, lang, cascade)
|
||||
if fail_on_drop and not changed:
|
||||
msg = "unable to drop language, use cascade to delete dependencies or fail_on_drop=no to ignore"
|
||||
module.fail_json(msg=msg)
|
||||
kw['lang_dropped'] = changed
|
||||
|
||||
if changed:
|
||||
if module.check_mode:
|
||||
db_connection.rollback()
|
||||
else:
|
||||
db_connection.commit()
|
||||
|
||||
kw['changed'] = changed
|
||||
module.exit_json(**kw)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.pycompat24 import get_exception
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
274
lib/ansible/modules/database/postgresql/postgresql_schema.py
Normal file
274
lib/ansible/modules/database/postgresql/postgresql_schema.py
Normal file
|
@ -0,0 +1,274 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
ANSIBLE_METADATA = {'status': 'preview',
|
||||
'supported_by': 'community',
|
||||
'version': '1.0'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: postgresql_schema
|
||||
short_description: Add or remove PostgreSQL schema from a remote host
|
||||
description:
|
||||
- Add or remove PostgreSQL schema from a remote host.
|
||||
version_added: "2.3"
|
||||
options:
|
||||
name:
|
||||
description:
|
||||
- Name of the schema to add or remove.
|
||||
required: true
|
||||
default: null
|
||||
database:
|
||||
description:
|
||||
- Name of the database to connect to.
|
||||
required: false
|
||||
default: postgres
|
||||
login_user:
|
||||
description:
|
||||
- The username used to authenticate with.
|
||||
required: false
|
||||
default: null
|
||||
login_password:
|
||||
description:
|
||||
- The password used to authenticate with.
|
||||
required: false
|
||||
default: null
|
||||
login_host:
|
||||
description:
|
||||
- Host running the database.
|
||||
required: false
|
||||
default: localhost
|
||||
login_unix_socket:
|
||||
description:
|
||||
- Path to a Unix domain socket for local connections.
|
||||
required: false
|
||||
default: null
|
||||
owner:
|
||||
description:
|
||||
- Name of the role to set as owner of the schema.
|
||||
required: false
|
||||
default: null
|
||||
port:
|
||||
description:
|
||||
- Database port to connect to.
|
||||
required: false
|
||||
default: 5432
|
||||
state:
|
||||
description:
|
||||
- The schema state.
|
||||
required: false
|
||||
default: present
|
||||
choices: [ "present", "absent" ]
|
||||
notes:
|
||||
- This module uses I(psycopg2), a Python PostgreSQL database adapter. You must ensure that psycopg2 is installed on
|
||||
the host before using this module. If the remote host is the PostgreSQL server (which is the default case), then PostgreSQL must also be installed on the remote host. For Ubuntu-based systems, install the C(postgresql), C(libpq-dev), and C(python-psycopg2) packages on the remote host before using this module.
|
||||
requirements: [ psycopg2 ]
|
||||
author: "Flavien Chantelot <contact@flavien.io>"
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Create a new schema with name "acme"
|
||||
- postgresql_schema:
|
||||
name: acme
|
||||
|
||||
# Create a new schema "acme" with a user "bob" who will own it
|
||||
- postgresql_schema:
|
||||
name: acme
|
||||
owner: bob
|
||||
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
schema:
|
||||
description: Name of the schema
|
||||
returned: success, changed
|
||||
type: string
|
||||
sample: "acme"
|
||||
'''
|
||||
|
||||
|
||||
try:
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
except ImportError:
|
||||
postgresqldb_found = False
|
||||
else:
|
||||
postgresqldb_found = True
|
||||
|
||||
class NotSupportedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
# ===========================================
|
||||
# PostgreSQL module specific support methods.
|
||||
#
|
||||
|
||||
def set_owner(cursor, schema, owner):
|
||||
query = "ALTER SCHEMA %s OWNER TO %s" % (
|
||||
pg_quote_identifier(schema, 'schema'),
|
||||
pg_quote_identifier(owner, 'role'))
|
||||
cursor.execute(query)
|
||||
return True
|
||||
|
||||
def get_schema_info(cursor, schema):
|
||||
query = """
|
||||
SELECT schema_owner AS owner
|
||||
FROM information_schema.schemata
|
||||
WHERE schema_name = %(schema)s
|
||||
"""
|
||||
cursor.execute(query, {'schema': schema})
|
||||
return cursor.fetchone()
|
||||
|
||||
def schema_exists(cursor, schema):
|
||||
query = "SELECT schema_name FROM information_schema.schemata WHERE schema_name = %(schema)s"
|
||||
cursor.execute(query, {'schema': schema})
|
||||
return cursor.rowcount == 1
|
||||
|
||||
def schema_delete(cursor, schema):
|
||||
if schema_exists(cursor, schema):
|
||||
query = "DROP SCHEMA %s" % pg_quote_identifier(schema, 'schema')
|
||||
cursor.execute(query)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def schema_create(cursor, schema, owner):
|
||||
if not schema_exists(cursor, schema):
|
||||
query_fragments = ['CREATE SCHEMA %s' % pg_quote_identifier(schema, 'schema')]
|
||||
if owner:
|
||||
query_fragments.append('AUTHORIZATION %s' % pg_quote_identifier(owner, 'role'))
|
||||
query = ' '.join(query_fragments)
|
||||
cursor.execute(query)
|
||||
return True
|
||||
else:
|
||||
schema_info = get_schema_info(cursor, schema)
|
||||
if owner and owner != schema_info['owner']:
|
||||
return set_owner(cursor, schema, owner)
|
||||
else:
|
||||
return False
|
||||
|
||||
def schema_matches(cursor, schema, owner):
|
||||
if not schema_exists(cursor, schema):
|
||||
return False
|
||||
else:
|
||||
schema_info = get_schema_info(cursor, schema)
|
||||
if owner and owner != schema_info['owner']:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
# ===========================================
|
||||
# Module execution.
|
||||
#
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
login_user=dict(default="postgres"),
|
||||
login_password=dict(default=""),
|
||||
login_host=dict(default=""),
|
||||
login_unix_socket=dict(default=""),
|
||||
port=dict(default="5432"),
|
||||
schema=dict(required=True, aliases=['name']),
|
||||
owner=dict(default=""),
|
||||
database=dict(default="postgres"),
|
||||
state=dict(default="present", choices=["absent", "present"]),
|
||||
),
|
||||
supports_check_mode = True
|
||||
)
|
||||
|
||||
if not postgresqldb_found:
|
||||
module.fail_json(msg="the python psycopg2 module is required")
|
||||
|
||||
schema = module.params["schema"]
|
||||
owner = module.params["owner"]
|
||||
state = module.params["state"]
|
||||
database = module.params["database"]
|
||||
changed = False
|
||||
|
||||
# To use defaults values, keyword arguments must be absent, so
|
||||
# check which values are empty and don't include in the **kw
|
||||
# dictionary
|
||||
params_map = {
|
||||
"login_host":"host",
|
||||
"login_user":"user",
|
||||
"login_password":"password",
|
||||
"port":"port"
|
||||
}
|
||||
kw = dict( (params_map[k], v) for (k, v) in module.params.iteritems()
|
||||
if k in params_map and v != '' )
|
||||
|
||||
# If a login_unix_socket is specified, incorporate it here.
|
||||
is_localhost = "host" not in kw or kw["host"] == "" or kw["host"] == "localhost"
|
||||
if is_localhost and module.params["login_unix_socket"] != "":
|
||||
kw["host"] = module.params["login_unix_socket"]
|
||||
|
||||
try:
|
||||
db_connection = psycopg2.connect(database=database, **kw)
|
||||
# Enable autocommit so we can create databases
|
||||
if psycopg2.__version__ >= '2.4.2':
|
||||
db_connection.autocommit = True
|
||||
else:
|
||||
db_connection.set_isolation_level(psycopg2
|
||||
.extensions
|
||||
.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||
cursor = db_connection.cursor(
|
||||
cursor_factory=psycopg2.extras.DictCursor)
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="unable to connect to database: %s" %(text, str(e)))
|
||||
|
||||
try:
|
||||
if module.check_mode:
|
||||
if state == "absent":
|
||||
changed = not schema_exists(cursor, schema)
|
||||
elif state == "present":
|
||||
changed = not schema_matches(cursor, schema, owner)
|
||||
module.exit_json(changed=changed, schema=schema)
|
||||
|
||||
if state == "absent":
|
||||
try:
|
||||
changed = schema_delete(cursor, schema)
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
|
||||
elif state == "present":
|
||||
try:
|
||||
changed = schema_create(cursor, schema, owner)
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
except NotSupportedError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
except SystemExit:
|
||||
# Avoid catching this on Python 2.4
|
||||
raise
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="Database query failed: %s" %(text, str(e)))
|
||||
|
||||
module.exit_json(changed=changed, schema=schema)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.database import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue