|
|
|
@ -36,8 +36,14 @@ def db_delete(cursor, db):
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def db_create(cursor, db):
|
|
|
|
|
query = "CREATE DATABASE %s" % db
|
|
|
|
|
def db_create(cursor, db, owner, template, encoding):
|
|
|
|
|
if owner:
|
|
|
|
|
owner = " OWNER %s" % owner
|
|
|
|
|
if template:
|
|
|
|
|
template = " TEMPLATE %s" % template
|
|
|
|
|
if encoding:
|
|
|
|
|
encoding = " ENCODING '%s'" % encoding
|
|
|
|
|
query = "CREATE DATABASE %s%s%s%s" % (db, owner, template, encoding)
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
@ -52,6 +58,9 @@ def main():
|
|
|
|
|
login_password=dict(default=""),
|
|
|
|
|
login_host=dict(default=""),
|
|
|
|
|
db=dict(required=True),
|
|
|
|
|
owner=dict(default=""),
|
|
|
|
|
template=dict(default=""),
|
|
|
|
|
encoding=dict(default=""),
|
|
|
|
|
state=dict(default="present", choices=["absent", "present"]),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
@ -60,6 +69,9 @@ def main():
|
|
|
|
|
module.fail_json(msg="the python psycopg2 module is required")
|
|
|
|
|
|
|
|
|
|
db = module.params["db"]
|
|
|
|
|
owner = module.params["owner"]
|
|
|
|
|
template = module.params["template"]
|
|
|
|
|
encoding = module.params["encoding"]
|
|
|
|
|
state = module.params["state"]
|
|
|
|
|
changed = False
|
|
|
|
|
try:
|
|
|
|
@ -79,7 +91,7 @@ def main():
|
|
|
|
|
changed = db_delete(cursor, db)
|
|
|
|
|
else:
|
|
|
|
|
if state == "present":
|
|
|
|
|
changed = db_create(cursor, db)
|
|
|
|
|
changed = db_create(cursor, db, owner, template, encoding)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json(msg="Database query failed: %s" % e)
|
|
|
|
|
|
|
|
|
|