1

我使用 Ansible 过滤器 json_query

数据:

[
  "just_dir",
  {
     "path": "extend_dir",
     "order": "nginx"
  }
]

我想得到:

[
  {
     "path": "just_dir",
  },
  {
     "path": "extend_dir",
     "order": "nginx"
  }
]
merage([?type(@) == `string`].{path: @}, [?type(@) == `object`])

不起作用。

4

1 回答 1

1

不幸的是,看起来不可能合并到 2 个数组

Github 问题:jmespath.py#152

您还需要from_json在应用之前添加一个过滤器json_query

---
- hosts: localhost
  gather_facts: no
  vars:
    data: '[
      "just_dir",
      {
         "path": "extend_dir",
         "order": "nginx"
      }
    ]'
  tasks:
   - name: debug just_dir
     debug: msg="{{ data | from_json  | json_query(jmesquery) }}"
     vars:
       jmesquery: "[?type(@) == `string`].{path: @}"

   - name: debug Other data
     debug: msg="{{ data | from_json  | json_query(jmesquery) }}"
     vars:
       jmesquery: "[?type(@) == `object`]"
于 2020-02-19T13:19:09.400 回答