【问题标题】:filter the variable in ansible output过滤ansible输出中的变量
【发布时间】:2021-03-14 17:45:51
【问题描述】:

我正在尝试从 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>"

}

【问题讨论】:

    标签: filter ansible jinja2


    【解决方案1】:

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

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

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

    更新:由于您的数据来自 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
    

    【讨论】:

    • 嗨@Zeitounator,感谢您的回复。我尝试了上述解决方案,但它不起作用。我正在添加错误和我在问题中尝试的代码。请做必要的事情。
    • 如何加载示例中提供的数据?请按照已经提出的要求将这一切变成MCVE
    • 我正在将此数据作为我尝试过滤的上一个任务的输出。@Zeitounator 在问题中添加了上一个任务的完整输出供您参考。
    • 清楚地更新了上一个任务的输出。请指教..!提前谢谢..
    • 您是如何注册该输出的?来自任务输出的数据是一回事。产生该输出的任务本身是另一个任务。两者都需要帮助您。
    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多