在Ansible中过滤JSON

ogsagwnx  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(94)

你好,我有下面的json输出,它是由API调用收集的

ok: [localhost] => {
    "_ticket_details": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "cache_control": "no-cache, no-store",
                "changed": false,
                "connection": "close",
                "content_type": "application/json; charset=utf-8",
                "cookies": {},
                "cookies_string": "",
                "date": "Mon, 24 Jul 2023 15:03:54 GMT",
                "elapsed": 0,
                "expires": "Wed, 13 Oct 2010 00:00:00 UTC",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "body": null,
                        "body_format": "raw",
                        "ca_path": null,
                        "client_cert": null,
                        "client_key": null,
                        "creates": null,
                        "dest": null,
                        "follow_redirects": "safe",
                        "force": false,
                        "force_basic_auth": true,
                        "group": null,
                        "headers": {
                            "Authorization": "",
                            "Content-Type": "application/json"
                        },
                        "http_agent": "ansible-httpget",
                        "method": "GET",
                        "mode": null,
                        "owner": null,
                        "remote_src": false,
                        "removes": null,
                        "return_content": false,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": null,
                        "status_code": [
                            200
                        ],
                        "timeout": 30,
                        "unix_socket": null,
                        "unredirected_headers": [],
                        "unsafe_writes": false,
                        "url": "https://testapi.com/api/v2/tickets/513010/requested_items",
                        "url_password": null,
                        "url_username": null,
                        "use_gssapi": false,
                        "use_proxy": true,
                        "validate_certs": true
                    }
                },
                "item": 513010,
                "json": {
                    "requested_items": [
                        {
                            "cost_per_request": 0.0,
                            "created_at": "2023-07-24T14:57:24Z",
                            "custom_fields": {
                                "adapt_account_required_please_sate_which___perm": null,
                                "address": null,
                                "branch_country": "United Kingdom,
                                "branch_location___contractor": "United Kingdom",
                            },
                            "delivery_time": null,
                            "id": 50007379930,
                            "is_parent": true,
                            "loaned": false,
                            "quantity": 1,
                            "remarks": null,
                            "service_item_id": 196,
                            "service_item_name": "Test",
                            "stage": 1,
                            "updated_at": "2023-07-24T14:57:24Z"
                        }
                    ]
                },
                "msg": "OK (unknown bytes)",
                "nel": "{ \"report_to\": \"\", \"max_age\": 2592000, \"include_subdomains\": true}",
                "pragma": "no-cache",
                "redirected": false,
                "report_to": "{ \"group\": \"\", \"max_age\": 2592000, \"include_subdomains\": true, \"endpoints\": [{\"url\": \"https://edge.eu"}]}",
                "server": "fwe",
                "status": 200,
                "transfer_encoding": "chunked",
                "url": "https://testapi.com/api/v2/tickets/513010/requested_items",
                "x_envoy_upstream_service_time": "104",
                "x_freshservice_api_version": "latest=v2; requested=v2",
                "x_fw_ratelimiting_managed": "true",
                "x_ratelimit_remaining": "139",
                "x_ratelimit_total": "140",
                "x_ratelimit_used_currentrequest": "1",
                "x_request_id": "28f7252b-2199-49b5-a751-178cceb00b3b",
                "x_trace_id": "00-d9a92eb7b7f5e3480a56c13f06a7fbec-a9bc5e842bc8b604-00"
            }
        ],
        "skipped": false
    }
}

字符串
我需要创建一个字典使用项目字段(513010),并添加分支国家了这个输出。我将进一步添加其他领域也下自定义领域的输出也。
我试图用下面的代码访问这个值,但在custom_fields下很难深入,因为它似乎是一个嵌入式数组。

- name: "Filter Json"
      set_fact:
        _json_filter: "{{ item.json.requested_items}}"
      with_items: "{{ _ticket_details.results }}"


但却不能让它工作。
有人能帮忙吗?似乎方括号在输出(0)中导致了另一个数组,我似乎无法通过。

ui7jx7zq

ui7jx7zq1#

如果你在你的例子中替换branch_country的值,那么我们有:

.
.
.
              "custom_fields": {
                "adapt_account_required_please_sate_which___perm": null,
                "address": null,
                "branch_country": "United Kingdom",
                "branch_location___contractor": "United Kingdom"
              },
.
.
.

字符串
然后执行以下任务:

- name: "Get branch countries"
  set_fact:
    _json_filter: >-
      {{
        _ticket_details |
        json_query('results[].json.requested_items[].custom_fields[].branch_country')
      }}


_json_filter的值设置为["United Kingdom"]

相关问题