-1

我有以下 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 }}"

我无法让第三个阵列工作。

我想从名称包含暂存的所有对象中打印偶数成员 full_path 变量。

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

4

1 回答 1

0

根据我自己看到/阅读/尝试过的内容,您陷入了这个错误: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   
于 2019-05-10T22:14:21.570 回答