community.mongodb.mongodb lookup – 从 MongoDB 中查找信息
注意
此查找插件是 community.mongodb 集合(版本 1.7.8)的一部分。
如果您正在使用 ansible
包,您可能已经安装了此集合。它不包含在 ansible-core
中。要检查是否已安装,请运行 ansible-galaxy collection list
。
要安装它,请使用:ansible-galaxy collection install community.mongodb
。您需要进一步的要求才能使用此查找插件,有关详细信息,请参阅 要求。
要在 playbook 中使用它,请指定:community.mongodb.mongodb
。
community.mongodb 1.0.0 中的新功能
概要
``MongoDB`` 查找在给定的 *MongoDB* 服务器上的给定 *集合* 上运行 *find()* 命令。
结果是一个 json 列表,因此与 PyMongo 返回的结果略有不同。特别是,*时间戳* 会转换为 epoch 整数。
要求
执行此查找的本地控制器节点上需要满足以下要求。
pymongo >= 2.4 (python 库)
关键字参数
这描述了查找的关键字参数。这些是以下示例中的值 key1=value1
、key2=value2
等:lookup('community.mongodb.mongodb', key1=value1, key2=value2, ...)
和 query('community.mongodb.mongodb', key1=value1, key2=value2, ...)
参数 |
注释 |
---|---|
将进行查询的集合的名称 |
|
可以是任何有效的 MongoDB 连接字符串,支持身份验证、副本集等。 有关更多信息,请访问 https://docs.mongodb.org/manual/reference/connection-string/ 默认值: |
|
将进行查询的数据库的名称 |
|
要发送到 pymongo.MongoClient 的额外连接参数 请查看示例,了解如何使用 SSL 证书连接到 mongo。 所有可能的参数都在这里:https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient 默认值: |
|
输出的标准 默认值: |
|
应显示多少结果 |
|
您希望返回的字段 默认值: |
|
应跳过多少结果 |
|
排序规则。 请使用字符串 有关更多信息,请查看示例。 默认值: |
注释
示例
- hosts: localhost
gather_facts: false
vars:
mongodb_parameters:
#mandatory parameters
database: 'local'
collection: "startup_log"
#optional
connection_string: "mongodb://127.0.0.1/"
# connection_string: "mongodb://username:[email protected]: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://127.0.0.1/"
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://127.0.0.1/"
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://127.0.0.1/"
返回值
键 |
描述 |
---|---|
包含 MongoDB 查询结果的 JSON 列表。 已返回: 成功 |