Ansible MySQL role – creating databases and users

Ansible MySQL roleWhen you have only one local database server dedicated to your application, there are no problems with creating a database and a user. But the problem appears when we want to create many databases and users manually. Of course we can create a bash script and run it everytime we want to create a new database and user, but there is another and easier way. This way is called Ansible.

Ansible is a really helpful and excellent tool for automation. I have tried to use Ansible anytime since I have heard about it the first time. Some time ago I needed to find a way to create databases and users on my GaleraDB Cluster. At first I though about bash (it’s hard to get rid of that habit), but almost immediately I decided to choose Ansible! I made the appropriate role and of course pushed it into the GitHub. You can find it here.

Ansible MySQL download

If you use Ansible at your work, you can also use Ansible Galaxy to download Ansible MySQL role.

Let’s take a look at some details. Here you can see all of the variables which are needed to set up.

I used “Looping over Hashes” method to create many databases, so you can easily add another set of the database vars and schedule tens of database and user creations at the same time. Users passwords in this case are stored in vars/secure.yml file, which can be encrypt with ansible-vault command.

More specifics you can find in file in my GitHub repository. Of course there are no obstacles for fork my Ansible MySQL role and creating your own versions, adjusted to your necessities! You can also find much more efficient roles, including full MySQL stacks. Or maybe it will be a good idea to use Ansible’s requirements possibility to create chains of some mutually supportive roles? Just try it with yourselves!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.