mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
#!/usr/bin/python
|
|
|
|
# (c) 2012, Mark Theunissen <mark.theunissen@gmail.com>
|
|
# Sponsored by Four Kitchens http://fourkitchens.com.
|
|
#
|
|
# 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 MySQLdb
|
|
except ImportError:
|
|
mysqldb_found = False
|
|
else:
|
|
mysqldb_found = True
|
|
|
|
# ===========================================
|
|
# MySQL module specific support methods.
|
|
#
|
|
|
|
def db_exists(cursor, db):
|
|
res = cursor.execute("SHOW DATABASES LIKE %s", (db,))
|
|
return bool(res)
|
|
|
|
def db_delete(cursor, db):
|
|
query = "DROP DATABASE %s" % db
|
|
cursor.execute(query)
|
|
return True
|
|
|
|
def db_create(cursor, db):
|
|
query = "CREATE DATABASE %s" % db
|
|
res = cursor.execute(query)
|
|
return True
|
|
|
|
# ===========================================
|
|
# Module execution.
|
|
#
|
|
|
|
def main():
|
|
module = AnsibleModule(
|
|
argument_spec = dict(
|
|
loginuser=dict(default="root"),
|
|
loginpass=dict(default=""),
|
|
loginhost=dict(default="localhost"),
|
|
db=dict(required=True),
|
|
state=dict(default="present", choices=["absent", "present"]),
|
|
)
|
|
)
|
|
|
|
if not mysqldb_found:
|
|
module.fail_json(msg="the python mysqldb module is required")
|
|
|
|
db = module.params["db"]
|
|
state = module.params["state"]
|
|
changed = False
|
|
try:
|
|
db_connection = MySQLdb.connect(host=module.params["loginhost"], user=module.params["loginuser"], passwd=module.params["loginpass"], db="mysql")
|
|
cursor = db_connection.cursor()
|
|
except Exception as e:
|
|
module.fail_json(msg="unable to connect to database")
|
|
|
|
if db_exists(cursor, db):
|
|
if state == "absent":
|
|
changed = db_delete(cursor, db)
|
|
else:
|
|
if state == "present":
|
|
changed = db_create(cursor, db)
|
|
|
|
module.exit_json(changed=changed, db=db)
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
main()
|