...

Automate MongoDB Setup through Ansible in 2 Simple Easy Steps

5/5 - (1 vote)

Introduction

MongoDB is a popular document-oriented database. This post explains MongoDB setup through Ansible. Installing MongoDB through Ansible offers a streamlined and automated approach, simplifying the deployment of this powerful NoSQL database.

Ansible, an open-source automation tool, allows for seamless configuration management, making MongoDB installation more efficient and scalable across multiple servers or environments. Administrators can automate repetitive tasks by leveraging Ansible’s declarative language and playbooks, ensuring consistent and error-free setups.

This guide will delve into the step-by-step process of deploying MongoDB using Ansible, enabling users to harness the benefits of automation, reduce manual configurations, and expedite the setup of their database infrastructure with ease and reliability.

Prerequisites:

  • Basic guide to start with Ansible.
MongoDB setup through Ansible
MongoDB setup through Ansible

Break down of MongoDB setup through Ansible

  • Add MongoDB gpg key
  • Add MongoDB sources list (equivalent to sudo apt update)
  • Install the MongoDB server and tool package
  • Create /var/log/MongoDB directory (this is not auto-created on all systems)
  • Customise mongod configuration by creating /etc/mongod.conf(optional)
  • Create a MongoDB group and user
  • Start mongod service with reloading configuration and enabling it also.

Please see this in action below.

# mongodb-playbook.yml

---
- name: Install MongoDB 6 on debian (please make minor changes as instructed for ubuntu)
  hosts: mongo_servers
  become: yes
  vars:
    mongodb_version: '6.0'
    mongodb_keyserver: 'hkp://keyserver.ubuntu.com:80'
    mongodb_keyid: 'B00A0BD1E2C63C11'
  tasks:
    - name: Add MongoDB GPG key
      apt_key:
        keyserver: '{{ mongodb_keyserver }}'
        id: '{{ mongodb_keyid }}'
        state: present

###### Please change repo value as below on ubuntu systems
  repo: deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu {{ ansible_lsb.codename }}/mongodb-org/{{ mongodb_version }} multiverse

    - name: Add MongoDB repository
      apt_repository:
        repo: deb [arch=amd64] https://repo.mongodb.org/apt/debian bullseye/mongodb-org/{{ mongodb_version }} main
        state: present
      ignore_errors: yes

    - name: Install MongoDB packages (mongodb-org-shell is optional)
      apt:
        name:
          - mongodb-org-server
          - mongodb-org-shell
        state: present
        update_cache: yes

    - name: create /var/log/mongodb directory
      file:
        name: /var/log/mongodb
        state: directory

    - name: create /var/log/mongodb/mongod.log file
      file:
        name: /var/log/mongodb/mongod.log
        state: touch
        mode: '0666'

    - name: Create /etc/mongod.conf file
      template:
        src: ../templates/mongod.conf.j2
        dest: /etc/mongod.conf
        mode: '0644'
      become: yes

    - name: Create mongodb group
      group:
        name: mongodb
        system: yes

    - name: Create mongodb user
      user:
        name: mongodb
        system: yes
        create_home: no        
        shell: /bin/false

    - name: Start mongod service, also reload daemon-first and enable this to start on system reboot
      systemd:
        name: mongod
        state: started
        enabled: yes
        daemon_reload: yes


Execution of MongoDB Setup

Precondition: ansible should be installed.

go into the root dir of ansible.
Let us say it is ansible so go inside it using cd /path/to/ansible

Create a folder named playbooks using mkdir playbooks.


Now create the file mongodb-playbook.yml and save the above scripts in it.
create inventory.yml file with the below contents

# inventory.yml
---
all:
  children:
    mongo_servers:
      hosts:
        mongo-server-01:
        mongo-server-02:

run the command: ansible-playbook playbooks/mongodb-playbook.yml -i inventory.yml

If you run into any issues, Please work it out yourself or let me know.

Exercise

You can convert the above setup into a role-based setup. Please go through Role in Ansible first and try it yourself.

You may further extend the role-based solution to set up different versions of MongoDB for different servers.

Doing such exercise will build your strength in ansible and ultimately make your life easy.

Frequently Asked Questions on MongoDB setup through Ansible.

Q1 – I don’t have ansible installed on my system.

Answer – Please go through the prerequisites and follow the guide on installing Ansible.

Q2 – My playbook execution failed with errors.

Answer – Ansible will throw details on errors on your terminal. Please investigate that first. If details are not sufficient then increase the verbosity level with -vv, -vvv or -vvvv on playbook execution as ansible-playbook playbooks/mongodb-playbook.yml -i inventory.yml -vvv

Conclusion

In this post, you learned about MongoDB setup through Ansible. We focused on playbook setup, variable setup, and inventory setup.

You can customize the above setup as per your requirements. e.g. – MongoDB version update.

Once you have automated the MongoDB setup, you can execute the above setup on any number of servers without any issues.

Hope you will enjoy this post!!

Spread the love

1 thought on “Automate MongoDB Setup through Ansible in 2 Simple Easy Steps”

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.