Skip to content

Commit 3a15b07

Browse files
committed
Allow creation of databases and users
Inspired by the Ansibles.mysql role.
1 parent cbf36a7 commit 3a15b07

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ The home directory inside which Python MySQL settings will be stored, which Ansi
2020

2121
The MySQL root user account password.
2222

23+
mysql_databases: []
24+
25+
The MySQL databases to create. A database has the values `name`, `encoding` (defaults to `utf8`) and `collation` (defaults to `utf8_general_ci`). The formats of these are the same as in the `mysql_db` module.
26+
27+
mysql_users: []
28+
29+
The MySQL users and their privileges. A user has the values `name`, `host` (defaults to `localhost`), `password` and `priv` (defaults to `*.*:USAGE`). The formats of these are the same as in the `mysql_user` module.
30+
2331
mysql_packages:
2432
- mysql
2533
- mysql-server
@@ -59,6 +67,15 @@ None.
5967
*Inside `vars/main.yml`*:
6068

6169
mysql_root_password: super-secure-password
70+
mysql_databases:
71+
- name: example_db
72+
encoding: latin1
73+
collation: latin1_general_ci
74+
mysql_users:
75+
- name: example_user
76+
host: "%"
77+
password: similarly-secure-password
78+
priv: "example_db.*:ALL"
6279

6380
## License
6481

defaults/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ mysql_innodb_lock_wait_timeout: 50
4242

4343
# mysqldump settings
4444
mysql_mysqldump_max_allowed_packet: "64M"
45+
46+
# databases and users settings
47+
mysql_databases: []
48+
mysql_users: []

tasks/main.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,20 @@
7070
mysql_db: >
7171
name="test"
7272
state=absent
73+
74+
- name: Ensure MySQL databases are present.
75+
mysql_db: >
76+
name="{{ item.name }}"
77+
collation="{{ item.collation | default('utf8_general_ci') }}"
78+
encoding="{{ item.encoding | default('utf8') }}"
79+
state=present
80+
with_items: mysql_databases
81+
82+
- name: Ensure MySQL users are present.
83+
mysql_user: >
84+
name="{{ item.name }}"
85+
host="{{ item.host | default('localhost') }}"
86+
password="{{ item.password }}"
87+
priv="{{ item.priv | default('*.*:USAGE') }}"
88+
state=present
89+
with_items: mysql_users

0 commit comments

Comments
 (0)