|
|
@ -64,15 +64,6 @@ options:
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
default: present
|
|
|
|
choices: [ "present", "absent", "dump", "import" ]
|
|
|
|
choices: [ "present", "absent", "dump", "import" ]
|
|
|
|
all_databases:
|
|
|
|
|
|
|
|
description:
|
|
|
|
|
|
|
|
- May only be provided if I(state) is C(dump) or C(import). (added in Ansible 1.4)
|
|
|
|
|
|
|
|
- Requires I(name) = a valid database name.
|
|
|
|
|
|
|
|
- Uses --all-databases option for mysqldump.
|
|
|
|
|
|
|
|
- When used for import, works similiar to mysql -u <username> -p <pasword> < target.sql
|
|
|
|
|
|
|
|
required: no
|
|
|
|
|
|
|
|
default: false
|
|
|
|
|
|
|
|
choices: [ "yes", "no", "true", "false", "0", "1" ]
|
|
|
|
|
|
|
|
collation:
|
|
|
|
collation:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Collation mode
|
|
|
|
- Collation mode
|
|
|
@ -101,10 +92,6 @@ author: Mark Theunissen
|
|
|
|
EXAMPLES = '''
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a new database with name 'bobdata'
|
|
|
|
# Create a new database with name 'bobdata'
|
|
|
|
- mysql_db: name=bobdata state=present
|
|
|
|
- mysql_db: name=bobdata state=present
|
|
|
|
# Dumps all databases to hostname.sql
|
|
|
|
|
|
|
|
- mysql_db: name=bobdata state=dump all_databases=true target=/tmp/{{ inventory_hostname }}.sql
|
|
|
|
|
|
|
|
# Imports file.sql similiar to mysql -u <username> -p <password> < hostname.sql
|
|
|
|
|
|
|
|
- mysql_db: name=bobdata state=import all_databases=true target=/tmp/{{ inventory_hostname }}.sql
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import ConfigParser
|
|
|
|
import ConfigParser
|
|
|
@ -129,21 +116,13 @@ def db_delete(cursor, db):
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def db_dump(host, user, password, db_name, target, all_databases):
|
|
|
|
def db_dump(host, user, password, db_name, target):
|
|
|
|
if all_databases:
|
|
|
|
|
|
|
|
res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" --all-databases > "
|
|
|
|
|
|
|
|
+target)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" "
|
|
|
|
res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" "
|
|
|
|
+db_name+" > "
|
|
|
|
+db_name+" > "
|
|
|
|
+target)
|
|
|
|
+target)
|
|
|
|
return (res == 0)
|
|
|
|
return (res == 0)
|
|
|
|
|
|
|
|
|
|
|
|
def db_import(host, user, password, db_name, target, all_databases):
|
|
|
|
def db_import(host, user, password, db_name, target):
|
|
|
|
if all_databases:
|
|
|
|
|
|
|
|
res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" < "
|
|
|
|
|
|
|
|
+target)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" "
|
|
|
|
res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" "
|
|
|
|
+db_name+" < "
|
|
|
|
+db_name+" < "
|
|
|
|
+target)
|
|
|
|
+target)
|
|
|
@ -228,7 +207,6 @@ def main():
|
|
|
|
db=dict(required=True, aliases=['name']),
|
|
|
|
db=dict(required=True, aliases=['name']),
|
|
|
|
encoding=dict(default=""),
|
|
|
|
encoding=dict(default=""),
|
|
|
|
collation=dict(default=""),
|
|
|
|
collation=dict(default=""),
|
|
|
|
all_databases=dict(default="no", choices=BOOLEANS, type='bool'),
|
|
|
|
|
|
|
|
target=dict(default=None),
|
|
|
|
target=dict(default=None),
|
|
|
|
state=dict(default="present", choices=["absent", "present","dump", "import"]),
|
|
|
|
state=dict(default="present", choices=["absent", "present","dump", "import"]),
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -238,7 +216,6 @@ def main():
|
|
|
|
module.fail_json(msg="the python mysqldb module is required")
|
|
|
|
module.fail_json(msg="the python mysqldb module is required")
|
|
|
|
|
|
|
|
|
|
|
|
db = module.params["db"]
|
|
|
|
db = module.params["db"]
|
|
|
|
all_databases = module.params["all_databases"]
|
|
|
|
|
|
|
|
encoding = module.params["encoding"]
|
|
|
|
encoding = module.params["encoding"]
|
|
|
|
collation = module.params["collation"]
|
|
|
|
collation = module.params["collation"]
|
|
|
|
state = module.params["state"]
|
|
|
|
state = module.params["state"]
|
|
|
@ -281,11 +258,11 @@ def main():
|
|
|
|
if state == "absent":
|
|
|
|
if state == "absent":
|
|
|
|
changed = db_delete(cursor, db)
|
|
|
|
changed = db_delete(cursor, db)
|
|
|
|
elif state == "dump":
|
|
|
|
elif state == "dump":
|
|
|
|
changed = db_dump(login_host, login_user, login_password, db, target, all_databases)
|
|
|
|
changed = db_dump(login_host, login_user, login_password, db, target)
|
|
|
|
if not changed:
|
|
|
|
if not changed:
|
|
|
|
module.fail_json(msg="dump failed!")
|
|
|
|
module.fail_json(msg="dump failed!")
|
|
|
|
elif state == "import":
|
|
|
|
elif state == "import":
|
|
|
|
changed = db_import(login_host, login_user, login_password, db, target, all_databases)
|
|
|
|
changed = db_import(login_host, login_user, login_password, db, target)
|
|
|
|
if not changed:
|
|
|
|
if not changed:
|
|
|
|
module.fail_json(msg="import failed!")
|
|
|
|
module.fail_json(msg="import failed!")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|