分享
三行代码  ›  专栏  ›  技术社区  ›  Kinwolf

ansible:如何从yaml中的json响应中提取嵌套字典值 - Ansible : How to extract a nested dictionary value from a JSON response in YAML

  •  0
  • Kinwolf  · 技术社区  · 1 周前

    在剧本中,我们通过它的web api查询密码库。问题是,我要寻找的值在另一个dict内部的dict中,我不知道如何使用yaml语法从嵌套的dict中获取该值。以下是Web请求的内容:

    [localhost] => {
        "cache_control": "no-store",
        "changed": false,
        "connection": "close",
        "content_length": "190",
        "content_type": "application/json",
        "cookies": {},
        "cookies_string": "",
        "date": "Tue, 10 Sep 2019 18:00:18 GMT",
        "elapsed": 0,
        "invocation": {
            "module_args": {
                "attributes": null,
                "backup": null,
                "body": null,
                "body_format": "raw",
                "client_cert": null,
                "client_key": null,
                "content": null,
                "creates": null,
                "delimiter": null,
                "dest": null,
                "directory_mode": null,
                "follow": false,
                "follow_redirects": "safe",
                "force": false,
                "force_basic_auth": false,
                "group": null,
                "headers": {
                    "X-Vault-Token": "<token>"
                },
                "http_agent": "ansible-httpget",
                "method": "GET",
                "mode": null,
                "owner": null,
                "regexp": null,
                "remote_src": null,
                "removes": null,
                "return_content": false,
                "selevel": null,
                "serole": null,
                "setype": null,
                "seuser": null,
                "src": null,
                "status_code": [
                    200
                ],
                "timeout": 30,
                "unix_socket": null,
                "unsafe_writes": null,
                "url": "http://<serverip>:8200/v1/cubbyhole/ansible",
                "url_password": null,
                "url_username": null,
                "use_proxy": true,
                "validate_certs": false
            }
        },
        "json": {
            "auth": null,
            "data": {
                "ansible_password": "<thepassword>"
            },
            "lease_duration": 0,
            "lease_id": "",
            "renewable": false,
            "request_id": "77f067f4-8deb-538b-26d2-8b70f8c83e71",
            "warnings": null,
            "wrap_info": null
        },
        "msg": "OK (190 bytes)",
        "redirected": false,
        "status": 200,
        "url": "http://<serverip>:8200/v1/cubbyhole/ansible"
    }
    

    我要检索的数据是位于“data”dict中的密码,它本身就是json dict的一部分。

    我尝试过的每一种语法似乎都不能比json指令更深入。有什么线索可以继续吗?

    剧本看起来像:(调试部分是最后一次尝试获取密码值)

      - name: get password
        uri:
          url: "http://<serverip>:8200/v1/cubbyhole/ansible"
          method: GET
          validate_certs: no
          headers:
            X-Vault-Token: "{{ansible_token}}"
        register: secret_result
    
        debug:
              msg: "{{item.key['data']}}"
            with_dict: 
              - '{{ secret_result }}'
    

    我看了很多帖子,但大多数都不一样,他们的解决方案不适用。我还尝试了从这些帖子中派生出的大量语法(使用dict和子元素),但都没有成功。任何帮助都将不胜感激。

    1 回复  |  直到 1 周前
        1
  •  0
  •   sebthebert    1 周前

    简单地:

    - debug:
        msg: "{{ secret_result.json['data']['ansible_password'] }}"