postgresql_ext: Allow creating extension in a specific schema (#47279)

* Allow creating extension in a specific schema

* Code Review: Update example to include schema parameter

* Code Review: Use simple string concatenation to build query
This commit is contained in:
Prashant Baisla 2019-01-25 14:55:39 +05:30 committed by ansibot
parent 35761a99c6
commit 9cb7cbb62a

View file

@ -28,6 +28,10 @@ options:
description:
- name of the database to add or remove the extension to/from
required: true
schema:
description:
- name of the schema to add the extension to
version_added: "2.8"
login_user:
description:
- The username used to authenticate with
@ -84,6 +88,7 @@ EXAMPLES = '''
- postgresql_ext:
name: postgis
db: acme
schema: extensions
'''
import traceback
@ -123,9 +128,11 @@ def ext_delete(cursor, ext):
return False
def ext_create(cursor, ext):
def ext_create(cursor, ext, schema):
if not ext_exists(cursor, ext):
query = 'CREATE EXTENSION "%s"' % ext
if schema:
query += ' WITH SCHEMA "%s"' % schema
cursor.execute(query)
return True
else:
@ -146,6 +153,7 @@ def main():
port=dict(default="5432"),
db=dict(required=True),
ext=dict(required=True, aliases=['name']),
schema=dict(default=""),
state=dict(default="present", choices=["absent", "present"]),
ssl_mode=dict(default='prefer', choices=[
'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full']),
@ -159,6 +167,7 @@ def main():
db = module.params["db"]
ext = module.params["ext"]
schema = module.params["schema"]
state = module.params["state"]
sslrootcert = module.params["ssl_rootcert"]
changed = False
@ -218,7 +227,7 @@ def main():
changed = ext_delete(cursor, ext)
elif state == "present":
changed = ext_create(cursor, ext)
changed = ext_create(cursor, ext, schema)
except NotSupportedError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
except Exception as e: