Ansible – In and Out | Manage DB | Loops In Playbooks | Variable Precedence | Part 7

Create database and add a user in MariaDB database

In this, we are going to see how we can create a database and add a user in the MariaDB database. There are lots of database modules in Ansible that we can use and they can be found here

Currently, in our directory in Ansible machine, we have local Ansible config and inventory file which we saw and improved in the previous article.

Let’s first create the playbook db.yml and write some tasks to create a database. Below is the code for our playbook.

Suppose we have a lot of other modules to install or we want to add a list of users, then we would have to write a lot of similar code. Well, that is not a good approach as it will make our playbook very lengthy and not optimized as well. 

Thus, to make it better, we have something called Loops in playbook which simplifies our work. The detailed documentation can be found here. After using the loop, our updated playbook will look like below. 

#######################db.yml############################################

---
- name: Displays DB setup and Vars
  hosts: dbsrvgrp
  become: yes
  gather_facts: False
  tasks:
    - name: Install MariaDB SVC
      yum:
        name: "{{item}}"
        state: present
      loop:
        - mariadb-server
        - MySQL-python
        - unzip
        - git
        - unzip


    - name: start and enable mariadb SVC
      service:
        name: mariadb
        state: started
        enabled: yes


    - name: Add users
      user:
        name: "{{item}}"
        state: present
      loop:
        - deepak
        - amit
        - eshaan
        - rachit


    - name: create a new database with name "accounts"
      mysql_db:
        name: accounts
        state: present

And its a success !!!!!

We can see from the output that all the packages in the first task have been installed and loop has executed 5 times and also our list of users got added to the database server 01. We are done with creating the database, now it ‘s time to add a database user with password. Below is the updated playbook for the same.

#######################db.yml############################################

---
- name: Displays DB setup and Vars
  hosts: dbsrvgrp
  become: yes
  gather_facts: False
  tasks:
    - name: Install MariaDB SVC
      yum:
        name: "{{item}}"
        state: present
      loop:
        - mariadb-server
        - MySQL-python
        - unzip
        - git
        - unzip


    - name: start and enable mariadb SVC
      service:
        name: mariadb
        state: started
        enabled: yes


    - name: Add users
      user:
        name: "{{item}}"
        state: present
      loop:
        - deepak
        - amit
        - eshaan
        - rachit


    - name: create a new database with name "accounts"
      mysql_db:
        name: accounts
        state: present

    - name: create database user with name "admin"
      mysql_user:
        name: admin
        password: 12345
        priv: '*.*:ALL'
        state: present

User has been created successfully. So, we have seen different modules provided by Ansible like user module, mysql_db module, mysql_user module, and loops. Next, we are going to look into the variables and their precedence.

Variables Precedence

There are different types of variables in Ansible and each type has different precedence. So, let’s first understand their precedence and then we will go into the practical examples to demonstrate how do they work.

Below is the order of priority or precedence for the variables. 

  1. Command Line variables 
  2. Playbook variables 
  3. Host_vars/hostname 
  4. Group_vars/groupname 
  5. Group_vars/all 

Few Important Points to Remember,

  • Top priority always goes with the variables defined through the command line using -e option. 
  • Next priority goes for variables defined inside the playbook using vars keyword. 
  • Then priority goes for variables defined in hostname file inside host_vars directory. So, suppose we have a hostname websrv01 then we will create a file with this name under host_vars directory and will define all the variables related to this host.
  • Next priority goes for variables defined in groupname file inside group_vars directory. So, suppose we have a group name WebServersGroup and this group contains 3 web servers then we will create a file with this “WebServersGroup” name under group_vars directory and will define all the variables related to this group. 
  • Last priority goes for variables defined in all file inside group_vars directory and this is a common place for holding all the variables.

About the author

Deepak Sood

Deepak Sood is Lead Consultant in an IT firm holding expertise in Devops and QA Architecture with 8 years of experience.

His expertise is in building highly scalable frameworks. His skills include Java, Configuration Management, Containers, and Kubernetes.

Reach out to him using contact form.

View all posts