community.mongodb.mongodb (1.7.3) — lookup

lookup info from MongoDB

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

Authors: Marcos Diez (@marcosdiez)

Install collection

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


Add to requirements.yml

  collections:
    - name: community.mongodb
      version: 1.7.3

Description

The ``MongoDB`` lookup runs the *find()* command on a given *collection* on a given *MongoDB* server.

The result is a list of jsons, so slightly different from what PyMongo returns. In particular, *timestamps* are converted to epoch integers.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- hosts: localhost
  gather_facts: false
  vars:
    mongodb_parameters:
      #mandatory parameters
      database: 'local'
      collection: "startup_log"
      #optional
      connection_string: "mongodb://localhost/"
      # connection_string: "mongodb://username:password@my.server.com:27017/"
      # extra_connection_parameters: { "ssl" : True , "ssl_certfile": /etc/self_signed_certificate.pem" }
      #optional query  parameters, we accept any parameter from the normal mongodb query.
      # filter:  { "hostname": "u18" }
      projection: { "pid": True    , "_id" : False , "hostname" : True }
      skip: 0
      limit: 1
      sort:  [ [ "startTime" , "ASCENDING" ] , [ "age", "DESCENDING" ] ]
  tasks:
    - debug: msg="The PID from MongoDB is {{ lookup('mongodb', mongodb_parameters ).pid }}"

    - debug: msg="The HostName from the MongoDB server is {{ lookup('mongodb', mongodb_parameters ).hostname }}"

    - debug: msg="Mongo DB is stored at {{ lookup('mongodb', mongodb_parameters_inline )}}"
      vars:
        mongodb_parameters_inline:
          database: 'local'
          collection: "startup_log"
          connection_string: "mongodb://localhost/"
          limit: 1
          projection: { "cmdline.storage": True }

      # lookup syntax, does the same as below
    - debug: msg="The hostname is {{ item.hostname }} and the pid is {{ item.pid }}"
      loop: "{{ lookup('mongodb', mongodb_parameters, wantlist=True) }}"

      # query syntax, does the same as above
    - debug: msg="The hostname is {{ item.hostname }} and the pid is {{ item.pid }}"
      loop: "{{ query('mongodb', mongodb_parameters) }}"

    - name: "Raw output from the mongodb lookup (a json with pid and hostname )"
      debug: msg="{{ lookup('mongodb', mongodb_parameters) }}"

    - name: "Yet another mongodb query, now with the parameters on the task itself"
      debug: msg="pid={{item.pid}} hostname={{item.hostname}} version={{ item.buildinfo.version }}"
      with_mongodb:
        - database: 'local'
          collection: "startup_log"
          connection_string: "mongodb://localhost/"
          limit: 1
          projection: { "pid": True    , "hostname": True , "buildinfo.version": True }

    # Please notice this specific query may result more than one result. This is expected
    - name: "Shows the whole output from mongodb"
      debug: msg="{{ item }}"
      with_mongodb:
        - database: 'local'
          collection: "startup_log"
          connection_string: "mongodb://localhost/"

Inputs

    
skip:
    description:
    - How many results should be skipped
    type: integer

sort:
    default: []
    description:
    - Sorting rules.
    - Please use the strings C(ASCENDING) and C(DESCENDING) to set the order.
    - Check the example for more information.
    elements: list
    type: list

limit:
    description:
    - How many results should be shown
    type: integer

filter:
    default: {}
    description:
    - Criteria of the output
    type: dict

database:
    description:
    - Name of the database which the query will be made
    required: true

collection:
    description:
    - Name of the collection which the query will be made
    required: true

projection:
    default: {}
    description:
    - Fields you want returned
    type: dict

connect_string:
    default: mongodb://localhost/
    description:
    - Can be any valid MongoDB connection string, supporting authentication, replica sets,
      etc.
    - More info at U(https://docs.mongodb.org/manual/reference/connection-string/)

extra_connection_parameters:
    default: {}
    description:
    - Extra connection parameters that to be sent to pymongo.MongoClient
    - Check the example to see how to connect to mongo using an SSL certificate.
    - 'All possible parameters are here: U(https://api.mongodb.com/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient)'
    type: dict

Outputs

_list_of_jsons:
  description:
  - a list of JSONs with the results of the MongoDB query.
  type: list