Skip to content

Instantly share code, notes, and snippets.

@viq
Last active September 14, 2018 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viq/f3d8a4906795cc0c05ea2a22db1898ee to your computer and use it in GitHub Desktop.
Save viq/f3d8a4906795cc0c05ea2a22db1898ee to your computer and use it in GitHub Desktop.
First attempt at automating installing mongodb and configuring it to require authentication
prepare mongodb:
pkg:
- installed
- pkgs:
- mongodb
- py-mongo
file:
- managed
- name: /etc/mongodb.conf
- source: salt://mongodb/mongodb.conf
- template: jinja
- user: root
- group: _mongodb
- mode: 0640
- require:
- pkg: prepare mongodb
service:
- running
- name: mongod
- watch:
- file: prepare mongodb
{% if salt['grains.get']('mongodb:configured', False) == False %}
cmd:
- script
- name: salt://mongodb/mongoadmin.py
- template: jinja
- env:
- PATH: '/bin:/usr/bin:/usr/local/bin'
- require:
- service: prepare mongodb
- require_in:
- mongodb_user: prepare mongodb
{% endif %}
mongodb_user:
- present
- name: {{salt.pillar.get('mongodb.user')}}
- passwd: {{salt.pillar.get('mongodb.password')}}
- host: {{salt.pillar.get('mongodb.host')}}
- database: admin
- roles:
- userAdminAnyDatabase
- require:
- service: prepare mongodb
grains:
- present
- name: mongodb:configured
- value: True
- require:
- mongodb_user: prepare mongodb
#!/usr/bin/env python2
import pymongo
pymongo.database.Database(pymongo.MongoClient(
host="{{salt.pillar.get('mongodb.host', '127.0.0.1')}}",
port={{salt.pillar.get('mongodb.port', 27017)}}
), 'admin').add_user(
"{{salt.pillar.get('mongodb.user')}}",
"{{salt.pillar.get('mongodb.password')}}",
roles=['userAdminAnyDatabase']
)
# $OpenBSD: mongodb.conf,v 1.2 2016/05/06 15:33:37 sthen Exp $
# Sample configuration. See
# https://docs.mongodb.org/manual/administration/configuration/
# for details.
processManagement:
fork: true
net:
# Only listen on the local network interface. Change this only if you
# need a public-facing instance and have turned on authorization.
bindIp: 127.0.0.1
storage:
dbPath: /var/mongodb/data
journal:
enabled: true
systemLog:
destination: file
path: /var/log/mongodb/mongodb.log
logAppend: true
{#% if salt['grains.get']('mongodb:configured', False) %#}
security:
authorization: enabled
setParameter:
enableLocalhostAuthBypass: true
{#% endif %#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment