DEV Community

Kshitij (kd)
Kshitij (kd)

Posted on

Install and Run Mongodb using Ansible

Introduction

As a person who has to do several on-premise deployments, at a place where docker is not accepted yet, I have had several issues installing mongodb. Even if I am installing it in the same environment, or upgrading it, I face several problems regarding permissions, ownerships, lockfiles etc.
In this article I will share an automated way through which you can install mongodb (version 4.2). Yes, we will be using ansible.

Pre-requisites

You need to have ansible installed on your system.

Hosts

When you run an ansible-playbook command, you may or may not mention the host file which has the all the required host information. Lets create a new file named mongo-hosts and enter name of all the servers on which you need mongodb installed

mongo-hosts

[local]
localhost       ansible_connection=local

[mongo-server-1]
XX.YY.ZZ.AA   ansible_connection=ssh  ansible_user=user

[mongo-server-2]
XX.YY.ZZ.BB   ansible_connection=ssh ansible_user=user

[mongo-servers:children]
local
mongo-server-1
mongo-server-2

Enter fullscreen mode Exit fullscreen mode

We have mentioned all the instances, with their connection type and username.
Here you can see that I have grouped all the servers at the end. So anywhere mongo-servers is mentioned in playbook,All the child instances will be considered.

Playbook - mongo-playbook.yml

- hosts: mongo-servers
  become: true
  serial: 1

  tasks:
    - name: Install aptitude using apt
      apt:
        name: aptitude
        state: latest
        update_cache: yes

    - name: Import public key
      apt_key:
        url: 'https://www.mongodb.org/static/pgp/server-4.2.asc'
        state: present

    - name: Add repository
      apt_repository:
        filename: '/etc/apt/sources.list.d/mongodb-org-4.2.list'
        repo: 'deb https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse'
        state: present
        update_cache: yes

    - name: Install mongoDB
      apt:
        name: mongodb-org
        state: present
        update_cache: yes

    - name: Ensure mongodb is running and and enabled to start automatically on reboots
      service:
        name: mongod
        enabled: yes
        state: started

Enter fullscreen mode Exit fullscreen mode

So here as we can see

  • hosts represent the instances on which this playbook will run.
  • become parameter is a privilege escalation setting as some commands we would only be able to run if the user is sudo. Note: These commands are only possible if the user through which ansible logs into the server is a sudo user.
  • serial parameter is for controlling how the playbook will be executed. In our case, this would mean that we are going to run the playbook on all the servers serially.
  • tasks are the steps that ansible will take on each server. These are the commands that we generally run to install mongodb manually.

Execution:

ansible-playbook mongo-playbook.yml -vvvv -i ./mongo-hosts 
Enter fullscreen mode Exit fullscreen mode

-vvvv is used to run the ansible playbook in verbose mode.
If we dont mention the hosts file(using i flag) , ansible-playbook will pick from the default one. In case of Ubuntu, that is /var/ansible/hosts

And thats it! This is a really basic case which only touches a simple installation. Let me know if you need examples of more complex ones (Primary,secondary,arbiter or the same using docker)

Discussion (0)