Skip to main content
RH294

Installing MariaDB Using Ansible

By January 24, 2020September 12th, 2022No Comments

In this blog we take the time to look at the full story of Ansible by installing MariaDB using Ansible. First we install Ansible on CentOS 8, we need to add the EPEL repository to do this:

$ sudo yum install -y epel-release
$ sudo yum install -y ansible

Within our home directory we can create a project directory. This will become the working directory where we execute the ansible-playbook command and create all files for the project:

$ mkdir $HOME/mariadb
$ cd $HOME/mariadb

Ansible needs a list of hosts to work with. We will create a file call inventory. To begin with we will have just the single localhost listed to test our Playbook development. We include a variable against this host to ensure we don’t use the default SSH connect to this host as it is local.

$ echo '127.0.0.1 ansible_connection=local' > inventory

We can now create a project configuration for ansible called ansible.cfg. We only need to set the inventory location for this project:

$ cat > ansible.cfg <<END
[defaults]
inventory = inventory
END

The configuration is now complete and we can now create the ansible playbook. The playbook defines the desired state of the managed system. In our case, the localhost. We can create the file called mariadb.yml:

---
- name: MariaDB
  hosts: all
  gather_facts: true
  become: true
  vars:
    mysql_root_password: "Password1"
  tasks:
    - name: install mariadb
      yum:
        name:
          - mariadb-server
          - python3-PyMySQL
        state: latest
    - name: start mariadb
      service:
        name: mariadb
        enabled: true
        state: started
    - name: mysql_root_password
      mysql_user:
        login_user: root
        login_password: "{{ mysql_root_password }}"
        user: root
        check_implicit_admin: true
        password: "{{ mysql_root_password }}"
        host: localhost
    - name: remove remote root
      mysql_user:
        check_implicit_admin: true
        login_user: root
        login_password: "{{ mysql_root_password }}"
        user: root
        host: "{{ ansible_fqdn }}"
        state: absent

An Ansible Playbook is formatted with YAML, Yet Another Markup Language. This means that the whitespace is significant and items at the same configuration level should be spaced the same as other sibling elements. The first task installs the database and python interface with the yum module. Then, with the second task, we ensure the service enabled and running. We then set the mysql root user password and ensure, using the final task that remote connections are not enabled for the root account. The video shows the process in more detail.

Installing Mariadb using Ansible is by using the following , we should execute the command from the $HOME/mariadb directory and the ivventory, ansible.cfg and playbook should also be in this directory.

$ ansible-playbook mariadb.yml