Deploy MongoDB using Ansible-playbook on Ubuntu 22.04 LTS


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).


  • 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:

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
    - name: Import MongoDB GPG key
        state: present

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

    - name: Update apt package cache
        update_cache: yes

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

    - name: Start MongoDB service
        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.


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.


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