Hi Readers, this document deals with how to setup ansible role for mongodb server.
Ansible is open source software that automates software provisioning, configuration management, and application deployment. Ansible connects via SSH, remote PowerShell or via other remote APIs.

Here is the setup.

Initialize role set up for MongoDB server using ansible galaxy

ansible-galaxy init mongodbserver

once the command is executed, folder structures are automatically created for the role.



└──╼ $tree mongodbserver/
mongodbserver/
├── defaults
│   └── main.yml
├── files
│   └── mongodb-org-3.6.repo
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── centos.yml
│   ├── debian.yml
│   └── main.yml
├── templates
│   ├── mongodc.j2
│   └── mongodd.j2
├── tests
│   ├── inventory
│   └── test.yml
└── vars
└── main.yml

create repository file for MongoDB

mongodbserver/files/mongodb-org-3.6.repo

vi files/mongodb-org-3.6.repo

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

Create a handler for restarting the service

mongodbserver/handlers/main.yml

 - name: start
   service: name=mongod state=restarted

Create task files for MongoDB

mongodbserver/tasks/main.yml


 - include_tasks: debian.yml
   when: ansible_os_family == 'Debian'
 - include_tasks: centos.yml
   when: ansible_os_family == 'RedHat'

Create task file for centos mongodbserver/tasks/centos.yml

---
# tasks file for mongocentos
 - name: Copy mongodb repository 
   copy: src=files/mongodb-org-3.6.repo dest=/etc/yum.repos.d/mongodb-org-3.6.repo
 - name: Install mongodb
   yum: name=mongodb-org state=latest
 - name: remove mongod default a file
   file:
     path: /etc/mongod.conf
     state: absent
 - name: mongod conf copy
   template:
    src: mongodc.j2
    dest: /etc/mongod.conf
   notify:
   - start

Create task file for debian

mongodbserver/tasks/debian.yml

---
# tasks file for mongodebian
 - name: add MongoDB GPG key
   apt_key:
     keyserver: keyserver.ubuntu.com
     id: 9DA31620334BD75D9DCB49F368818C72E52529D4
 - apt_repository:
    repo: deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse
    state: present
    filename: mongodb-org-4.0.list
 - name: Update repositories cache and install "mongodb" package
   apt: name=mongodb-org state=present update_cache=true
 - name: remove a file
   file:
     path: /etc/mongod.conf
     state: absent
 - name: mongo conf copy
   template:
    src: mongodd.j2
    dest: /etc/mongod.conf
   notify:
   - start
   

Create Template files having monodb configuration.

For centos mongodbserver/templates/mongodc.j2


# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: {{ bind }}  # Listen to local interface only, comment to listen on all interfaces.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

 

For debian mongodbserver/templates/mongodd.j2

 

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: {{ bind }}


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

 

 

create bind variable mongodbserver/vars/main.yml

mongodbserver/vars/main.yml

---
# vars file for mongodbserver
bind: 0.0.0.0

Finally create the playbook for MongoDB mongodb.yml


mongodb.yml

---
 - hosts: local
   gather_facts: true
   become: true
   become_user: root
   become_method: sudo
   roles:
    - mongodbserver

 

Finally run the create playbook by executing following command,

ansible-playbook mongodb.yml

 

 

© 2018, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.