过滤ansible输出中的变量

filter the variable in ansible output

我正在尝试从 ansible 中的一个任务的输出中过滤一个变量。但挑战是我有变量的值,需要得到反向的变量。

我得到以下输出的任务。

    - uri:
      url: "https://*********/nics/nic1"
        method: GET
        force_basic_auth: true
        headers:
          cookie: "{{ login.cookies_string }}"
        validate_certs: false
      register: test

输出:-

    ok: [192.168.84.203] => {
        "allow": "GET, PUT", 
        "invocation": {
            "module_args": {
                "status_code": [
                    200
                ],
            }
        }, 
        "json": {
            "body": {
                "interfaces": {
                     "@order": [
                         "ff7574025754b3df1647001"
                     ], 
                     "ff7574025754b3df1647001": {
                         "addresses": {
                             "1": "192.168.1.4/22", 
                             "@order": [
                                 "1"
                             ]
                         }, 
                         "mtu": 1500, 
                         "name": "default", 
                         "source_based_routes": [], 
                         "vlantag": 0
                     }
                }, 
           }
           }
        }
    }

上面的输出有 1: 192.168.1.3/22,我需要从中检索“1”,我手边有 192.168.1.4/22。 这基本上与我们经常做的相反。在此感谢您的帮助。

我尝试了下面的任务,但没有成功。

    - name: find key
      vars:
        key_list: >-
          {{ 
            body.interfaces
            | dict2items
            | selectattr('value.addresses', 'defined')
            | map(attribute='value.addresses')
            | map('dict2items')
            | flatten
            | selectattr('value', 'eq', rl_ip)
            | map(attribute='key')
          }}
      debug:
        msg: "This is the key:{{ key_list }}"

最终输出:

    TASK [find key] *********************************************************************************
    ok: [192.168.84.203] => {
    "msg": "This is the key: <generator object do_map at 0x7fe0a693fc80>"

}

你想找到一个对应于它包含的给定值的字典键。这里的困难在于它嵌套在更高级别的结构中,并且您可能在同一个字典中有多个结果。

下一个问题的旁注:请不要提供人们必须重建才能测试的截断输出,并在他们的答案中给出示例:创建一个 MCVE

简而言之,下面的剧本示例应该给出预期的结果(如果我从截断的示例中正确理解了您的数据结构)

Update:因为您的数据来自 uri 任务,在 json 键中返回 http 调用,并且您注册该任务会导致名为 test 的变量我更新了下面的示例以符合您的情况。

---
- name: Find key demo
  hosts: localhost
  gather_facts: false

  vars:
    # This is what you data looks like after the uri call
    test:
      json:
        "body": {
          "interfaces": {
            "@order": [
                "ff7574025754b3df1647001"
            ],
            "ff7574025754b3df1647001": {
              "addresses": {
                "1": "192.168.1.4/22",
                "@order": [
                    "1"
                ]
              },
              "mtu": 1500,
              "name": "default",
              "source_based_routes": [ ],
              "vlantag": 0
            }
          }
        }

    # this is what we are looking for
    needle_in_haystack: "192.168.1.4/22"

  tasks:
    - name: find key(s) in haystack for given needle
      vars:
        key_list: >-
          {{
            test.json.body.interfaces
            | dict2items
            | selectattr('value.addresses', 'defined')
            | map(attribute='value.addresses')
            | map('dict2items')
            | flatten
            | selectattr('value', 'eq', needle_in_haystack)
            | map(attribute='key')
            | list
          }}
      debug:
        msg: "This is the list of found keys: {{ key_list }}"

结果:

PLAY [Find key demo] *******************************************************************************************************************************************************************************************************************

TASK [find key(s) in haystack for given needle] ****************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is the list of found keys: ['1']"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0