Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

386 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ansible role for MongoDB Build Status

Ansible role which manages MongoDB.

  • Install and configure the MongoDB;
  • Configure mongodb users
  • Configure replication
  • Provide handlers for restart and reload;
  • Setup MMS authomation agent;

MongoDB support matrix:

Distribution < MongoDB 3.0 MongoDB 3.2 MongoDB 3.4 MongoDB 3.6 MongoDB 4.0 MongoDB 4.2 MongoDB 4.4 MongoDB 5.0
Ubuntu 14.04 ⁉️ ⁉️ ⁉️ ⁉️
Ubuntu 16.04 ⁉️ ⁉️ ⁉️ ⁉️
Ubuntu 18.04 ⁉️ ⁉️ ⁉️
Debian 8.x ⁉️ ⁉️ ⁉️ ⁉️
Debian 9.x ⁉️ ⁉️ ⁉️ ⁉️
Debian 10.x : white_check_mark:
RHEL 6.x ⁉️ ⁉️ ⁉️ ⁉️
RHEL 7.x ⁉️ ⁉️ ⁉️ ⁉️
  • ✅ - fully tested, should works fine
  • ⁉️ - maybe works, not tested
  • ❌ - don't have official support
  • ⛔ - MongoDB has reached EOL

For MongoDB 5.0

how fix error arbiter Reconfig attempted to install a config that would change the implicit default write concern. Use the setDefaultRWConcern command to set a cluster-wide write concern and try the reconfig again.

connect to primary and run command:

db.adminCommand({
  "setDefaultRWConcern" : 1,
  "defaultWriteConcern" : {
    "w" : 1
  }
})

Variables

# You can use this variable to control installation source of MongoDB
# 'mongodb' will be installed from Debian/Ubuntu repos
# 'mongodb-org' will be installed from MongoDB official repos
mongodb_package: mongodb-org

# You can control installed version via this param.
# Should be '3.2', '3.4', '3.6'. This role doesn't support MongoDB < 3.2.
# I will recommend you to use latest version of MongoDB.
mongodb_version: "3.6"

mongodb_pymongo_from_pip: true                   # Install latest PyMongo via PIP or package manager
mongodb_pymongo_pip_version: 3.6.1               # Choose PyMong version to install from pip. If not set use latest
mongodb_user_update_password: "on_create"        # MongoDB user password update default policy
mongodb_manage_service: true

mongodb_user: "{{ 'mongod' if ('RedHat' == ansible_os_family) else 'mongodb' }}"
mongodb_uid:
mongodb_gid:
mongodb_daemon_name: "{{ 'mongod' if ('mongodb-org' in mongodb_package) else 'mongodb' }}"
## net Options
mongodb_net_bindip: 127.0.0.1                    # Comma separated list of ip addresses to listen on
mongodb_net_http_enabled: false                  # Enable http interface
mongodb_net_ipv6: false                          # Enable IPv6 support (disabled by default)
mongodb_net_maxconns: 65536                      # Max number of simultaneous connections
mongodb_net_port: 27017                          # Specify port number

## processManagement Options
mongodb_processmanagement_fork: false            # Fork server process

## security Options
# Disable or enable security. Possible values: 'disabled', 'enabled'
mongodb_security_authorization: "disabled"
mongodb_security_keyfile: /etc/mongodb-keyfile   # Specify path to keyfile with password for inter-process authentication

## storage Options
mongodb_storage_dbpath: /data/db                 # Directory for datafiles
mongodb_storage_dirperdb: false                  # Use one directory per DB

# The storage engine for the mongod database. Available values:
# 'mmapv1', 'wiredTiger'
mongodb_storage_engine: "{{ 'mmapv1' if mongodb_version[0:3] == '3.0' else 'wiredTiger' }}"
# mmapv1 specific options
mongodb_storage_quota_enforced: false            # Limits each database to a certain number of files
mongodb_storage_quota_maxfiles: 8                # Number of quota files per DB
mongodb_storage_smallfiles: false                # Very useful for non-data nodes

mongodb_storage_journal_enabled: true            # Enable journaling
mongodb_storage_prealloc: true                   # Disable data file preallocation

## systemLog Options
## The destination to which MongoDB sends all log output. Specify either 'file' or 'syslog'.
## If you specify 'file', you must also specify mongodb_systemlog_path.
mongodb_systemlog_destination: "file"
mongodb_systemlog_logappend: true                                        # Append to logpath instead of over-writing
mongodb_systemlog_path: /var/log/mongodb/{{ mongodb_daemon_name }}.log   # Log file to send write to instead of stdout

