gpt4 book ai didi

json - 当值包含ansible中的字符串时解析json值

转载 作者:行者123 更新时间:2023-12-05 04:30:33 24 4
gpt4 key购买 nike

这个 ansible 剧本有效

---
- hosts: localhost
gather_facts: False

vars:
jq: "[?contains(name, 'Pizza')]"
json: |
[{
"name": "Ted's Sub Shop - 720895714701",
"templateid": "24632"
},
{
"name": "Ted's Pizza - 720895714702",
"templateid": "24663"
}]

tasks:
- name: DEBUG
debug:
msg: "{{ json | from_json | json_query(jq) }}"

它返回以下内容

ok: [localhost] => {
"msg": [
{
"name": "Ted's Pizza - 720895714702",
"templateid": "24663"
}
]
}

我需要更进一步,当 name 的值包含 Pizza 时,我需要它仅返回末尾的 12 位数字。所以返回输出看起来像这样

ok: [localhost] => {
"msg": "720895714702"
}

想法?

最佳答案

你可以试试

- name: testplaybook jinja2
hosts: localhost
gather_facts: no
vars:
json: |
[{
"name": "Ted's Sub Shop - 720895714701",
"templateid": "24632"
},
{
"name": "Ted's Pizza - 720895714702",
"templateid": "24663"
}]
tasks:
- name: DEBUG
debug:
msg: "{{ json | from_json | selectattr('name', 'contains' , 'Pizza')
| map(attribute='name')
| map('regex_replace', '^.*?(\\d*)$', '\\1')}}"

结果:

ok: [localhost] => {
"msg": [
"720895714702"
]
}

关于json - 当值包含ansible中的字符串时解析json值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72047807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com