Deploy MongoDB using Ansible-playbook on Ubuntu 22.04 LTS

Introduction

MongoDB is a popular, open-source NoSQL database management system that falls under the category of document-oriented databases. Developed by MongoDB Inc., it provides a flexible and scalable solution for storing and retrieving data in a way that is different from traditional relational database management systems (RDBMS).

Prerequisites

  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in Linux commands.
  • Internet connectivity.
  • Ansible should be installed.

We can use the given link to install Ansible on Ubuntu 22.04 LTS Linux machine.

In this post, We will show you how to deploy MongoDB using Ansible-play on ubuntu 22.04 LTS Linux machine.

Step 1: Create an Ansible playbook

To deploy MongoDB using Ansible on Ubuntu 22.04 LTS, you can create an Ansible playbook. Below is a simple example playbook that installs MongoDB and starts the MongoDB service. Make sure you have Ansible installed on your local machine before running the playbook.

Create an inventory file (e.g., inventory.ini) with the target server’s IP address:

[mongodb]
your_server_ip ansible_ssh_user=your_ssh_user

Replace your_server_ip and your_ssh_user with the appropriate values.

To create ansible playbook.

sudo nano deploy_mongodb.yml

Paste the fallowing ansible tasks.

---
- name: Deploy MongoDB on Ubuntu 22.04
  hosts: mongodb
  become: true
  tasks:
    - name: Import MongoDB GPG key
      apt_key:
        url: https://www.mongodb.org/static/pgp/server-5.0.asc
        state: present

    - name: Add MongoDB repository
      apt_repository:
        repo: "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse"
        state: present

    - name: Update apt package cache
      apt:
        update_cache: yes

    - name: Install MongoDB
      apt:
        name: mongodb-org
        state: present

    - name: Start MongoDB service
      systemd:
        name: mongod
        state: started
        enabled: yes

Step 2: Run the playbook

Run the playbook using the following command:

ansible-playbook -i inventory.ini deploy_mongodb.yml

This playbook performs the following tasks:

  • Imports the MongoDB GPG key.
  • Adds the MongoDB repository to the system.
  • Updates the package cache.
  • Installs MongoDB.
  • Starts the MongoDB service.

You can customize the playbook according to your specific requirements. MongoDB has various configuration options that you may want to adjust based on your needs. Additionally, consider securing your MongoDB installation by configuring authentication and other security measures.

Conclusion

We have successfully deployed MongoDB database server using Ansible playbook on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.

Author

Deploy MongoDB using Ansible-playbook on Ubuntu 22.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top