community.mongodb.mongodb_shell (1.6.1) — module

Run commands via the MongoDB shell.

| "added in version" 1.1.0 of community.mongodb"

Authors: Rhys Campbell (@rhysmeister)

Install collection

Install with ansible-galaxy collection install community.mongodb:==1.6.1


Add to requirements.yml

  collections:
    - name: community.mongodb
      version: 1.6.1

Description

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

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Run the listDatabases command
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    eval: "db.adminCommand('listDatabases')"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: List collections and stringify the output
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    eval: "db.adminCommand('listCollections')"
    stringify: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Run the showBuiltinRoles command
  community.mongodb.mongodb_shell:
    login_user: user
    login_password: secret
    eval: "db.getRoles({showBuiltinRoles: true})"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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"

Inputs

    
db:
    default: test
    description:
    - The database to run commands against
    required: false
    type: str

eval:
    description:
    - A MongoDB command to run.
    type: str

file:
    description:
    - Path to a file containing MongoDB commands.
    type: str

nodb:
    default: false
    description:
    - Specify a non-default encoding for output.
    type: bool

norc:
    default: false
    description:
    - Prevents the shell from sourcing and evaluating ~/.mongorc.js on start up.
    type: bool

omit:
    default: []
    description:
    - Parameter to omit from the command line.
    - This should match the parameter name that the MongoDB shell accepts not the module
      name.
    elements: str
    type: list

debug:
    default: false
    description:
    - show additional debug info.
    type: bool

quiet:
    default: true
    description:
    - Silences output from the shell during the connection process..
    type: bool

mongo_cmd:
    default: mongosh
    description:
    - 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.
    type: str

stringify:
    default: null
    description:
    - 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.
    type: bool

transform:
    choices:
    - auto
    - split
    - json
    - raw
    default: auto
    description:
    - 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.
    type: str

atlas_auth:
    default: false
    description:
    - Authentication path intended for MongoDB Atlas Instances
    type: bool

idempotent:
    default: false
    description:
    - 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.
    type: bool

login_host:
    default: localhost
    description:
    - The host running MongoDB instance to login to.
    required: false
    type: str

login_port:
    default: 27017
    description:
    - The MongoDB server port to login to.
    required: false
    type: int

login_user:
    description:
    - The MongoDB user to login with.
    - Required when I(login_password) is specified.
    required: false
    type: str

split_char:
    default: ' '
    description:
    - Used by the split action in the transform stage.
    type: str

login_database:
    default: admin
    description:
    - The database where login credentials are stored.
    required: false
    type: str

login_password:
    description:
    - The password used to authenticate with.
    - Required when I(login_user) is specified.
    required: false
    type: str

additional_args:
    description:
    - 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.
    type: raw

strict_compatibility:
    default: true
    description:
    - Enforce strict requirements for pymongo and MongoDB software versions
    type: bool

Outputs

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