2012-07-26 17:02:28 +02:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-07-26 17:02:28 +02:00
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
import psycopg2
|
|
|
|
except ImportError:
|
|
|
|
postgresqldb_found = False
|
|
|
|
else:
|
|
|
|
postgresqldb_found = True
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# PostgreSQL module specific support methods.
|
|
|
|
#
|
|
|
|
|
|
|
|
def db_exists(cursor, db):
|
|
|
|
query = "SELECT * FROM pg_database WHERE datname=%(db)s"
|
|
|
|
cursor.execute(query, {'db': db})
|
|
|
|
return cursor.rowcount == 1
|
|
|
|
|
|
|
|
def db_delete(cursor, db):
|
|
|
|
query = "DROP DATABASE %s" % db
|
|
|
|
cursor.execute(query)
|
|
|
|
return True
|
|
|
|
|
2012-07-31 11:56:29 +02:00
|
|
|
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)
|
2012-07-26 17:02:28 +02:00
|
|
|
cursor.execute(query)
|
|
|
|
return True
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2012-07-29 18:47:44 +02:00
|
|
|
login_user=dict(default="postgres"),
|
|
|
|
login_password=dict(default=""),
|
|
|
|
login_host=dict(default=""),
|
2012-08-01 06:21:36 +02:00
|
|
|
db=dict(required=True, aliases=['name']),
|
2012-07-31 11:56:29 +02:00
|
|
|
owner=dict(default=""),
|
|
|
|
template=dict(default=""),
|
|
|
|
encoding=dict(default=""),
|
2012-07-26 17:02:28 +02:00
|
|
|
state=dict(default="present", choices=["absent", "present"]),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not postgresqldb_found:
|
|
|
|
module.fail_json(msg="the python psycopg2 module is required")
|
|
|
|
|
|
|
|
db = module.params["db"]
|
2012-07-31 11:56:29 +02:00
|
|
|
owner = module.params["owner"]
|
|
|
|
template = module.params["template"]
|
|
|
|
encoding = module.params["encoding"]
|
2012-07-26 17:02:28 +02:00
|
|
|
state = module.params["state"]
|
|
|
|
changed = False
|
|
|
|
try:
|
2012-07-29 18:47:44 +02:00
|
|
|
db_connection = psycopg2.connect(host=module.params["login_host"],
|
|
|
|
user=module.params["login_user"],
|
|
|
|
password=module.params["login_password"],
|
2012-07-26 17:02:28 +02:00
|
|
|
database="template1")
|
|
|
|
# Enable autocommit so we can create databases
|
|
|
|
db_connection.autocommit = True
|
|
|
|
cursor = db_connection.cursor()
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="unable to connect to database: %s" % e)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if db_exists(cursor, db):
|
|
|
|
if state == "absent":
|
|
|
|
changed = db_delete(cursor, db)
|
|
|
|
else:
|
|
|
|
if state == "present":
|
2012-07-31 11:56:29 +02:00
|
|
|
changed = db_create(cursor, db, owner, template, encoding)
|
2012-07-26 17:02:28 +02:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Database query failed: %s" % e)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, db=db)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|