├── .bumpversion.cfg ├── .travis.yml ├── CONTRIBUTORS ├── LICENSE ├── Makefile ├── README.md ├── defaults └── main.yml ├── files └── mongod.service ├── handlers └── main.yml ├── meta └── main.yml ├── runtests.sh ├── spec ├── README.md └── mongodb_spec.rb ├── tasks ├── backup.yml ├── configure.yml ├── docker.yml ├── install.deb.yml ├── install.xenial.yml ├── main.yml ├── mms-agent.yml └── mongodb.yml ├── templates ├── automation-agent.config.j2 ├── backup │ ├── aws.j2 │ ├── backup.j2 │ ├── backup.sh.j2 │ ├── cronjob.j2 │ ├── download.sh.j2 │ ├── list.sh.j2 │ └── restore.sh.j2 ├── logrotate.conf.j2 ├── mongod.conf.j2 ├── mongod.service.j2 └── transparent_hugepage.conf ├── test.yml └── vars ├── Debian.yml └── Ubuntu.yml /.bumpversion.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | commit = True 3 | current_version = 2.3.12 4 | tag = True 5 | tag_name = {new_version} 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: python 3 | python: "2.7" 4 | before_install: 5 | - sudo apt-get update -qq 6 | - sudo apt-get install -qq python-apt python-pycurl 7 | - sudo apt-get remove --purge -y mongodb-* 8 | install: 9 | - pip install ansible>=1.6.0 10 | script: 11 | - echo localhost > inventory 12 | 13 | # Check syntax 14 | - ansible-playbook --syntax-check -i inventory test.yml 15 | 16 | # First run 17 | - ansible-playbook -i inventory test.yml --connection=local --sudo 18 | 19 | # Second run Idempotence test 20 | - > 21 | ansible-playbook -i inventory test.yml --connection=local --sudo 22 | | grep -q 'changed=0.*failed=0' 23 | && (echo 'Idempotence test: pass' && exit 0) 24 | || (echo 'Idempotence test: fail' && exit 1) 25 | 26 | # Check for mongodb is running 27 | - ps -ef | grep [m]ongod || exit 1 28 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | Contributors: 2 | 3 | * Guillaume Dedrie (https://github.com/guillaumededrie) 4 | * Jason McVetta (https://github.com/jmcvetta) 5 | * Joao C Costa (https://github.com/joaocc) 6 | * Kirill Klenov (http://klen.github.io/) 7 | * Luke Rohde (https://github.com/thyming) 8 | * Mikołaj Koziarkiewicz (https://github.com/mikolak-net) 9 | * Sergey Protko (https://github.com/fesor) 10 | * Till Hofmann (https://github.com/morxa) 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Stouts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: release major minor patch 2 | 3 | VERSION?=minor 4 | release: 5 | @bumpversion $(VERSION) 6 | @git checkout master 7 | @git merge develop 8 | @git checkout develop 9 | @git push --all 10 | @git push --tags 11 | @git checkout develop 12 | 13 | major: 14 | make release VERSION=major 15 | 16 | minor: 17 | make release VERSION=minor 18 | 19 | patch: 20 | make release VERSION=patch 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Stouts.mongo 2 | ============ 3 | 4 | [![Build Status](http://img.shields.io/travis/Stouts/Stouts.mongodb.svg?style=flat-square)](https://travis-ci.org/Stouts/Stouts.mongodb) 5 | [![Galaxy](http://img.shields.io/badge/galaxy-Stouts.mongodb-blue.svg?style=flat-square)](https://galaxy.ansible.com/list#/roles/982) 6 | 7 | Ansible role which manage [MongoDB](http://www.mongodb.org/) 8 | 9 | * Install and configure the MongoDB; 10 | * Provide hanlers for restart and reload; 11 | * Setup MMS authomation agent; 12 | 13 | #### Variables 14 | 15 | ```yaml 16 | 17 | mongodb_enabled: yes 18 | mongodb_install: yes 19 | 20 | mongodb_additional_packages: 21 | - python-selinux 22 | - python-pymongo 23 | 24 | mongodb_user: mongodb 25 | 26 | mongodb_version: "3.2.4" # Fully specified target installation version 27 | 28 | # Service setup options 29 | mongodb_version_config: # Custom configuration for major/minor version variants 30 | v2.4: 31 | daemon: mongodb 32 | config_file: mongodb.conf 33 | v2.6: 34 | daemon: mongod 35 | config_file: mongod.conf 36 | v3.0: 37 | daemon: mongod 38 | config_file: mongod.conf 39 | v3.2: 40 | daemon: mongod 41 | config_file: mongod.conf 42 | 43 | # Config options 44 | mongodb_conf_auth: no # Run with security 45 | mongodb_conf_bind_ip: 127.0.0.1 # Comma separated list of ip addresses to listen on 46 | mongodb_conf_cpu: yes # Periodically show cpu and iowait utilization 47 | mongodb_conf_dbpath: /data/db # Directory for datafiles 48 | mongodb_conf_fork: no # Fork server process 49 | mongodb_conf_httpinterface: no # Enable http interface 50 | mongodb_conf_ipv6: no # Enable IPv6 support (disabled by default) 51 | mongodb_conf_journal: no # Enable journaling 52 | mongodb_conf_logappend: yes # Append to logpath instead of over-writing 53 | mongodb_conf_logpath: /var/log/mongodb/{{ mongodb_daemon_name }}.log # Log file to send write to instead of stdout 54 | mongodb_conf_maxConns: 20000 # Max number of simultaneous connections 55 | mongodb_conf_noprealloc: no # Disable data file preallocation 56 | mongodb_conf_noscripting: no # Disable scripting engine 57 | mongodb_conf_notablescan: no # Do not allow table scans 58 | mongodb_conf_port: 27017 # Specify port number 59 | mongodb_conf_quota: no # Limits each database to a certain number of files 60 | mongodb_conf_quotaFiles: 8 # Number of quota files 61 | mongodb_conf_syslog: no # Log to system's syslog facility instead of file (ignored if logpath set) 62 | mongodb_conf_smallfiles: no # Sets MongoDB to use a smaller default file size 63 | 64 | # Replica set options: 65 | mongodb_conf_replSet: # Enable replication [/] 66 | mongodb_conf_replIndexPrefetch: all # specify index prefetching behavior (if secondary) [none|_id_only|all] 67 | 68 | mongodb_shell: {} # Define mongo shell commands to run 69 | # Syntax: mongodb_shell: 70 | # dbname: 71 | # - db.setProfilingLevel(1, 50) 72 | 73 | 74 | # MMS Agent 75 | mongodb_mms_agent_pkg: https://mms.mongodb.com/download/agent/automation/mongodb-mms-automation-agent-manager_latest_amd64.deb 76 | mongodb_mms_group_id: "" 77 | mongodb_mms_api_key: "" 78 | mongodb_mms_base_url: https://mms.mongodb.com 79 | 80 | # Log rotation 81 | mongodb_logrotate: yes # Rotate mongodb logs. 82 | mongodb_logrotate_options: 83 | - compress 84 | - copytruncate 85 | - daily 86 | - dateext 87 | - rotate 7 88 | - size 10M 89 | ``` 90 | 91 | #### Usage 92 | 93 | Add `Stouts.mongodb` to your roles and set vars in your playbook file. 94 | 95 | Example: 96 | 97 | ```yaml 98 | 99 | - hosts: all 100 | 101 | roles: 102 | - Stouts.mongodb 103 | 104 | vars: 105 | mongodb_conf_port: 27400 106 | ``` 107 | 108 | #### License 109 | 110 | Licensed under the MIT License. See the LICENSE file for details. 111 | 112 | #### Feedback, bug-reports, requests, ... 113 | 114 | Are [welcome](https://github.com/Stouts/Stouts.mongodb/issues)! 115 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | mongodb_enabled: yes 4 | mongodb_install: yes 5 | 6 | # Install with docker 7 | mongodb_docker: no # Run mongo db as docker container 8 | mongodb_docker_net: bridge 9 | mongodb_docker_hostname: "mongodb.{{inventory_hostname}}" 10 | mongodb_docker_image: "mongo:{{mongodb_version}}" 11 | mongodb_docker_instances: 12 | - name: mongodb 13 | volumes: ["{{mongodb_conf_dbpath}}:/data/db"] 14 | mongodb_docker_ports: ["{{mongodb_conf_bind_ip}}:{{mongodb_conf_port}}:27017"] # Set empty to disable ports forwarding 15 | mongodb_docker_command: " --smallfiles --storageEngine=wiredTiger" 16 | mongodb_docker_volumes: 17 | - "{{mongodb_conf_dbpath}}:/data/db" 18 | 19 | mongodb_additional_packages: 20 | - python-selinux 21 | - python-pymongo 22 | 23 | mongodb_user: mongodb 24 | mongodb_logdir: /var/log/mongodb 25 | 26 | mongodb_version: "3.2.4" # Fully specified target installation version 27 | mongodb_transparent_hugepage_disable: false 28 | 29 | # Service setup options 30 | mongodb_config: # Custom configuration for major/minor version variants 31 | daemon: mongod 32 | config_file: mongod.conf 33 | 34 | # Config options 35 | mongodb_conf_auth: no # Run with security 36 | mongodb_conf_bind_ip: 127.0.0.1 # Comma separated list of ip addresses to listen on 37 | mongodb_conf_cpu: yes # Periodically show cpu and iowait utilization 38 | mongodb_conf_dbpath: /data/db # Directory for datafiles 39 | mongodb_conf_fork: no # Fork server process 40 | mongodb_conf_httpinterface: no # Enable http interface 41 | mongodb_conf_ipv6: no # Enable IPv6 support (disabled by default) 42 | mongodb_conf_journal: no # Enable journaling 43 | mongodb_conf_logappend: yes # Append to logpath instead of over-writing 44 | mongodb_conf_logpath: "{{mongodb_logdir}}/mongo.log" # Log file to send write to instead of stdout 45 | mongodb_conf_maxConns: 20000 # Max number of simultaneous connections 46 | mongodb_conf_noprealloc: no # Disable data file preallocation 47 | mongodb_conf_noscripting: no # Disable scripting engine 48 | mongodb_conf_notablescan: no # Do not allow table scans 49 | mongodb_conf_port: 27017 # Specify port number 50 | mongodb_conf_quota: no # Limits each database to a certain number of files 51 | mongodb_conf_quotaFiles: 8 # Number of quota files 52 | mongodb_conf_syslog: no # Log to system's syslog facility instead of file (ignored if logpath set) 53 | mongodb_conf_smallfiles: no # Sets MongoDB to use a smaller default file size 54 | 55 | # Replica set options: 56 | mongodb_conf_replSet: # Enable replication [/] 57 | mongodb_conf_replIndexPrefetch: all # specify index prefetching behavior (if secondary) [none|_id_only|all] 58 | 59 | mongodb_shell: {} # Define mongo shell commands to run 60 | # Syntax: mongodb_shell: 61 | # dbname: 62 | # - db.setProfilingLevel(1, 50) 63 | 64 | 65 | # MMS Agent 66 | mongodb_mms_agent_pkg: https://mms.mongodb.com/download/agent/automation/mongodb-mms-automation-agent-manager_latest_amd64.deb 67 | mongodb_mms_group_id: "" 68 | mongodb_mms_api_key: "" 69 | mongodb_mms_base_url: https://mms.mongodb.com 70 | 71 | # Log rotation 72 | mongodb_logrotate: yes # Rotate mongodb logs. 73 | mongodb_logrotate_options: 74 | - compress 75 | - copytruncate 76 | - daily 77 | - dateext 78 | - rotate 7 79 | - size 10M 80 | 81 | # Backups 82 | mongodb_backup_aws_key: "" 83 | mongodb_backup_aws_cmd: /usr/local/bin/aws 84 | mongodb_backup_aws_region: "us-west-2" 85 | mongodb_backup_aws_secret: "" 86 | mongodb_backup_db: "" 87 | mongodb_backup_enabled: false 88 | mongodb_backup_home: /data/backup 89 | mongodb_backup_host: "{{mongodb_conf_bind_ip}}" 90 | mongodb_backup_pass: "" 91 | mongodb_backup_port: "{{mongodb_conf_port}}" 92 | mongodb_backup_s3path: "" 93 | mongodb_backup_user: "" 94 | mongodb_backup_opts: "" 95 | mongodb_backup_cron: false 96 | mongodb_backup_cron_schedule: "0 1 * * *" 97 | -------------------------------------------------------------------------------- /files/mongod.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=High-performance, schema-free document-oriented database 3 | After=network.target 4 | 5 | [Service] 6 | User=mongodb 7 | ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf 8 | 9 | [Install] 10 | WantedBy=multi-user.target 11 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: mongodb reload 4 | service: name={{ mongodb_daemon_name }} state=reloaded 5 | 6 | - name: mongodb restart 7 | service: name={{ mongodb_daemon_name }} state=restarted 8 | 9 | - name: mongodb-mms-automation-agent restart 10 | service: name=mongodb-mms-automation-agent state=restarted 11 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | dependencies: [] 4 | 5 | galaxy_info: 6 | author: klen 7 | company: Stouts 8 | description: Manage MongoDB (MMS) 9 | license: MIT 10 | platforms: 11 | - name: Ubuntu 12 | versions: 13 | - all 14 | - name: Debian 15 | versions: 16 | - wheezy 17 | categories: 18 | - database:nosql 19 | -------------------------------------------------------------------------------- /runtests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | IMAGES=( 4 | "horneds/stouts-ubuntu14.04" 5 | "horneds/stouts-debian8" 6 | ) 7 | 8 | TESTS=( 9 | "ansible-playbook -c local --syntax-check test.yml" 10 | "ansible-playbook -c local test.yml" 11 | "which mongo" 12 | ) 13 | 14 | APPDIR=/var/tests 15 | CURDIR=`pwd` 16 | ARGS=("$@") 17 | 18 | docker info || exit 1 19 | 20 | assert () { 21 | echo "ASSERT: $1" 22 | execute "$@" || ( echo ${2-'Test is failed'} && exit 1 ) 23 | echo "SUCCESS" 24 | } 25 | 26 | execute () { 27 | docker exec -it $RUNNER $1 28 | return $? 29 | } 30 | 31 | suite () { 32 | 33 | echo "=================" 34 | echo "RUN IMAGE: $1" 35 | echo "=================" 36 | 37 | for TEST in "${TESTS[@]}"; do 38 | assert "$TEST" || ( echo "FAILED" && exit 1 ) 39 | done 40 | 41 | echo 42 | 43 | } 44 | 45 | 46 | for IMAGE in "${IMAGES[@]}" 47 | do 48 | 49 | RUNNER=`docker run -v $CURDIR:$APPDIR -w $APPDIR -dit $IMAGE bash` 50 | 51 | suite $IMAGE || { 52 | echo "Tests are failed $IMAGE" 53 | docker stop $RUNNER 54 | exit 1 55 | } 56 | 57 | if [[ "$1" = "shell" ]]; then 58 | echo "Run shell" 59 | execute /bin/bash 60 | fi 61 | 62 | docker stop $RUNNER 63 | 64 | done 65 | -------------------------------------------------------------------------------- /spec/README.md: -------------------------------------------------------------------------------- 1 | Serverspec Tests 2 | =============== 3 | 4 | This folder contains a [Serverspec](http://serverspec.org) test file, 5 | `mongodb_spec.rb`. Use [AnsibleSpec](http://github.com/volanja/ansible_spec) 6 | to run Serverspec tests on Ansible roles. 7 | -------------------------------------------------------------------------------- /spec/mongodb_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe package('mongodb-org') do 4 | it { should be_installed } 5 | end 6 | 7 | describe service('mongod') do 8 | it { should be_enabled } 9 | it { should be_running } 10 | end 11 | 12 | describe port(27017) do 13 | it { should be_listening } 14 | end 15 | 16 | -------------------------------------------------------------------------------- /tasks/backup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Create a backup directory 4 | file: state=directory path={{ mongodb_backup_home }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755 5 | 6 | - name: Write AWS credentials 7 | template: src=backup/aws.j2 dest={{mongodb_backup_home}}/aws owner={{mongodb_user}} group={{mongodb_user}} mode=0644 8 | 9 | - name: Create a list script 10 | template: src=backup/list.sh.j2 dest={{mongodb_backup_home}}/list.sh owner={{mongodb_user}} group={{mongodb_user}} mode=0755 11 | 12 | - name: Create a backup script 13 | template: src=backup/backup.sh.j2 dest={{mongodb_backup_home}}/backup.sh owner={{mongodb_user}} group={{mongodb_user}} mode=0755 14 | 15 | - name: Create a restore script 16 | template: src=backup/restore.sh.j2 dest={{mongodb_backup_home}}/restore.sh owner={{mongodb_user}} group={{mongodb_user}} mode=0755 17 | 18 | - name: Create a download script 19 | template: src=backup/download.sh.j2 dest={{mongodb_backup_home}}/download.sh owner={{mongodb_user}} group={{mongodb_user}} mode=0755 20 | 21 | - name: Setup a CRON job 22 | template: src=backup/cronjob.j2 dest=/etc/cron.d/mongo-backup owner=root group=root mode=0644 23 | when: mongodb_backup_cron 24 | -------------------------------------------------------------------------------- /tasks/configure.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Configure mongodb 4 | template: src=mongod.conf.j2 dest=/etc/{{mongodb_config['config_file']}} owner=root group=root mode=0644 5 | notify: mongodb restart 6 | 7 | - name: Configure log rotation 8 | template: src=logrotate.conf.j2 dest=/etc/logrotate.d/mongodb.conf 9 | when: mongodb_logrotate 10 | 11 | - name: Create mongodb user 12 | user: name={{mongodb_user}} group={{mongodb_user}} 13 | 14 | - name: Configure database directory 15 | file: state=directory path={{ mongodb_conf_dbpath }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755 16 | 17 | - name: Configure log directory 18 | file: state=directory path={{ mongodb_conf_logpath | dirname }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755 19 | 20 | - name: Disable transparent hugepages 21 | template: src=transparent_hugepage.conf dest=/etc/init/mongodb_settings.conf owner=root group=root mode=0644 22 | notify: ["mongodb restart"] 23 | when: mongodb_transparent_hugepage_disable 24 | 25 | - name: Ensure mongodb is started 26 | service: name={{ mongodb_daemon_name }} state=started enabled=yes 27 | 28 | - name: Flush handlers to restart mongodb if necessary 29 | meta: flush_handlers 30 | 31 | - name: Run mongoshell commands 32 | command: mongo {{ item.key }} --eval "{{ item.value|join(';') }}" 33 | with_dict: "{{mongodb_shell}}" 34 | -------------------------------------------------------------------------------- /tasks/docker.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | # TODO: Support configuration 4 | 5 | - name: mongodb-docker | Ensure that redis directories is exists 6 | file: state=directory path={{item}} recurse=True 7 | with_items: 8 | - "{{mongodb_conf_dbpath}}" 9 | 10 | - set_fact: 11 | mongodb_docker_hostname: "" 12 | mongodb_docker_ports: [] 13 | when: mongodb_docker_net == 'host' 14 | 15 | - name: mongodb-docker | Start mongo 16 | docker_container: 17 | hostname: "{{item.hostname|default(mongodb_docker_hostname)}}" 18 | image: "{{mongodb_docker_image}}" 19 | command: "{{mongodb_docker_command|default(omit)}}" 20 | name: "{{item.name}}" 21 | memory_limit: "{{item.memory_limit|default(omit)}}" 22 | net: "{{mongodb_docker_net}}" 23 | ports: "{{mongodb_docker_ports}}" 24 | links: "{{item.links|default([])}}" 25 | pull: always 26 | restart_policy: always 27 | state: reloaded 28 | volumes: "{{item.volumes|default(mongodb_docker_volumes)}}" 29 | log_opt: 30 | max-size: 50m 31 | max-file: 10 32 | with_items: "{{mongodb_docker_instances}}" 33 | 34 | - name: mongodb-docker | Run mongoshell commands 35 | shell: docker exec {{mongodb_docker_instances[0].name}} mongo {{item.key}} --eval "{{ item.value|join(';') }}" 36 | with_dict: "{{mongodb_shell}}" 37 | -------------------------------------------------------------------------------- /tasks/install.deb.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - include_vars: "{{ansible_distribution}}.yml" 4 | 5 | - name: Obtain repository config 6 | set_fact: 7 | mongodb_repository_data: "{{mongodb_repository_config[mongodb_major_minor_version]}}" 8 | 9 | - name: Add APT key 10 | apt_key: keyserver=keyserver.ubuntu.com id="{{mongodb_repository_data.key}}" 11 | 12 | - name: Add APT repository 13 | apt_repository: repo="{{mongodb_repository_data.repository}}" update_cache=yes 14 | 15 | - name: Install MongoDB package 16 | apt: name="{{mongodb_repository_data.package+'='+mongodb_version}}" state=present force=yes 17 | 18 | - name: Install additional packages 19 | apt: name={{item}} 20 | with_items: "{{mongodb_additional_packages}}" 21 | -------------------------------------------------------------------------------- /tasks/install.xenial.yml: -------------------------------------------------------------------------------- 1 | - name: install systemd service 2 | copy: 3 | src=mongod.service 4 | dest=/etc/systemd/system/mongod.service 5 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - include: mongodb.yml 4 | when: mongodb_enabled 5 | tags: [mongodb] 6 | 7 | - include: backup.yml 8 | when: mongodb_enabled 9 | tags: [mongodb-backup] 10 | -------------------------------------------------------------------------------- /tasks/mms-agent.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Install MMS agent pt. 1 4 | get_url: url={{mongodb_mms_agent_pkg}} dest={{mongodb_conf_dbpath}}/mms-agent.deb 5 | register: mongodb_mms_agent_loaded 6 | 7 | - name: Install MMS agent pt. 2 8 | apt: deb={{mongodb_conf_dbpath}}/mms-agent.deb 9 | when: mongodb_mms_agent_loaded.changed 10 | 11 | - name: Configure the MMS agent pt. 1 12 | file: state=directory path=/etc/mongodb-mms owner={{mongodb_user}} group={{mongodb_user}} mode=0755 13 | 14 | - name: Configure the MMS agent pt. 2 15 | template: src=automation-agent.config.j2 dest=/etc/mongodb-mms/automation-agent.config 16 | notify: mongodb-mms-automation-agent restart 17 | 18 | - name: Ensure that the MMS agent is started 19 | service: name=mongodb-mms-automation-agent state=started enabled=yes 20 | -------------------------------------------------------------------------------- /tasks/mongodb.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Set up configuration parameters 1/2 3 | set_fact: mongodb_major_minor_version=v{{mongodb_version[:3]}} # the 'v' is necessary due to Ansible's number autoconversion quirks 4 | 5 | - name: Set up configuration parameters 2/2 6 | set_fact: 7 | mongodb_daemon_name: "{{mongodb_config['daemon']}}" 8 | 9 | - include: install.deb.yml 10 | when: ansible_os_family == 'Debian' and mongodb_install 11 | 12 | - include: install.xenial.yml 13 | when: ansible_os_family == 'Debian' and ansible_distribution_release == 'xenial' 14 | 15 | - include: configure.yml 16 | when: mongodb_install 17 | 18 | - include: docker.yml 19 | when: mongodb_docker 20 | 21 | - include: mms-agent.yml 22 | when: mongodb_mms_api_key != "" 23 | -------------------------------------------------------------------------------- /templates/automation-agent.config.j2: -------------------------------------------------------------------------------- 1 | mmsApiKey={{ mongodb_mms_api_key }} 2 | mmsBaseUrl={{ mongodb_mms_base_url }} 3 | mmsGroupId={{ mongodb_mms_group_id }} 4 | -------------------------------------------------------------------------------- /templates/backup/aws.j2: -------------------------------------------------------------------------------- 1 | # This file was generated by Ansible for {{ ansible_fqdn }} 2 | # Do NOT modify this file by hand! 3 | 4 | export AWS_ACCESS_KEY_ID={{mongodb_backup_aws_id}} 5 | export AWS_SECRET_ACCESS_KEY={{mongodb_backup_aws_secret}} 6 | export AWS_DEFAULT_REGION={{mongodb_backup_aws_region}} 7 | -------------------------------------------------------------------------------- /templates/backup/backup.j2: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # This file was generated by Ansible for {{ ansible_fqdn }} 4 | # Do NOT modify this file by hand! 5 | 6 | set -e # exit on command errors (so you MUST handle exit codes properly!) 7 | set -o pipefail # capture fail exit codes in piped commands 8 | #set -x # execution tracing debug messages 9 | 10 | # Get command info 11 | CMD_PWD=$(pwd) 12 | CMD="$0" 13 | CMD_DIR="$(cd "$(dirname "$CMD")" && pwd -P)" 14 | 15 | # Defaults and command line options 16 | [ "$VERBOSE" ] || VERBOSE= 17 | [ "$DEBUG" ] || DEBUG= 18 | 19 | # Basic helpers 20 | out() { echo "$(date +%Y%m%dT%H%M%SZ): $*"; } 21 | err() { out "$*" 1>&2; } 22 | vrb() { [ ! "$VERBOSE" ] || out "$@"; } 23 | dbg() { [ ! "$DEBUG" ] || err "$@"; } 24 | die() { err "EXIT: $1" && [ "$2" ] && [ "$2" -ge 0 ] && exit "$2" || exit 1; } 25 | usage() { [ "$0" = "bash" ] || sed '2,/^##/p;d' "$0"; echo "$*"; exit 1; } 26 | 27 | [ "$DEBUG" ] && set -x 28 | 29 | # Validate some things 30 | [ "$1" = "--help" -o "$1" = "-h" ] && usage "" 31 | 32 | ############################################################################### 33 | 34 | MONGODB_HOST={{ mongodb_backup_host }} 35 | MONGODB_PORT={{ mongodb_backup_port }} 36 | MONGODB_USER={{ mongodb_backup_user }} 37 | MONGODB_PASS={{ mongodb_backup_pass }} 38 | MONGODB_DB={{ mongodb_backup_db }} 39 | S3PATH={{ mongodb_backup_s3path }} 40 | 41 | TIMESTAMP=`date +"%Y%m%dT%H%M%S"` 42 | BACKUP_NAME=${TIMESTAMP}.dump.gz 43 | S3BACKUP=${S3PATH}/${BACKUP_NAME} 44 | S3LATEST=${S3PATH}/latest.dump.gz 45 | 46 | EXTRA_OPTS={{ mongodb_backup_opts }} 47 | 48 | [[ ( -z "${MONGODB_USER}" ) && ( -n "${MONGODB_PASS}" ) ]] && MONGODB_USER='admin' 49 | [[ ( -n "${MONGODB_USER}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --username ${MONGODB_USER}" 50 | [[ ( -n "${MONGODB_PASS}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --password ${MONGODB_PASS}" 51 | [[ ( -n "${MONGODB_DB}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --db ${MONGODB_DB}" 52 | 53 | cd {{ mongodb_backup_home }} 54 | 55 | source ./aws 56 | 57 | echo "=> Done" 58 | 59 | case "$1" in 60 | 61 | backup|b) 62 | echo 'backup' 63 | ;; 64 | 65 | restore|r) 66 | echo 'restore' 67 | ;; 68 | 69 | *) 70 | usage "Unknown option '$1'" 71 | ;; 72 | 73 | esac 74 | -------------------------------------------------------------------------------- /templates/backup/backup.sh.j2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This file was generated by Ansible for {{ ansible_fqdn }} 4 | # Do NOT modify this file by hand! 5 | 6 | MONGODB_HOST={{ mongodb_backup_host }} 7 | MONGODB_PORT={{ mongodb_backup_port }} 8 | MONGODB_USER={{ mongodb_backup_user }} 9 | MONGODB_PASS={{ mongodb_backup_pass }} 10 | MONGODB_DB={{ mongodb_backup_db }} 11 | S3PATH={{ mongodb_backup_s3path }} 12 | 13 | TIMESTAMP=`/bin/date +"%Y%m%dT%H%M%S"` 14 | BACKUP_NAME=${TIMESTAMP}.dump.gz 15 | S3BACKUP=${S3PATH}/${BACKUP_NAME} 16 | S3LATEST=${S3PATH}/latest.dump.gz 17 | 18 | EXTRA_OPTS={{ mongodb_backup_opts }} 19 | 20 | [[ ( -z "${MONGODB_USER}" ) && ( -n "${MONGODB_PASS}" ) ]] && MONGODB_USER='admin' 21 | [[ ( -n "${MONGODB_USER}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --username ${MONGODB_USER}" 22 | [[ ( -n "${MONGODB_PASS}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --password ${MONGODB_PASS}" 23 | [[ ( -n "${MONGODB_DB}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --db ${MONGODB_DB}" 24 | 25 | cd {{ mongodb_backup_home }} 26 | 27 | echo "=> Backup started: $EXTRA_OPTS" 28 | 29 | source ./aws 30 | 31 | if mongodump --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} --archive=${BACKUP_NAME} --gzip ${EXTRA_OPTS} && {{mongodb_backup_aws_cmd}} s3 cp ${BACKUP_NAME} ${S3BACKUP} && {{mongodb_backup_aws_cmd}} s3 cp ${S3BACKUP} ${S3LATEST} && rm ${BACKUP_NAME} ;then 32 | echo " > Backup succeeded" 33 | else 34 | echo " > Backup failed" 35 | fi 36 | 37 | echo "=> Done" 38 | -------------------------------------------------------------------------------- /templates/backup/cronjob.j2: -------------------------------------------------------------------------------- 1 | # This file was generated by Ansible for {{ ansible_fqdn }} 2 | # Do NOT modify this file by hand! 3 | 4 | # Run Mongo backups 5 | {{ mongodb_backup_cron_schedule }} {{ mongodb_user }} {{ mongodb_backup_home }}/backup.sh >> {{ mongodb_logdir }}/backup.log 2>&1 6 | 7 | 8 | -------------------------------------------------------------------------------- /templates/backup/download.sh.j2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This file was generated by Ansible for {{ ansible_fqdn }} 4 | # Do NOT modify this file by hand! 5 | 6 | source ./aws 7 | 8 | MONGODB_HOST={{ mongodb_backup_host }} 9 | MONGODB_PORT={{ mongodb_backup_port }} 10 | MONGODB_USER={{ mongodb_backup_user }} 11 | MONGODB_PASS={{ mongodb_backup_pass }} 12 | MONGODB_DB={{ mongodb_backup_db }} 13 | S3PATH={{ mongodb_backup_s3path }} 14 | 15 | if [[( -n "${1}" )]];then 16 | RESTORE_ME=${1}.dump.gz 17 | else 18 | RESTORE_ME=latest.dump.gz 19 | fi 20 | 21 | EXTRA_OPTS= 22 | 23 | [[ ( -z "${MONGODB_USER}" ) && ( -n "${MONGODB_PASS}" ) ]] && MONGODB_USER='admin' 24 | [[ ( -n "${MONGODB_USER}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --username ${MONGODB_USER}" 25 | [[ ( -n "${MONGODB_PASS}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --password ${MONGODB_PASS}" 26 | [[ ( -n "${MONGODB_DB}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --db ${MONGODB_DB}" 27 | 28 | cd {{ mongodb_backup_home }} 29 | 30 | S3RESTORE=${S3PATH}/${RESTORE_ME} 31 | echo "=> Restore database from ${RESTORE_ME}" 32 | if {{mongodb_backup_aws_cmd}} s3 cp ${S3RESTORE} ${RESTORE_ME}; then 33 | echo " Download succeeded" 34 | else 35 | echo " Download failed" 36 | fi 37 | 38 | echo "=> Done" 39 | 40 | -------------------------------------------------------------------------------- /templates/backup/list.sh.j2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This file was generated by Ansible for {{ ansible_fqdn }} 4 | # Do NOT modify this file by hand! 5 | 6 | source ./aws 7 | 8 | S3PATH={{ mongodb_backup_s3path }} 9 | 10 | cd {{ mongodb_backup_home }} 11 | 12 | {{ mongodb_backup_aws_cmd }} s3 ls ${S3PATH}/ 13 | -------------------------------------------------------------------------------- /templates/backup/restore.sh.j2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This file was generated by Ansible for {{ ansible_fqdn }} 4 | # Do NOT modify this file by hand! 5 | 6 | source ./aws 7 | 8 | MONGODB_HOST={{ mongodb_backup_host }} 9 | MONGODB_PORT={{ mongodb_backup_port }} 10 | MONGODB_USER={{ mongodb_backup_user }} 11 | MONGODB_PASS={{ mongodb_backup_pass }} 12 | MONGODB_DB={{ mongodb_backup_db }} 13 | S3PATH={{ mongodb_backup_s3path }} 14 | 15 | if [[( -n "${1}" )]];then 16 | RESTORE_ME=${1}.dump.gz 17 | else 18 | RESTORE_ME=latest.dump.gz 19 | fi 20 | 21 | EXTRA_OPTS= 22 | 23 | [[ ( -z "${MONGODB_USER}" ) && ( -n "${MONGODB_PASS}" ) ]] && MONGODB_USER='admin' 24 | [[ ( -n "${MONGODB_USER}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --username ${MONGODB_USER}" 25 | [[ ( -n "${MONGODB_PASS}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --password ${MONGODB_PASS}" 26 | [[ ( -n "${MONGODB_DB}" ) ]] && EXTRA_OPTS="$EXTRA_OPTS --db ${MONGODB_DB}" 27 | 28 | cd {{ mongodb_backup_home }} 29 | 30 | S3RESTORE=${S3PATH}/${RESTORE_ME} 31 | echo "=> Restore database from ${RESTORE_ME}" 32 | if {{mongodb_backup_aws_cmd}} s3 cp ${S3RESTORE} ${RESTORE_ME} && mongorestore --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${EXTRA_OPTS} --drop --archive=${RESTORE_ME} --gzip && rm ${RESTORE_ME}; then 33 | echo " Restore succeeded" 34 | else 35 | echo " Restore failed" 36 | fi 37 | 38 | echo "=> Done" 39 | -------------------------------------------------------------------------------- /templates/logrotate.conf.j2: -------------------------------------------------------------------------------- 1 | # This file was generated by Ansible for {{ ansible_fqdn }} 2 | # Do NOT modify this file by hand! 3 | 4 | {{ mongodb_conf_logpath }} { 5 | {% for option in mongodb_logrotate_options %} 6 | {{ option }} 7 | {% endfor %} 8 | } 9 | -------------------------------------------------------------------------------- /templates/mongod.conf.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | auth = {{ mongodb_conf_auth|to_nice_json|bool|lower }} 4 | bind_ip = {{ mongodb_conf_bind_ip }} 5 | cpu = {{ mongodb_conf_cpu|to_nice_json|bool|lower }} 6 | dbpath = {{ mongodb_conf_dbpath }} 7 | fork = {{ mongodb_conf_fork|to_nice_json|bool|lower }} 8 | httpinterface = {{ mongodb_conf_httpinterface|to_nice_json|bool|lower }} 9 | ipv6 = {{ mongodb_conf_ipv6|to_nice_json|bool|lower }} 10 | journal = {{ mongodb_conf_journal|to_nice_json|bool|lower }} 11 | logappend = {{ mongodb_conf_logappend|to_nice_json|bool|lower }} 12 | logpath = {{ mongodb_conf_logpath }} 13 | maxConns = {{ mongodb_conf_maxConns|int }} 14 | noprealloc = {{ mongodb_conf_noprealloc|to_nice_json|bool|lower }} 15 | noscripting = {{ mongodb_conf_noscripting|to_nice_json|bool|lower }} 16 | notablescan = {{ mongodb_conf_notablescan|to_nice_json|bool|lower }} 17 | port = {{ mongodb_conf_port|int }} 18 | quota = {{ mongodb_conf_quota|to_nice_json|bool|lower }} 19 | {% if mongodb_conf_quota %} 20 | quotaFiles = {{ mongodb_conf_quotaFiles|int }} 21 | {% endif %} 22 | {% if mongodb_conf_logpath == '' %} 23 | syslog = {{ mongodb_conf_syslog|to_nice_json|bool|lower }} 24 | {% endif %} 25 | smallfiles = {{ mongodb_conf_smallfiles|to_nice_json|bool|lower }} 26 | {% if mongodb_conf_replSet %} 27 | # Replica set options: 28 | replSet = {{ mongodb_conf_replSet }} 29 | replIndexPrefetch = {{ mongodb_conf_replIndexPrefetch }} 30 | {% endif %} 31 | -------------------------------------------------------------------------------- /templates/mongod.service.j2: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=High-performance, schema-free document-oriented database 3 | After=network.target 4 | 5 | [Service] 6 | User=mongodb 7 | ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf 8 | 9 | [Install] 10 | WantedBy=multi-user.target 11 | 12 | -------------------------------------------------------------------------------- /templates/transparent_hugepage.conf: -------------------------------------------------------------------------------- 1 | # Ubuntu upstart file at /etc/init/mongod_vm_settings.conf 2 | # 3 | # This file was generated by Ansible for {{ ansible_fqdn }} 4 | # Do NOT modify this file by hand! 5 | 6 | start on (starting mongod) 7 | script 8 | echo "never" > /sys/kernel/mm/transparent_hugepage/enabled 9 | echo "never" > /sys/kernel/mm/transparent_hugepage/defrag 10 | end script 11 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | - hosts: all 2 | tasks: 3 | - include: tasks/main.yml 4 | handlers: 5 | - include: handlers/main.yml 6 | vars_files: 7 | - defaults/main.yml 8 | -------------------------------------------------------------------------------- /vars/Debian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | mongodb_repository_config: 3 | v2.6: 4 | repository: deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen 5 | package: mongodb-org 6 | key: EA312927 7 | v3.0: 8 | repository: deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main 9 | package: mongodb-org 10 | key: EA312927 11 | v3.2: 12 | repository: deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main 13 | package: mongodb-org 14 | key: EA312927 15 | v3.4: 16 | repository: deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.4 main 17 | package: mongodb-org 18 | key: A15703C6 19 | -------------------------------------------------------------------------------- /vars/Ubuntu.yml: -------------------------------------------------------------------------------- 1 | --- 2 | mongodb_repository_config: 3 | v2.6: 4 | repository: deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen 5 | package: mongodb-org 6 | key: EA312927 7 | v3.0: 8 | repository: deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse 9 | package: mongodb-org 10 | key: EA312927 11 | v3.2: 12 | repository: deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse 13 | package: mongodb-org 14 | key: EA312927 15 | v3.4: 16 | repository: deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse 17 | package: mongodb-org 18 | key: A15703C6 19 | --------------------------------------------------------------------------------