DEV Community

Kshitij (kd)
Kshitij (kd)

Posted on

Install and Run Mongodb using Ansible


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.


You need to have ansible installed on your system.


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


localhost       ansible_connection=local

XX.YY.ZZ.AA   ansible_connection=ssh  ansible_user=user

XX.YY.ZZ.BB   ansible_connection=ssh ansible_user=user


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

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

    - name: Import public key
        url: ''
        state: present

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

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

    - name: Ensure mongodb is running and and enabled to start automatically on reboots
        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.


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)