mongodb_shell -- Run commands via the MongoDB shell.

Synopsis

Run commands via the MongoDB shell.

Commands provided with the eval parameter or included in a Javascript file.

Attempts to parse returned data into a format that Ansible can use.

Module uses the mongosh shell by default.

Support for mongo is depreciated.

Requirements

The below requirements are needed on the host that executes this module.

Parameters

mongo_cmd (optional, str, mongosh)

The MongoDB shell command.

auto - Automatically detect which MongoDB shell command used. Use "mongosh" if available, else use "mongo" command.

mongo - This should still work for most cases but you might have problems with json parsinf. Use transform_type of 'raw' is you encounter problems.

db (False, str, test)
The database to run commands against
file (optional, str, None)
Path to a file containing MongoDB commands.
eval (optional, str, None)
A MongoDB command to run.
nodb (optional, bool, False)
Specify a non-default encoding for output.
norc (optional, bool, False)
Prevents the shell from sourcing and evaluating ~/.mongorc.js on start up.
quiet (optional, bool, True)
Silences output from the shell during the connection process..
debug (optional, bool, False)
show additional debug info.
transform (optional, str, auto)

Transform the output returned to the user.

auto - Attempt to automatically decide the best tranformation.

split - Split output on a character.

json - parse as json.

raw - Return the raw output.

split_char (optional, str, )
Used by the split action in the transform stage.
stringify (optional, bool, None)

Wraps the command in eval in JSON.stringify(<js cmd>) (mongo) or EJSON.stringify(<js cmd>) (mongosh).

Useful for escaping documents that are returned in Extended JSON format.

Automatically set to false when using mongo.

Automatically set to true when using mongosh.

Set explicitly to override automatic selection.

additional_args (optional, raw, None)

Additional arguments to supply to the mongo command.

Supply as key-value pairs.

If the parameter is a valueless flag supply an empty string as the value.

idempotent (optional, bool, False)

Provides a form of pseudo-idempotency to the module.

We perform a hash calculation on the contents of the eval key or the file name provided in the file key.

When the command is first execute a filed called <hash>.success will be created.

The module will not rerun the command if this file exists and idempotent is set to true.

omit (optional, list, [])

Parameter to omit from the command line.

This should match the parameter name that the MongoDB shell accepts not the module name.

login_user (False, str, None)

The MongoDB user to login with.

Required when login_password is specified.

login_password (False, str, None)

The password used to authenticate with.

Required when login_user is specified.

login_database (False, str, admin)
The database where login credentials are stored.
login_host (False, str, localhost)
The host running MongoDB instance to login to.
login_port (False, int, 27017)
The MongoDB server port to login to.
strict_compatibility (optional, bool, True)
Enforce strict requirements for pymongo and MongoDB software versions

Examples

- name: Run the listDatabases command
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    eval: "db.adminCommand('listDatabases')"

- name: List collections and stringify the output
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    eval: "db.adminCommand('listCollections')"
    stringify: yes

- name: Run the showBuiltinRoles command
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    eval: "db.getRoles({showBuiltinRoles: true})"

- name: Run a js file containing MongoDB commands with pseudo-idempotency
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    file: "/path/to/mongo/file.js"
    idempotent: yes

- name: Provide a couple of additional cmd args
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    eval: "db.adminCommand('listDatabases')"
    additional_args:
      verbose: True
      networkMessageCompressors: "snappy"

Return Values

file (When a js file is used., str, )
JS file that was executed successfully.
msg (always, str, )
A message indicating what has happened.
transformed_output (on success, list, )
Output from the mongo command. We attempt to parse this into a list or json where possible.
changed (always, bool, )
Change status.
failed (on failure, bool, )
Something went wrong.
out (when debug is set to true, str, )
Raw stdout from mongo.
err (when debug is set to true, str, )
Raw stderr from mongo.
rc (when debug is set to true, int, )
Return code from mongo.

Status

Authors

  • Rhys Campbell (@rhysmeister)