Installing MySQL on Centos 7 server using Ansible.

November 20, 2016

This is the content of the inventory file on my Ansible server. The remote server is called test1 under the group web-server.

[root@ansible work]# cat hosts

The playbook is called mysql-repo.yml. This will install the MySQL Community Repo, you need this to install MySQL. CentOS7 is shipped with MariaDB but since I am familiar with MySQL I decided to install that instead.

The playbook will complete the below tasks:
(1) Download the MySQL community repo rpm.
(2) Install MySQL community repo.
(3) Install MySQL server.
(4) Install MySQL-python as this is required to change the root passwords furthe in the playbook.
(5) Enable and start MySQL server.
(6) Some version of MySQl installation creates the database test, remove it if found.
(7) Remove all anonymous user accounts.
(8) Change the root password for all MySQL hosts.



Below is the content of mysql-repo.yml

[root@ansible work]# cat mysql-repo.yml

– name: Install Packages
hosts: web-server
root_db_password: Password123


– name: Download MySQL Community Repo
dest: /tmp

– name: Install MySQL Community Repo
command: /usr/bin/rpm -ivh /tmp/mysql-community-release-el7-7.noarch.rpm

– name: Install MySQL Server
yum: name=mysql-server state=present

– name: Install MySQL-python, this is required for the task remove anonymous user
yum: name=MySQL-python state=present

– name: Start MySQL Server and enable it
service: name=mysqld state=started enabled=yes

– name: Remove Test database if it exist.
mysql_db: name=test state=absent

– name: Remove All Anonymous User Accounts
mysql_user: name=” host_all=yes state=absent

# – name: Output ansible_hostname
# debug: msg=”Hostname is {{ansible_hostname}}”

– name: Change root password
mysql_user: name=root host={{item}} password={{root_db_password}}
– “{{ansible_hostname}}”
– ::1
– localhost

To run the playbook use the command ansible-playbook.

[root@ansible work]# ansible-playbook -u root –ask-pass -i ./hosts mysql-repo.yml
SSH password:

PLAY [Install Packages] ********************************************************

TASK [setup] *******************************************************************
ok: [test1]

TASK [Download MySQL Community Repo] *******************************************
changed: [test1]

TASK [Install MySQL Community Repo] ********************************************
changed: [test1]
[WARNING]: Consider using yum, dnf or zypper module rather than running rpm

TASK [Install MySQL Server] ****************************************************
changed: [test1]

TASK [Install MySQL-python, this is required for the task remove anonymous user] ***
changed: [test1]

TASK [Start MySQL Server and enable it] ****************************************
changed: [test1]

TASK [Remove Test database if it exist.] ***************************************
ok: [test1]

TASK [Remove All Anonymous User Accounts] **************************************
changed: [test1]

TASK [Change root password] ****************************************************
changed: [test1] => (item=test1)
changed: [test1] => (item=
changed: [test1] => (item=::1)
changed: [test1] => (item=localhost)

PLAY RECAP *********************************************************************
test1 : ok=9 changed=7 unreachable=0 failed=0

Be Sociable, Share!


Leave a Reply