Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo 3 Support #14

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ Examples
mongod_replset_name: rs0


MongoDB 3.X
-----------

To install modern versions of mongo on Debian based systems, use the following vars:

```yaml
# see http://docs.mongodb.org/manual/administration/install-on-linux/ for other repositories
mongod_repo_debian: "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse"

mongod_pkgs:
- mongodb-org
- python-selinux
- python-pymongo

# defaults to mmapv1, so be explicit if you want to use WT
mongod_storage_engine: wiredTiger

# the localhost exception has changed in 3.x, so either disable the key file or send a PR :)
mongod_use_key: false

# change this to a sane value
mongod_bind_ip: 0.0.0.0
```


Dependencies
------------
Expand Down
7 changes: 7 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ mongod_replication: false
mongod_repl_servers: []
mongod_repl_master: "localhost"
mongod_replset_name: rs0
mongod_repo_debian: "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen"
mongod_storage_engine: 'mmapv1'
mongod_bind_ip: "127.0.0.1"
mongod_use_key: true
mongod_user: false

# mongod_auth: false
67 changes: 54 additions & 13 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
when: ansible_os_family == "Debian"

- name: Install the repository for Ubuntu mongodb
apt_repository: repo="deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" state=present
apt_repository: repo="{{ mongod_repo_debian }}" state=present
when: ansible_os_family == "Debian"

- name: Install the libselinux module
yum: name=libselinux-python state=installed
when: ansible_os_family == "RedHat"
Expand All @@ -38,16 +38,6 @@
- name: Create the data directory for the mongod
file: path={{ mongod_datadir_prefix }} owner={{ mongo_user }} group={{ mongo_group }} state=directory

- name: Install the mongodb package
yum: name={{ item }} state=installed
with_items: mongod_pkgs
when: ansible_os_family == "RedHat"

- name: Install the mongodb package
apt: name={{ item }} state=installed update_cache=yes
with_items: mongod_pkgs
when: ansible_os_family == "Debian"

- name: create data directory for mongodb
file: path={{ mongod_datadir_prefix }}/mongo-{{ mongod_port }} state=directory owner={{ mongo_user }} group={{ mongo_group }}

Expand All @@ -64,9 +54,21 @@

- name: Generate the keyfile for authentication
set_fact: mongod_secret_key="{{ lookup('password', 'secret length=256 chars=ascii_letters,digits') }}"
when: mongod_use_key

- name: Copy the keyfile for authentication
copy: src=secret dest={{ mongod_datadir_prefix }}/secret owner={{ mongo_user }} group={{ mongo_group }} mode=0400
when: mongod_use_key

- name: Install the mongodb package
yum: name={{ item }} state=installed
with_items: mongod_pkgs | default(mongod_pkgs_default)
when: ansible_os_family == "RedHat"

- name: Install the mongodb package
apt: name={{ item }} state=installed update_cache=yes
with_items: mongod_pkgs | default(mongod_pkgs_default)
when: ansible_os_family == "Debian"

- name: Start the mongodb service for redhat variants
command: creates=/var/lock/subsys/mongod-{{ mongod_port }} /etc/init.d/mongod-{{ mongod_port }} start
Expand All @@ -85,5 +87,44 @@
when: mongod_replication

- name: Initialize the replication set
shell: /usr/bin/mongo --port "{{ mongod_port }}" /tmp/repset_init.js
shell: /usr/bin/mongo --port "{{ mongod_port }}" admin /tmp/repset_init.js
when: mongod_replication and (mongod_repl_master == inventory_hostname or mongod_repl_master == ansible_hostname)

- name: Generate the password for authentication
set_fact: mongod_password="{{ lookup('password', 'mongod_password length=32 chars=ascii_letters,digits') }}"
when: mongod_user != false

- name: Construct mongo flags for user login
set_fact: mongod_user_login_flags="--username {{ mongod_user }} --password {{ mongod_password }}"
when: mongod_user != false

