2012-07-12 01:00:55 +02:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-07-12 01:00:55 +02:00
|
|
|
|
|
|
|
# (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/>.
|
|
|
|
|
2012-09-29 16:15:52 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: mysql_db
|
|
|
|
short_description: Add or remove MySQL databases from a remote host.
|
|
|
|
description:
|
|
|
|
- Add or remove MySQL databases from a remote host.
|
|
|
|
version_added: "0.6"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- name of the database to add or remove
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
login_user:
|
|
|
|
description:
|
|
|
|
- The username used to authenticate with
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
login_password:
|
|
|
|
description:
|
2012-09-29 16:43:21 +02:00
|
|
|
- The password used to authenticate with
|
2012-09-29 16:15:52 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
login_host:
|
|
|
|
description:
|
|
|
|
- Host running the database
|
|
|
|
required: false
|
|
|
|
default: localhost
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The database state
|
|
|
|
required: false
|
|
|
|
default: present
|
2012-10-11 05:28:42 +02:00
|
|
|
choices: [ "present", "absent", "dump", "import" ]
|
2012-09-29 16:15:52 +02:00
|
|
|
collation:
|
|
|
|
description:
|
|
|
|
- Collation mode
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
encoding:
|
|
|
|
description:
|
|
|
|
- Encoding mode
|
|
|
|
required: false
|
|
|
|
default: null
|
2012-10-11 05:28:42 +02:00
|
|
|
target:
|
|
|
|
description:
|
|
|
|
- Where to dump/get the .sql file
|
|
|
|
required: true
|
2012-09-29 16:15:52 +02:00
|
|
|
examples:
|
2012-10-23 15:14:01 +02:00
|
|
|
- code: "mysql_db: db=bobdata state=present"
|
2012-10-01 09:18:54 +02:00
|
|
|
description: Create a new database with name 'bobdata'
|
2012-09-29 16:15:52 +02:00
|
|
|
notes:
|
2012-10-01 09:18:54 +02:00
|
|
|
- Requires the MySQLdb Python package on the remote host. For Ubuntu, this
|
|
|
|
is as easy as apt-get install python-mysqldb.
|
|
|
|
- Both C(login_password) and C(login_username) are required when you are
|
|
|
|
passing credentials. If none are present, the module will attempt to read
|
|
|
|
the credentials from C(~/.my.cnf), and finally fall back to using the MySQL
|
|
|
|
default login of 'root' with no password.
|
2012-09-29 16:15:52 +02:00
|
|
|
requirements: [ ConfigParser ]
|
|
|
|
author: Mark Theunissen
|
|
|
|
'''
|
|
|
|
|
2012-07-25 23:31:12 +02:00
|
|
|
import ConfigParser
|
2012-10-11 05:28:42 +02:00
|
|
|
import os
|
2012-07-12 01:00:55 +02:00
|
|
|
try:
|
2012-07-22 01:02:34 +02:00
|
|
|
import MySQLdb
|
2012-07-12 01:00:55 +02:00
|
|
|
except ImportError:
|
2012-07-22 01:02:34 +02:00
|
|
|
mysqldb_found = False
|
|
|
|
else:
|
|
|
|
mysqldb_found = True
|
2012-07-12 01:00:55 +02:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# MySQL module specific support methods.
|
|
|
|
#
|
|
|
|
|
2012-07-22 01:02:34 +02:00
|
|
|
def db_exists(cursor, db):
|
2012-07-12 01:00:55 +02:00
|
|
|
res = cursor.execute("SHOW DATABASES LIKE %s", (db,))
|
|
|
|
return bool(res)
|
|
|
|
|
2012-07-22 01:02:34 +02:00
|
|
|
def db_delete(cursor, db):
|
2012-07-12 01:00:55 +02:00
|
|
|
query = "DROP DATABASE %s" % db
|
|
|
|
cursor.execute(query)
|
|
|
|
return True
|
|
|
|
|
2012-10-11 05:28:42 +02:00
|
|
|
def db_dump(user, password, db_name, target):
|
|
|
|
res = os.system("/usr/bin/mysqldump -q -u "+user+ " -p"+password+" "
|
|
|
|
+db_name+" > "
|
|
|
|
+target)
|
|
|
|
return (res == 0)
|
|
|
|
|
|
|
|
def db_import(user, password, db_name, target):
|
|
|
|
res = os.system("/usr/bin/mysql -u "+user+ " -p"+password+" "
|
|
|
|
+db_name+" < "
|
|
|
|
+target)
|
|
|
|
return (res == 0)
|
|
|
|
|
2012-07-31 11:56:29 +02:00
|
|
|
def db_create(cursor, db, encoding, collation):
|
|
|
|
if encoding:
|
|
|
|
encoding = " CHARACTER SET %s" % encoding
|
|
|
|
if collation:
|
|
|
|
collation = " COLLATE %s" % collation
|
|
|
|
query = "CREATE DATABASE %s%s%s" % (db, encoding, collation)
|
2012-07-12 01:00:55 +02:00
|
|
|
res = cursor.execute(query)
|
|
|
|
return True
|
|
|
|
|
2012-07-25 23:31:12 +02:00
|
|
|
def load_mycnf():
|
|
|
|
config = ConfigParser.RawConfigParser()
|
|
|
|
mycnf = os.path.expanduser('~/.my.cnf')
|
2012-07-26 18:30:22 +02:00
|
|
|
if not os.path.exists(mycnf):
|
|
|
|
return False
|
2012-07-25 23:31:12 +02:00
|
|
|
try:
|
2012-07-26 15:58:21 +02:00
|
|
|
config.readfp(open(mycnf))
|
2012-07-25 23:31:12 +02:00
|
|
|
creds = dict(user=config.get('client', 'user'),passwd=config.get('client', 'pass'))
|
2012-07-26 15:58:21 +02:00
|
|
|
except (ConfigParser.NoOptionError, IOError):
|
2012-07-25 23:31:12 +02:00
|
|
|
return False
|
|
|
|
return creds
|
|
|
|
|
2012-07-12 01:00:55 +02:00
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
2012-07-22 01:02:34 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2012-07-31 00:15:24 +02:00
|
|
|
login_user=dict(default=None),
|
|
|
|
login_password=dict(default=None),
|
|
|
|
login_host=dict(default="localhost"),
|
2012-08-03 12:34:55 +02:00
|
|
|
login_unix_socket=dict(default=None),
|
2012-08-01 06:21:36 +02:00
|
|
|
db=dict(required=True, aliases=['name']),
|
2012-07-31 11:56:29 +02:00
|
|
|
encoding=dict(default=""),
|
|
|
|
collation=dict(default=""),
|
2012-10-11 05:28:42 +02:00
|
|
|
target=dict(default=None),
|
|
|
|
state=dict(default="present", choices=["absent", "present","dump", "import"]),
|
2012-07-22 01:02:34 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not mysqldb_found:
|
|
|
|
module.fail_json(msg="the python mysqldb module is required")
|
|
|
|
|
|
|
|
db = module.params["db"]
|
2012-07-31 11:56:29 +02:00
|
|
|
encoding = module.params["encoding"]
|
|
|
|
collation = module.params["collation"]
|
2012-07-22 01:02:34 +02:00
|
|
|
state = module.params["state"]
|
2012-10-11 05:28:42 +02:00
|
|
|
target = module.params["target"]
|
2012-07-25 23:31:12 +02:00
|
|
|
|
|
|
|
# Either the caller passes both a username and password with which to connect to
|
|
|
|
# mysql, or they pass neither and allow this module to read the credentials from
|
|
|
|
# ~/.my.cnf.
|
2012-07-31 00:15:24 +02:00
|
|
|
login_password = module.params["login_password"]
|
|
|
|
login_user = module.params["login_user"]
|
|
|
|
if login_user is None and login_password is None:
|
2012-07-25 23:31:12 +02:00
|
|
|
mycnf_creds = load_mycnf()
|
|
|
|
if mycnf_creds is False:
|
2012-07-31 00:15:24 +02:00
|
|
|
login_user = "root"
|
|
|
|
login_password = ""
|
2012-07-25 23:31:12 +02:00
|
|
|
else:
|
2012-07-31 00:15:24 +02:00
|
|
|
login_user = mycnf_creds["user"]
|
|
|
|
login_password = mycnf_creds["passwd"]
|
|
|
|
elif login_password is None or login_user is None:
|
|
|
|
module.fail_json(msg="when supplying login arguments, both login_user and login_password must be provided")
|
2012-07-25 23:31:12 +02:00
|
|
|
|
2012-10-12 22:26:59 +02:00
|
|
|
if state in ['dump','import']:
|
|
|
|
if target is None:
|
|
|
|
module.fail_json(msg="with state={0} target is required".format(state))
|
|
|
|
connect_to_db = db
|
|
|
|
else:
|
|
|
|
connect_to_db = 'mysql'
|
2012-07-12 01:00:55 +02:00
|
|
|
try:
|
2012-08-03 12:34:55 +02:00
|
|
|
if module.params["login_unix_socket"] != None:
|
2012-10-12 22:26:59 +02:00
|
|
|
db_connection = MySQLdb.connect(host=module.params["login_host"], unix_socket=module.params["login_unix_socket"], user=login_user, passwd=login_password, db=connect_to_db)
|
2012-08-03 12:34:55 +02:00
|
|
|
else:
|
2012-10-12 22:26:59 +02:00
|
|
|
db_connection = MySQLdb.connect(host=module.params["login_host"], user=login_user, passwd=login_password, db=connect_to_db)
|
2012-07-12 01:00:55 +02:00
|
|
|
cursor = db_connection.cursor()
|
|
|
|
except Exception as e:
|
2012-07-31 00:15:24 +02:00
|
|
|
module.fail_json(msg="unable to connect, check login_user and login_password are correct, or alternatively check ~/.my.cnf contains credentials")
|
2012-07-12 01:00:55 +02:00
|
|
|
|
2012-07-25 23:31:12 +02:00
|
|
|
changed = False
|
2012-07-22 01:02:34 +02:00
|
|
|
if db_exists(cursor, db):
|
2012-07-12 01:00:55 +02:00
|
|
|
if state == "absent":
|
2012-07-22 01:02:34 +02:00
|
|
|
changed = db_delete(cursor, db)
|
2012-10-11 05:28:42 +02:00
|
|
|
elif state == "dump":
|
|
|
|
changed = db_dump(login_user, login_password, db, target)
|
|
|
|
if not changed:
|
|
|
|
module.fail_json(msg="dump failed!")
|
|
|
|
elif state == "import":
|
|
|
|
changed = db_import(login_user, login_password, db, target)
|
|
|
|
if not changed:
|
|
|
|
module.fail_json(msg="import failed!")
|
2012-07-12 01:00:55 +02:00
|
|
|
else:
|
|
|
|
if state == "present":
|
2012-07-31 11:56:29 +02:00
|
|
|
changed = db_create(cursor, db, encoding, collation)
|
2012-07-22 01:02:34 +02:00
|
|
|
|
|
|
|
module.exit_json(changed=changed, db=db)
|
2012-07-12 01:00:55 +02:00
|
|
|
|
2012-07-22 01:02:34 +02:00
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|