Ansible Playbook - Pysnow:使用 'IN' 子句查询查找记录
Ansible Playbook - Pysnow: Find record using 'IN' clause query
我正在编写 ansible 剧本,通过使用 snow_record_find 模块来获取 SNOW 记录。文档 (https://docs.ansible.com/ansible/latest/modules/snow_record_find_module.html) 的示例非常有限。
除此之外,我也无法准确理解 api 文档 (https://pysnow.readthedocs.io/en/latest/api/query_builder.html)。
我试过这个玩法:
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
sys_id:
IN:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord
并得到这个错误:
Expected value of type `str` or `list`, not <class 'dict'>", "query": {"sys_id": {"IN": ["5203930cdb230010a5d39235ca9619f6", "605d12bedbe70010a5d39235ca9619dd", "81115fc8db230010a5d39235ca96193d"]}}
我也修改了我的剧本是这样的:
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
IN:
sys_id:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord
- debug:
msg: "{{allVarsRecord}}"
然后得到这个错误:
Expected value of type `str` or `list`, not <class 'dict'>", "query": {"IN": {"sys_id": ["5203930cdb230010a5d39235ca9619f6", "605d12bedbe70010a5d39235ca9619dd", "81115fc8db230010a5d39235ca96193d"]}}
我该如何解决这个错误并使它正常工作?任何建议都可以,因为我已经筋疲力尽地想这个了..
提前致谢。
使用 equals
而不是 IN
,根据值,snow 查询构建器将添加 IN
(如果是列表)或 =
(如果是字符串)条件.
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
equals:
sys_id:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord
PS: 未经测试。基于查询生成器创建 documentation.
感谢franklinsijo 的回答。 'IN' 在 ansible 查看列表时自动应用,只需使用标准 equals 即可。但是对于这种情况,因为它是单查询,所以它也不需要 'equals' 操作,否则我仍然会得到预期的 'str' 和 'list' 错误。剧本如下所示:
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
sys_id:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord
我正在编写 ansible 剧本,通过使用 snow_record_find 模块来获取 SNOW 记录。文档 (https://docs.ansible.com/ansible/latest/modules/snow_record_find_module.html) 的示例非常有限。
除此之外,我也无法准确理解 api 文档 (https://pysnow.readthedocs.io/en/latest/api/query_builder.html)。
我试过这个玩法:
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
sys_id:
IN:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord
并得到这个错误:
Expected value of type `str` or `list`, not <class 'dict'>", "query": {"sys_id": {"IN": ["5203930cdb230010a5d39235ca9619f6", "605d12bedbe70010a5d39235ca9619dd", "81115fc8db230010a5d39235ca96193d"]}}
我也修改了我的剧本是这样的:
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
IN:
sys_id:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord
- debug:
msg: "{{allVarsRecord}}"
然后得到这个错误:
Expected value of type `str` or `list`, not <class 'dict'>", "query": {"IN": {"sys_id": ["5203930cdb230010a5d39235ca9619f6", "605d12bedbe70010a5d39235ca9619dd", "81115fc8db230010a5d39235ca96193d"]}}
我该如何解决这个错误并使它正常工作?任何建议都可以,因为我已经筋疲力尽地想这个了..
提前致谢。
使用 equals
而不是 IN
,根据值,snow 查询构建器将添加 IN
(如果是列表)或 =
(如果是字符串)条件.
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
equals:
sys_id:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord
PS: 未经测试。基于查询生成器创建 documentation.
感谢franklinsijo 的回答。 'IN' 在 ansible 查看列表时自动应用,只需使用标准 equals 即可。但是对于这种情况,因为它是单查询,所以它也不需要 'equals' 操作,否则我仍然会得到预期的 'str' 和 'list' 错误。剧本如下所示:
- name: Find records in sc_item_option list
snow_record_find:
username: username
password: password
instance: instance
table: sc_item_option
query:
sys_id:
- "5203930cdb230010a5d39235ca9619f6"
- "605d12bedbe70010a5d39235ca9619dd"
- "81115fc8db230010a5d39235ca96193d"
register: allVarsRecord