【问题标题】:problems with nested json_query嵌套 json_query 的问题
【发布时间】:2019-05-10 08:07:31
【问题描述】:

我有以下 json 结构。

    "results": [
        {
            "ltm_pools": [
                {
                    "members": [
                        {
                            "full_path": "/Common/10.49.128.185:8080",
                        },
                        {
                            "full_path": "/Common/10.49.128.186:8080",
                        }

                    "name": "Staging-1-stresslab",
                },
                {
                    "members": [
                        {
                            "full_path": "/Common/10.49.128.187:0",
                        },
                        {
                            "full_path": "/Common/10.49.128.188:0",
                        }
                    ],
                    "name": "Staging-2-lab",
                },

尝试执行此类操作时出现错误

  - debug:
      msg: "{{item[0].host}} --- {{ item[1] }} ---  {{ item[2] }}"
    with_nested:
      - "{{F5_hosts}}"
      - "{{bigip_facts | json_query('[results[0].ltm_pools[*].name]') | flatten }}"
      - "{{bigip_facts | json_query('[results[0].ltm_pools[?name.contains(@,'Staging'].members[::2].full_path]') | flatten }}"

我无法让第三个数组工作。

我想从 name 包含 staging 的所有对象中打印偶数成员 full_path 变量。

我希望有人可以帮助我,我已经为此苦苦挣扎了好几天。

【问题讨论】:

    标签: ansible json-query


    【解决方案1】:

    根据我自己看到/阅读/尝试过的情况,您陷入了这个错误:https://github.com/ansible/ansible/issues/27299

    这是由 Ansible 运行的“包含”JMESPath 函数的问题,引用: https://github.com/ansible/ansible/issues/27299#issuecomment-331068246

    问题与 Ansible 使用自己的字符串类型有关:AnsibleUnicodeAnsibleUnsafeText。 而且只要 jmespath 库有非常严格的类型检查,它就不能接受这种类型作为字符串文字。

    还有一个建议的解决方法,如果您将变量转换为 json 并返回,则其中的字符串具有正确的类型。长话短说,这是行不通的:

    "{{bigip_facts | json_query('results[0].ltm_pools[?name.contains(@,`Staging`)==`true`].members[::2].full_path') }}"
    

    但确实如此:

    "{{bigip_facts | to_json | from_json | json_query('results[0].ltm_pools[?name.contains(@,`Staging`)==`true`].members[::2].full_path') }}"
    

    我已经成功运行了这样的代码:

    - hosts: all
      gather_facts: no
      tasks:
        - set_fact:
            bigip_facts: 
              results: 
                - ltm_pools:
                    - members: 
                        - full_path: "/Common/10.49.128.185:8080"
                        - full_path: "/Common/10.49.128.186:8080"
                      name: "Staging-1-stresslab"
                    - members: 
                        - full_path: "/Common/10.49.128.187:0"
                        - full_path: "/Common/10.49.128.188:0"
                      name: "Staging-2-stresslab"
    
        - name: "Debug ltm-pools"
          debug:
            msg: "{{ item }}"
          with_items:
            - "{{bigip_facts | to_json | from_json | json_query('results[0].ltm_pools[?name.contains(@,`Staging`)==`true`].members[::2].full_path') }}"
    

    它可以按你的意愿工作:

    PLAY [all] *****************************************************************************************
    
    TASK [set_fact] ************************************************************************************
    ok: [localhost]
    
    TASK [Debug ltm-pools] *****************************************************************************
    ok: [localhost] => (item=[u'/Common/10.49.128.185:8080']) => {
        "msg": [
            "/Common/10.49.128.185:8080"
        ]
    }
    ok: [localhost] => (item=[u'/Common/10.49.128.187:0']) => {
        "msg": [
            "/Common/10.49.128.187:0"
        ]
    }
    
    PLAY RECAP *****************************************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      相关资源
      最近更新 更多