gpt4 book ai didi

Ansible playbook 中的 Mongodb 查找错误

转载 作者:行者123 更新时间:2023-12-03 15:59:55 28 4
gpt4 key购买 nike

我正在将 mongoDB 与 ansible 结合使用,尝试使用 ansible playbook 查询 mongodb 集合。

main.yaml(我声明了所有变量的变量文件)

source_dir_qry_res: "/home/dpatel/Desktop/query_output/"
mongodb_parameters:
- { collection: '1.1.1.1-mx', filter: {"config.groups.interfaces.interface.name" : "xyz"} , qry_res_file: 'allconfig' }

在上面的文件中,“filter”是针对 mongodb 集合运行的查询。我通过引用“Mongo Lookup inside this Ansible Lookup doc http://docs.ansible.com/ansible/playbooks_lookups.html”来使用过滤器。

#main_run.yaml

- name: query from mongodb collection
connection: local
gather_facts: no
vars_files:
- 'vars/main.yml'
tasks:
- name: "query to db"
query_config:
collection: "{{ item.collection }}"
filter: "{{ item.filter}}"
qry_res_file: "{{ item.qry_res_file}}"
source_dir: "{{ source_dir_qry_res }}"
with_items: "{{ mongodb_parameters }}"
tags: "save-query-result"

query_config(自定义 ansible 模块)

def main():
module = AnsibleModule(
argument_spec=dict(
#host=dict(required=True),
collection=dict(required=False),
qry_res_file=dict(required=False),
filter = dict(required=False),
source_dir=dict(required=True),
logfile=dict(required=False, default=None)),
supports_check_mode=True)
m_args = module.params
m_results = dict(changed=False)

try:
conn = pymongo.MongoClient()
#db = conn.m_args['dtbase']
db = conn.mydb
coll_name = m_args['collection']
print "Connected successfully!!!"
except Exception as ex:
module.fail_json(msg='mongodb connection error: %s' % ex.message)

try:
query = db[coll_name].find(m_args['filter'])
if(query):
target_json_file = m_args['source_dir']+m_args['collection'] + m_args['qry_res_file'] + ".json"
for ele in query:
del ele['_id']
with open(target_json_file, 'a') as the_file:
json.dump(ele, the_file)

except Exception as ex:
module.fail_json(msg="failed to get query result: %s" %ex.message)

module.exit_json(**m_results)

from ansible.module_utils.basic import *


if __name__ == '__main__':
main()

mongo集合(mydb[1.1.1.1-mx].find())

{"config": { "groups": {"name": "123", "system": {"host-name": "something"}, "interfaces": {"interface": {"name": "xyz"}}}}}

当我从 mongo shell 运行以下查询时,它工作正常。

mydb[1.1.1.1-mx].find({"config.groups.interfaces.interface.name" : "xyz"})

但是当我尝试通过代码运行相同的查询时,它给出了以下错误

$ ansible-playbook -i hosts main_run.yml
error msg:"failed to get query result: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping"

请查看这些屏幕截图以获取详细的错误消息。如果有人有任何解决此问题的想法,请分享您的想法,这将非常有帮助。

enter image description here

最佳答案

我会尝试在您的argument_spec中设置所需的类型

    argument_spec=dict(
collection = dict(required=False),
qry_res_file = dict(required=False),
filter = dict(required=False, type='dict'),
source_dir = dict(required=True),
logfile = dict(required=False, default=None))

因为默认情况下参数类型是字符串,所以最终您的过滤器对象会转换为字符串(请参阅输出中的 incovation.module_args)。

关于Ansible playbook 中的 Mongodb 查找错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com