如何从ansible列表中过滤e过滤器字符串

how to filter e filter string from list in ansible

这是我使用下面的 json_query

从嵌套 json 中筛选出来的列表

请看tmpdata的内容

{
  "content_length": "4883",
  "status": 200,
  "cookies": {},
  "changed": false,
  "strict_transport_security": "max-age=15768000",
  "x_api_total_time": "0.234s",
  "_ansible_item_result": true,
  "_ansible_no_log": false,
  "json": {
    "job_tags": "",
    "type": "job",
    "scm_revision": "",
    "status": "successful",
    "credential": null,
    "force_handlers": false,
    "job_slice_number": 0,
    "ask_credential_on_launch": false,
    "started": "2021-02-24T06:48:29.884643Z",
    "ask_job_type_on_launch": false,
    "start_at_task": "",
    "event_processing_finished": true,
    "elapsed": 27.82,
    "finished": "2021-02-24T06:48:57.704791Z",
    "ask_variables_on_launch": true,
    "ask_limit_on_launch": false,
    "job_slice_count": 1,
    "ask_skip_tags_on_launch": false,
    "extra_vars": "{\"domain\": \"abc-cn-1\", \"mgmt_password\": \"admin123\", \"mgmt_server\": \"192.168.20.30\", \"fingerprint\": \"9125544D272B5AD28F3E9AB7BC8AA3F276E45064\", \"fwcmd\": \"fw sam -v  -J src 192.68.10.10\", \"targets\": [\"abc-cn-c1\"]}",
    "use_fact_cache": false,
    "name": "test-Checkpoint",
    "created": "2021-02-24T06:48:29.003796Z",
    "url": "/api/v2/jobs/1198/",
    "vault_credential": null,
    "verbosity": 4,
    "job_args": "[\"ansible-playbook\", \"-u\", \"root\", \"-vvvv\", \"-i\", \"/tmp/awx_1198_e4iie_fm/tmpz4bfietf\", \"-e\", \"@/tmp/awx_1198_e4iie_fm/env/extravars\", \"chkpoint.yml\"]",
    "modified": "2021-02-24T06:48:29.720325Z",
    "unified_job_template": 7,
    "project": 6,
    "limit": "",
    "timeout": 0,
    "host_status_counts": {
      "ok": 1
    },
    "result_traceback": "",
    "launch_type": "manual"
  },
  "server": "nginx/1.12.2",
  "connection": "close",
  "_ansible_parsed": true,
  "allow": "GET, DELETE, HEAD, OPTIONS",
  "redirected": false,
  "cookies_string": "",
  "_ansible_ignore_errors": null
}

我正在使用下面的 json_query 过滤来自临时数据的输出

   - name: "search for traget fw"
     set_fact:
       job_status2: "{{ tmpdata | json_query('[json.status, json.extra_vars]') }}"

请查看上述事实的输出,您可以在列表中看到字典,我只想过滤下面提到的预期输出

当前输出:

ok: [localhost] => {
    "msg": [
        "successful",
        "{\"domain\": \"abc-cn-1\", \"mgmt_password\": \"admxxxxxin123\", \"mgmt_server\": \"192.168.10.20\", \"fingerprint\": \"9125544D272B5AD28F3E9AB7BC8AA3F276E45064\", \"fwcmd\": \"fw sam -v  -J src 192.68.10.10\", \"targets\": [\"abc-cn-c1\"]}"
    ]
}

预期输出:

["successful", targets": ["abc-cn-c1"]]

如果可以的话,请帮我 id

如果您想继续使用 json_query,您的问题(至少)有两个答案,一个可以完全避免混乱。我们将从后者开始,因为它对我来说更少打字:-)

- set_fact:
   job_status2: '{{ [ tmpdata.json.status, (tmpdata.json.extra_vars|from_json).targets] }}'

第二个是一个细微的变化,因为某人需要将嵌入的JSON解析为字典,而(AFAIK)JMESPath没有这样的能力

- set_fact:
    job_status2: "{{ newdata | json_query('[json.status, json.extra_vars.targets]') }}"
  vars:
    newdata: >-
     {{ tmpdata
     | combine({"json":{"extra_vars", tmpdata.json.extra_vars|from_json}}, recursive=True)
     }}

我提到那个主要是为了你正在用 json_query 做其他事情的情况,因此知道如何强制结构对 JMESPath

更友好会很有帮助

最后,您可以欺骗并使用 the JMESPath literal notation 将已经 JSON 化的字符串直接嵌入到查询中

- set_fact:
    job_status2: "{{ newdata | json_query('[json.status, `' + newdata.json.extra_vars + '`.targets]') }}"