【问题标题】:Ansible Playbook - Pysnow: Find record using 'IN' clause queryAnsible Playbook - Pysnow:使用“IN”子句查询查找记录
【发布时间】:2020-07-26 03:04:33
【问题描述】:

我正在编写 ansible playbook 以使用 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"]}}

如何解决此错误并使其正常工作?任何建议都可以,因为我已经筋疲力尽地考虑这个了..

提前致谢。

【问题讨论】:

    标签: python ansible in-clause


    【解决方案1】:

    使用equals 代替IN,根据值,雪查询构建器将添加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 创建。

    【讨论】:

    • 您好,感谢您的回答。这就是 api 文档中的含义。但是,我仍然收到预期的“str”和“list”错误。但我设法根据你的回答解决了它。该剧本在其他评论中。谢谢。
    【解决方案2】:

    感谢 franklinsijo 的回答。当ansible看到列表时自动应用'IN',只需使用标准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
    

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 1970-01-01
      • 2015-04-02
      • 2010-10-23
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      相关资源
      最近更新 更多