## replication Options
mongodb_replication_replset:                      # Enable replication <setname>[/<optionalseedhostlist>]
mongodb_replication_replindexprefetch: "all"      # specify index prefetching behavior (if secondary) [none|_id_only|all]
mongodb_replication_oplogsize: 1024               # specifies a maximum size in megabytes for the replication operation log

## setParameter options
# Configure setParameter option.
# Example :
mongodb_set_parameters: { "enableLocalhostAuthBypass": "true", "authenticationMechanisms": "SCRAM-SHA-1,MONGODB-CR" }

# MMS Agent
mongodb_mms_agent_pkg: https://mms.mongodb.com/download/agent/automation/mongodb-mms-automation-agent-manager_1.4.2.783-1_amd64.deb
mongodb_mms_group_id: ""
mongodb_mms_api_key: ""
mongodb_mms_base_url: https://mms.mongodb.com
# you must set inventory_hostname for install mms agent only one node. Instead this one mms agent will install to every node.
mms_agent_inventory_name: ""

# Log rotation
mongodb_logrotate: true                             # Rotate mongodb logs.
mongodb_logrotate_options:
  - compress
  - copytruncate
  - daily
  - dateext
  - rotate 7
  - size 10M

# password for inter-process authentication
# please regenerate this file on production environment with command 'openssl rand -base64 741'
mongodb_keyfile_content: |
  8pYcxvCqoe89kcp33KuTtKVf5MoHGEFjTnudrq5BosvWRoIxLowmdjrmUpVfAivh
  CHjqM6w0zVBytAxH1lW+7teMYe6eDn2S/O/1YlRRiW57bWU3zjliW3VdguJar5i9
  Z+1a8lI+0S9pWynbv9+Ao0aXFjSJYVxAm/w7DJbVRGcPhsPmExiSBDw8szfQ8PAU
  2hwRl7nqPZZMMR+uQThg/zV9rOzHJmkqZtsO4UJSilG9euLCYrzW2hdoPuCrEDhu
  Vsi5+nwAgYR9dP2oWkmGN1dwRe0ixSIM2UzFgpaXZaMOG6VztmFrlVXh8oFDRGM0
  cGrFHcnGF7oUGfWnI2Cekngk64dHA2qD7WxXPbQ/svn9EfTY5aPw5lXzKA87Ds8p
  KHVFUYvmA6wVsxb/riGLwc+XZlb6M9gqHn1XSpsnYRjF6UzfRcRR2WyCxLZELaqu
  iKxLKB5FYqMBH7Sqg3qBCtE53vZ7T1nefq5RFzmykviYP63Uhu/A2EQatrMnaFPl
  TTG5CaPjob45CBSyMrheYRWKqxdWN93BTgiTW7p0U6RB0/OCUbsVX6IG3I9N8Uqt
  l8Kc+7aOmtUqFkwo8w30prIOjStMrokxNsuK9KTUiPu2cj7gwYQ574vV3hQvQPAr
  hhb9ohKr0zoPQt31iTj0FDkJzPepeuzqeq8F51HB56RZKpXdRTfY8G6OaOT68cV5
  vP1O6T/okFKrl41FQ3CyYN5eRHyRTK99zTytrjoP2EbtIZ18z+bg/angRHYNzbgk
  lc3jpiGzs1ZWHD0nxOmHCMhU4usEcFbV6FlOxzlwrsEhHkeiununlCsNHatiDgzp
  ZWLnP/mXKV992/Jhu0Z577DHlh+3JIYx0PceB9yzACJ8MNARHF7QpBkhtuGMGZpF
  T+c73exupZFxItXs1Bnhe3djgE3MKKyYvxNUIbcTJoe7nhVMrwO/7lBSpVLvC4p3
  wR700U0LDaGGQpslGtiE56SemgoP

# names and passwords for administrative users
mongodb_user_admin_name: siteUserAdmin
mongodb_user_admin_password: passw0rd

mongodb_root_admin_name: siteRootAdmin
mongodb_root_admin_password: passw0rd

Usage

THis is example of playbook for launche role

