[英]how to get json_query to search for pattern in Ansible
I am trying to extract the data from a JSON output obtained from API call using uri
module.我正在尝试从使用
uri
模块从 API 调用获得的 JSON output 中提取数据。 The data contains some key value pairs.数据包含一些键值对。 The format is like below
格式如下
item : "name-of-the-item"
"json" : {
"data": {
"First.Name": "First Name"
"Middle.Name": "Middle Name"
"Last.Name": "Last Name"
"Home.Address": "Address "
"Work.Address": "Address 2"
"AuthID": ""
}
}
I am trying to group and output data with extension .Name
so that I get the following outputs First.Name, Middle.Name, Last.Name我正在尝试使用扩展名
.Name
对 output 数据进行分组,以便获得以下输出 First.Name、Middle.Name、Last.Name
Or list the Addresses with .Address
which would lead to outputs of Home.Address and Work.Address或列出带有
.Address
的地址,这将导致 Home.Address 和 Work.Address 的输出
my playbook我的剧本
- name: extract json data from url
uri:
url: https://api.url
method: GET
return_content: true
Content-Type: application/json
register: data_from_url
- name: display the names
debug:
var: item
loop: "{{ data_from_url.json.data | json_query ('Name') }}"
however, the query gives me an error.但是,查询给了我一个错误。 I tried looking into the JMESpath examples, but i cannot find anything which is used to match an extension in the JSON.
我尝试查看 JMESpath 示例,但找不到任何用于匹配 JSON 中的扩展名的内容。 Any help would be appreciated.
任何帮助,将不胜感激。
First of all, naming dict elements with an identifier containing a dot is a bad idea in ansible, and more generally in json/yaml.首先,在 ansible 中,更普遍地在 json/yaml 中,使用包含点的标识符命名 dict 元素是一个坏主意。 I suggest you change this if you can because it will probably add a lot of confusion in the future on your project.
如果可以的话,我建议你改变它,因为它可能会在未来给你的项目增加很多混乱。
Meanwhile, taking your current data as is, json_query
is not the tool you need in this circumstance.同时,按照您当前的数据,
json_query
不是您在这种情况下需要的工具。 Basically what you have to do is to filter out all the keys which do not have .Name
in their name.基本上你要做的就是过滤掉所有名字中没有
.Name
的键。
Here is a quick example to achieve this.这是一个实现此目的的快速示例。 The global idea is to:
全球理念是:
{key: keyname, value: keyvalue}
elements{key: keyname, value: keyvalue}
元素的列表keyname
does not match your criteria with selectattr
(or eventually rejectattr
)keyname
与您的条件不匹配的所有元素selectattr
(或最终rejectattr
) The following playbook:以下剧本:
---
- hosts: localhost
gather_facts: false
vars:
example_data: {
"First.Name": "First Name",
"Middle.Name": "Middle Name",
"Last.Name": "Last Name",
"Home.Address": "Address ",
"Work.Address": "Address 2",
"AuthID": ""
}
tasks:
- name: Show only elements containing ".name"
debug:
msg: "{{ example_data | dict2items | selectattr('key', 'match', '.*\\.Name') | list | items2dict }}"
gives:给出:
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************
TASK [Show only elements containing ".name"] *******************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"First.Name": "First Name",
"Last.Name": "Last Name",
"Middle.Name": "Middle Name"
}
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.