- name: Clear mongo flags for user login
set_fact: mongod_user_login_flags=""
when: mongod_user == false

- name: Check if mongo user exists
shell: /usr/bin/mongo --port {{ mongod_port }} {{ mongod_user_login_flags }} --eval 'db.serverStatus()' admin
register: mongod_user_check
when: mongod_user != false and ((not mongod_replication) or (mongod_repl_master == inventory_hostname or mongod_repl_master == ansible_hostname))
ignore_errors: true
changed_when: false

- name: Create the file to create the initial user
template: src=create_user.j2 dest=/tmp/create_user.js
when: mongod_user != false and (mongod_user_check | failed) and ((not mongod_replication) or (mongod_repl_master == inventory_hostname or mongod_repl_master == ansible_hostname))

- name: Create the password file for future reference
shell: echo "{{ mongod_password }}" > /tmp/mongod_password
changed_when: false

- name: Create the initial user
shell: /usr/bin/mongo --port "{{ mongod_port }}" admin /tmp/create_user.js
when: mongod_user != false and (mongod_user_check | failed) and ((not mongod_replication) or (mongod_repl_master == inventory_hostname or mongod_repl_master == ansible_hostname))

- name: Create the file to add additional RS members
template: src=add_members.j2 dest=/tmp/add_members.js
when: mongod_user != false and (mongod_user_check | failed) and ((not mongod_replication) or (mongod_repl_master == inventory_hostname or mongod_repl_master == ansible_hostname))

- name: Add members to RS
shell: /usr/bin/mongo --port "{{ mongod_port }}" {{ mongod_user_login_flags }} admin /tmp/add_members.js
when: mongod_replication and (mongod_repl_master == inventory_hostname or mongod_repl_master == ansible_hostname)
5 changes: 5 additions & 0 deletions templates/add_members.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% for host in mongod_repl_servers %}
rs.add("{{ host }}:{{ mongod_port }}")
sleep(8000)
{% endfor %}
printjson(rs.status())
6 changes: 6 additions & 0 deletions templates/create_user.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
db.createUser({
user: "{{ mongod_user }}",
pwd: "{{ mongod_password }}",
roles: [ 'root' ]
});
sleep(8000);
9 changes: 9 additions & 0 deletions templates/mongod.conf.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# mongo.conf
smallfiles={{ mongod_smallfiles }}
storageEngine={{ mongod_storage_engine }}

#where to log
{% if ansible_os_family == "RedHat" %}
Expand All @@ -19,7 +20,10 @@ logappend=true
port = {{ mongod_port }}

dbpath={{ mongod_datadir_prefix }}/mongo-{{ mongod_port }}

{% if mongod_use_key %}
keyFile={{ mongod_datadir_prefix }}/secret
{% endif %}

# location of pidfile
pidfilepath = {{ mongod_datadir_prefix }}/mongod_{{ mongod_port }}.pid
Expand All @@ -33,3 +37,8 @@ pidfilepath = {{ mongod_datadir_prefix }}/mongod_{{ mongod_port }}.pid
replSet={{ mongod_replset_name | default(mongod_port) }}
{% endif %}

bind_ip={{ mongod_bind_ip }}

{% if mongod_auth is defined %}
auth={{ mongod_auth }}
{% endif %}
6 changes: 1 addition & 5 deletions templates/repset_init.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
rs.initiate()
sleep(13000)
{% for host in mongod_repl_servers %}
rs.add("{{ host }}:{{ mongod_port }}")
sleep(8000)
{% endfor %}
printjson(rs.status())

2 changes: 1 addition & 1 deletion vars/Debian.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

mongod_pkgs:
mongod_pkgs_default:
- python-selinux
- mongodb-10gen
- python-pymongo
Expand Down
2 changes: 1 addition & 1 deletion vars/RedHat.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
mongod_pkgs:
mongod_pkgs_default:
- mongo-10gen
- mongo-10gen-server
- bc
Expand Down