Properly install MySQL on production server using ANSIBLE

Properly install MySQL on production server using ANSIBLE

Ansible is a free software platform for configuring and managing computers. It combines multi-node software deployment, ad hoc task execution, and configuration management. It manages nodes over SSH or PowerShell and requires Python to be installed on them. Modules work over JSON and standard output and can be written in any programming language. The system uses YAML to express reusable descriptions of systems.

ANSIBLE is one of the best automation tool that will help you deploy on multiple servers. The main reason is that you don’t need any client installed on the destination server, it uses Python and SSH.
Of course in this tutorial I don’t explain how you install ANSIBLE, but you can refer to their website which is very good explained.
In this playbook I will explain the different steps to install MySQL server on a Linux Debian like server.
In this first part we tell which user we will use that connects to the server using SSH, set the MySQL root password variable(that you need to replace with your own)
- name: Install MySQL for production ready server
  hosts: all
  user: alain
  sudo: True
    MySQL_root_pass: ReplaceWithYourPassword

When we start installing MySQL, it will prompt us for the root password, here we tell which MySQL root password to submit before we start the installation.
    - name: Set MySQL root password before installing
      debconf: name='mysql-server' question='mysql-server/root_password' value='{{MySQL_root_pass | quote}}' vtype='password'

The same for the confirmation.
    - name: Confirm MySQL root password before installing
      debconf: name='mysql-server' question='mysql-server/root_password_again' value='{{MySQL_root_pass | quote}}' vtype='password'

Now we can start installing the packages.
    - name: Install MySQL
      apt: package={{ item }} state=installed force=yes update_cache=yes cache_valid_time=3600
      when: ansible_os_family == 'Debian'
        - mysql-server
        - mysql-client
        - python-mysqldb

Some MySQL security setup, the tasks name is clear enough.
    - name: Deletes anonymous MySQL server user for localhost
      mysql_user: user="" state="absent" login_password="{{ MySQL_root_pass }}" login_user=root
    - name: Secures the MySQL root user
      mysql_user: user="root" password="{{ MySQL_root_pass }}" host="{{ item }}" login_password="{{MySQL_root_pass}}" login_user=root
        - localhost
        - ::1
        - "{{ ansible_fqdn }}"
    - name: Removes the MySQL test database
      mysql_db: db=test state=absent login_password="{{ MySQL_root_pass }}" login_user=root

Now you can adapt this playbook to your needs and maybe get inspired using it for other needs.
Enjoy your coffee ;) .


Like this Article? Share it!

About the Author

Author Gravatar

I would like to change the world, but they won't give me the source code

Related Posts

Comments are closed.