salt.modules.mongodb

Module to provide MongoDB functionality to Salt

configuration:

This module uses PyMongo, and accepts configuration details as parameters as well as configuration settings:

mongodb.host: 'localhost'
mongodb.port: 27017
mongodb.user: ''
mongodb.password: ''

This data can also be passed into pillar. Options passed into opts will overwrite options passed into pillar.

salt.modules.mongodb.collection_create(collection, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

New in version 3006.0.

Create a collection in the specified database.

collection

The name of the collection to create.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.collection_create mycollection <user> <password> <host> <port> <database>
salt.modules.mongodb.collection_drop(collection, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

New in version 3006.0.

Drop a collection in the specified database.

collection

The name of the collection to drop.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.collection_drop mycollection <user> <password> <host> <port> <database>
salt.modules.mongodb.collections_list(user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

New in version 3006.0.

List the collections available in the specified database.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.collections_list mycollection <user> <password> <host> <port> <database>
salt.modules.mongodb.db_exists(name, user=None, password=None, host=None, port=None, authdb=None, ssl=False, verify_ssl=None)

Checks if a database exists in MongoDB.

name

The name of the database to check for.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.db_exists <name> <user> <password> <host> <port>
salt.modules.mongodb.db_list(user=None, password=None, host=None, port=None, authdb=None, ssl=False, verify_ssl=None)

List all MongoDB databases.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.db_list <user> <password> <host> <port>
salt.modules.mongodb.db_remove(name, user=None, password=None, host=None, port=None, authdb=None, ssl=False, verify_ssl=None)

Remove a MongoDB database.

name

The name of the database to remove.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.db_remove <name> <user> <password> <host> <port>
salt.modules.mongodb.find(collection, query=None, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

Find an object or list of objects in a collection

collection

The collection to find the objects in.

query

The query to use when locating objects in the collection.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

database

The database containing the collection, if not "admin".

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.find mycollection '[{"foo": "FOO", "bar": "BAR"}]' <user> <password> <host> <port> <database> <authdb> <ssl>
salt.modules.mongodb.insert(objects, collection, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

Insert an object or list of objects into a collection.

objects

The documents to store in the collection.

collection

The MongoDB collection to store the documents in.

user

The user to authenticate with, if necessary.

password

The password to authenticate with, if necessary.

host

The host running MongoDB.

port

The port MongoDB is listening on.

database

The database containing the collection, if not "admin".

authdb

The database to authenticate against, if not "admin".

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.insert '[{"foo": "FOO", "bar": "BAR"}, {"foo": "BAZ", "bar": "BAM"}]' mycollection <user> <password> <host> <port> <database>
salt.modules.mongodb.remove(collection, query=None, user=None, password=None, host=None, port=None, database='admin', w=1, authdb=None, ssl=False, verify_ssl=None)

Remove an object or list of objects from a collection

collection

The collection to remove objects from based on the query.

query

Query to determine which objects to remove.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

database

The database where the collection is.

w

The number of matches to remove from the collection.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.remove mycollection '[{"foo": "FOO", "bar": "BAR"}, {"foo": "BAZ", "bar": "BAM"}]' <user> <password> <host> <port> <database> <w> <authdb> <ssl>
salt.modules.mongodb.update_one(objects, collection, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

Update a MongoDB document that matches the provided filter. http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_one

If the document is not found or too many documents match, then an error will be returned.

New in version 2016.11.0.

objects

The objects to update in the collection, should be provided as a list.

collection

The collection to insert the objects into.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

database

The database containing the collection, if not "admin".

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.update_one '{"_id": "my_minion"} {"bar": "BAR"}' mycollection <user> <password> <host> <port> <database> <ssl>
salt.modules.mongodb.user_create(name, passwd, user=None, password=None, host=None, port=None, database='admin', authdb=None, roles=None, ssl=False, verify_ssl=None)

Create a MongoDB user.

name

The name of the user to create.

passwd

The password for the user that is being created.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

database

The MongoDB database to use when checking if the user exists. Default is admin.

authdb

The MongoDB database to use for authentication. Default is None.

roles

The roles that should be associated with the user. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.user_create <user_name> <user_password> <roles> <user> <password> <host> <port> <database> <authdb> <roles> <ssl>
salt.modules.mongodb.user_exists(name, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

Checks if a user exists in MongoDB.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

database

The MongoDB database to use when checking if the user exists. Default is admin.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.user_exists <name> <user> <password> <host> <port> <database> <authdb> <ssl>
salt.modules.mongodb.user_find(name, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

Get single user from MongoDB.

name

The name of the user to find.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

database

The MongoDB database to use when looking for the user. Default is admin.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.user_find <name> <user> <password> <host> <port> <database> <authdb> <ssl>
salt.modules.mongodb.user_grant_roles(name, roles, database, user=None, password=None, host=None, port=None, authdb=None, ssl=False, verify_ssl=None)

Grant one or many roles to a MongoDB user.

name

The name of the user to grant roles to.

roles

JSON list of roles to check either as strings, or JSON objects of rolename and dbname to check. See user_roles_exists or examples below.

database

The database to grant roles on.

user

The user to authenticate with, if necessary.

password

The password to authenticate with, if necessary.

host

The host running MongoDB.

port

The port MongoDB is listening on.

authdb

The database to authenticate against, if not "admin".

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Examples:

salt '*' mongodb.user_grant_roles johndoe '["readWrite"]' dbname admin adminpwd localhost 27017
salt '*' mongodb.user_grant_roles janedoe '[{"role": "readWrite", "db": "dbname" }, {"role": "read", "db": "otherdb"}]' dbname admin adminpwd localhost 27017
salt.modules.mongodb.user_list(user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

List users of a MongoDB database.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

database

The MongoDB database to use when listing users. Default is admin.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.user_list <user> <password> <host> <port> <database> <authdb> <ssl>
salt.modules.mongodb.user_remove(name, user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

Remove a MongoDB user.

name

The name of the user that should be removed.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.user_remove <name> <user> <password> <host> <port> <database> <authdb> <ssl>
salt.modules.mongodb.user_revoke_roles(name, roles, database, user=None, password=None, host=None, port=None, authdb=None, ssl=False, verify_ssl=None)

Revoke one or many roles from a MongoDB user.

name

The name of the user to revoke roles from.

roles

JSON list of roles to revoke either as strings, or JSON objects of rolename and dbname to check. See user_roles_exists or examples below.

database

The database to revoke roles on.

user

The user to authenticate with, if necessary.

password

The password to authenticate with, if necessary.

host

The host running MongoDB.

port

The port MongoDB is listening on.

authdb

The database to authenticate against, if not "admin".

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Examples:

salt '*' mongodb.user_revoke_roles johndoe '["readWrite"]' dbname admin adminpwd localhost 27017
salt '*' mongodb.user_revoke_roles janedoe '[{"role": "readWrite", "db": "dbname" }, {"role": "read", "db": "otherdb"}]' dbname admin adminpwd localhost 27017
salt.modules.mongodb.user_roles_exists(name, roles, database, user=None, password=None, host=None, port=None, authdb=None, ssl=False, verify_ssl=None)

Checks if a user of a MongoDB database has specified roles.

name

The name of the user to check for roles.

roles

JSON list of roles to check either as strings, or JSON objects of rolename and dbname to check. Example: '["salt", "readWrite", "dinner"]' or [{"db": "some_database", "role": "dinner"}, {"db": "another_db", "role": "kaiser"}]

database

The name of the database to check the user and roles against.

user

The user to authenticate with, if necessary.

password

The password to authenticate with, if necessary.

host

The host running MongoDB.

port

The port MongoDB is listening on.

authdb

The database to authenticate against, if not "admin".

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Examples:

salt '*' mongodb.user_roles_exists johndoe '["readWrite"]' dbname admin adminpwd localhost 27017
salt '*' mongodb.user_roles_exists johndoe '[{"role": "readWrite", "db": "dbname" }, {"role": "read", "db": "otherdb"}]' dbname admin adminpwd localhost 27017
salt.modules.mongodb.version(user=None, password=None, host=None, port=None, database='admin', authdb=None, ssl=False, verify_ssl=None)

Get MongoDB instance version.

user

The user to connect to MongoDB as. Default is None.

password

The password to use to connect to MongoDB as. Default is None.

host

The host where MongoDB is running. Default is None.

port

The host where MongoDB is running. Default is None.

authdb

The MongoDB database to use for authentication. Default is None.

ssl

Whether or not to connect to MongoDB over SSL. Default False.

New in version 3008.0.

verify_ssl

Whether or not to verify the server cert when connecting. Default True.

New in version 3008.0.

CLI Example:

salt '*' mongodb.version <user> <password> <host> <port> <database> <ssl>