- name: Setup Mongo
  hosts: mongo
  become: yes
  pre_tasks:
    - name: Verify Ansible version
      assert:
        that:
          - "ansible_version.full is version_compare('2.8.0', '>=')"
          - "ansible_version.full is version_compare('2.9.0', '<')"
        msg: >
          Your Ansible version: {{ ansible_version.full }}.
          You must install Ansible version from 2.8.0 to 2.8.x
    - name: mongodb_replication_params
      debug:
        var: mongodb_replication_params
        verbosity: 2
  roles:
    - role: ansible-role-mongodb
      tags: [ 'mongodb-install' ]

  tasks:
    - lineinfile:
        path: /lib/systemd/system/mongod.service
        regexp: '^{{ item }}$'
        insertafter: '^PIDFile='
        line: "{{ item }}"
      with_items:
        - RestartSec=5
        - Restart=on-failure
      notify: daemon_reload
      tags: [ 'mongodb-install-without-additionals' ]
  handlers:
    - name: daemon_reload
      become: yes
      systemd: daemon_reload=yes
      tags: [ 'mongodb-install-without-additionals' ]
  vars_files:
    - environments/prod/env_vars/production-mongodb.yml

This is a simple example production-mongodb.yml var file for install mongodb 4

# For Install and configure mongodb
mongodb_version: '4.2'
mongodb_net_bindip: '0.0.0.0'
mongodb_net_port: 27017
mongodb_storage_dbpath: /var/lib/mongodb
mongodb_login_host: "{{ ansible_host }}"
mongodb_security_authorization: "enabled"
mongodb_user_admin_name: 'admin'
#mongodb_user_admin_password: in production_vault.yml
mongodb_root_admin_name: 'root'
#mongodb_root_admin_password: in production_vault.yml
mongodb_replication_replset: 'fp0'
# replicaset 
mongodb_replication_params:
  - { host_name: "{{ hostvars['master-test-mongodb-01']['inventory_hostname'] }}.example.com", host_port: "{{ mongodb_net_port }}", host_type: replica, priority: 2 }
  - { host_name: "{{ hostvars['slave-test-mongodb-02']['inventory_hostname'] }}.example.com", host_port: "{{ mongodb_net_port }}", host_type: replica, priority: 1 }
  - { host_name: "{{ hostvars['arbitr-test-mongodb-01']['inventory_hostname'] }}.example.com", host_port: "{{ mongodb_net_port }}", host_type: arbiter }

mongodb_net_ssl: enabled
# option requireSSL use only for mongodb 4. For mongo version 5 pls use requireTLS. 
# https://www.mongodb.com/docs/v4.2/tutorial/configure-ssl/#procedures-using-net-tls-settings
mongodb_net_ssl_mode: requireSSL
mongodb_net_ssl_pemfile: "/etc/mongodb.pem"
mongodb_net_ssl_pemfile_content: "{{ lookup('file', 'environments/prod/mongodb_certs/' + inventory_hostname + '.pem') }}"
mongodb_disable_transparent_hugepages: true
mongodb_systemlog_logrotate: "reopen"

mongodb_access_groups:
  - mongo
  - mongo-application

mongodb_authdb_name: admin
#mms settings
mms_agent_inventory_name: 'arbitr-test-mongodb-01'
mongodb_mms_group_id: 6436a619ad7cb6df41
mongodb_mms_base_url: https://mms.example.com
mongodb_mms_api_key: 6436a633ad7cb628c461e0aa9e95a688c6ca84499cbab6
#mongodb_mms_agent_pkg: https://mms.example.com/download/agent/automation/mongodb-mms-automation-agent-manager_11.0.10.7021-1_amd64.ubuntu1604.deb

production_vault.yml

mongodb_user_admin_password: 'passwd1'
mongodb_root_admin_password: 'passwd2'

# Users for application
mongodb_users:
  - {
    name: application_user,
    password: pass,
    roles: readWrite,
    database: database
}

mongodb_keyfile_content: |
  a8mzugVDSOMRxkvQEhKRoZCTGF8clldd8WuWiQMCtrdAKmko5i
  ...

And inventory file for replica set:

[mongo_master]
192.158.56.2 ansible_host=master-test-mongodb-01 mongodb_master=True # it is't a really master of MongoDB replica set,
                                 # use this variable for replica set init only
								 # or when master is moved from initial master node

[mongo_replicas]
192.168.56.3 ansible_host=slave-test-mongodb-02
192.168.56.4 ansible_host=arbitr-test-mongodb-01

[mongo:children]
mongo_master
mongo_replicas

Tags

mongodb-install-without-additionals - you can use when need only install mongodb.

Licensed under the GPLv2 License. See the LICENSE.md file for details.

Feedback, bug-reports, requests, ...

Are welcome!

About

Ansible role to configure MongoDB

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages