eslam-gomaa / mysql_secure_installation_Ansible

Idempotent Ansible Module that provides the functions of "mysql_secure_installation" script
43 stars 22 forks source link
ansible automation module mysql playbook python

mysql_secure_installation_Ansible



✋ Wait a second !

This module has been refactored to get rid of the dependencies issue across distributions, And it's much simpler now.

Updates

To do,


Features

An Idempotent Ansible Module that provides the functions of mysql_secure_installation

💎 The Module is Idempotent Means that when you run it again, will not re-execute the commands If the desired state meets the current state


Dependencies

This is NOT something to worry about, It is something to make sure it's meet if you faced an error

  1. mysqladmin command (already installed with MySQL/Mariadb) -- Needed to get information such as unix_socket location & MySQL version
  2. python-pymysql which can be easily installed using the pkg manager e.g: apt, yum
    • The only caveat is that this package name may differ between distributions e.g: python3-pymysql or python36-pymysql (Trying to cover all the possible differences in the example provided)

Usage

💎 A full sample is provided at sample-playbook.yml which installs & secures MySQL -- Workes on the tested distributions below

# Modify the hosts
ansible-playbook sample-playbook.yml

cd my_playbook_folder
# OR
# cd my_role_folder
mkdir library
cp mysql_secure_installation.py library/

- name: test mysql_secure_installation
  mysql_secure_installation:
    login_password: ''
    new_password: password22
    user: root
    login_host: localhost
    hosts: ['localhost', '127.0.0.1', '::1']
    change_root_password: true
    remove_anonymous_user: true
    disallow_root_login_remotely: true
    remove_test_db: true
  register: mysql_secure

# To see detailed output
- debug:
    var: mysql_secure
- name: test mysql_secure_installation
  mysql_secure_installation:
    login_password: password22
    new_password: password23
    user: root
    login_host: localhost
    hosts: ['localhost', '127.0.0.1', '::1']

Test

Below, is a list of the tested distributions

📌 The tests are done by an automated Jenkins pipeline

Distribution Test result Comment
Centos 7
Centos 8
fedora-34
Debian 10
Debian 11
Ubuntu 16.04 Make sure you're not using an old version (Related to example syntax)
Ubuntu 18.04
Ubuntu 20.04

💎 I'll be more than happy when you let me know if you faced an error !



Updates / News

as of Mariadb v10.4+ we can not use update mysql.user > Currently that affects disable_unix_socket option for Mariadb versions above 10.4 (need some investigation & will update the module)

Error produced

ERROR 1356 (HY000): View 'mysql.user' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them



Input

:Param :Description :Default :Type
login_password Root's password to login to MySQL String
new_password New desired Root password String
user MySQL user root String
login_host host to connect to localhost String
hosts List of hosts for the provided user i.e ['localhost', '127.0.0.1', '::1'], Note: all will have the same new password [‘localhost’] List
change_root_password True Boolean
remove_anonymous_user True Boolean
disallow_root_login_remotely False Boolean
remove_test_db True Boolean
disable_unix_socket Disable login with unix_socket False Boolean

Debug Output

Sample output

image

image



Known issues

No package matching 'python*-pymysql' is available

If you face this, don't worry it's NOT an issue, the problem is that python-pymysql might has a different name on the distro you're using

You'll probably face this if you are using a non tested distribution

image

Fix

Just Update pymysql package name with the correct name in the playbook

image

Extra mile

It is much appreciated ♥️ if you share package name on your tested distribution either with opening an issue or a pull request.


Read the Module’s Documentation

cd playbook_directory
# OR
cd role_directory
ansible-doc -M library mysql_secure_installation -v


Please leave a ⭐ if you found it useful


Thank you

Maintainer: Eslam Gomaa