gpt4 book ai didi

ansible - 如果 json 键名称中包含破折号,则带有 json 循环的 Ansible playbook 不起作用

转载 作者:行者123 更新时间:2023-12-02 20:02:18 24 4
gpt4 key购买 nike

如果 json 键名称中包含破折号,Ansible playbook 将无法工作,如果没有破折号,则可以正常工作。

我尝试使用 from_json 和 json_query,但遇到了同样的问题

这是我必须解析的输出:

ok: [2.2.2.2] => {
"bgp": {
"response": {
"@status": "success",
"result": {
"entry": [
{
"@peer": "V4",
"@vr": "VR",
"ORF-entry-received": "0",
"aggregate-confed-as": "yes",
"config": {
"remove-private-as": "yes"
},
"connect-retry-interval": "15",
"established-counts": "0",
"holdtime": "0",
"holdtime-config": "90",
"idle-hold": "15",
"keepalive": "0",
"keepalive-config": "30",
"last-error": null,
"last-update-age": "634",
"local-address": "3.3.3.3",
"msg-total-in": "0",
"msg-total-out": "0",
"msg-update-in": "0",
"msg-update-out": "0",
"multi-hop-ttl": "1",
"nexthop-peer": "no",
"nexthop-self": "no",
"nexthop-thirdparty": "yes",
"open-delay": "0",
"passive": "no",
"password-set": "no",
"peer-address": "2.2.2.2",
"peer-capability": null,
"peer-group": "PEER_A",
"peer-router-id": "0.0.0.0",
"peering-type": "Unspecified",
"prefix-counter": null,
"prefix-limit": "5000",
"reflector-client": "not-client",
"remote-as": "65000",
"same-confederation": "no",
"status": "Connect",
"status-duration": "0",
"status-flap-counts": "49"
}
]
}
}
}
}

这是我当前的代码(当我添加“对等地址”时出现问题):

 - debug:
msg:
peer-address: "{{ item.peer-address }}"
status: "{{ item.status }}"
loop: "{{ (result_3.stdout|from_json).response.result.entry }}"

这是我收到的错误:

{"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'peer'\n\nThe error appears to have been in '/etc/ansible/Playbook-v2.yml': line 49, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}

我只想获取我正在解析的两个键和值:

"peer-address": "2.2.2.2"
"status": "Connect"

最佳答案

这是你的问题:

peer-address: "{{ item.peer-address }}"

名称 peer-address 不是有效的标识符(在 Jinja 语法中,与大多数语言一样,变量名称不能包含 -)。您可以使用替代语法来引用键:

peer-address: "{{ item['peer-address'] }}"

关于ansible - 如果 json 键名称中包含破折号,则带有 json 循环的 Ansible playbook 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55574776/

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