【问题标题】:Anible playbook with json loop does not work if json key has a dash in the name如果 json 键的名称中有破折号,则带有 json 循环的 Ansible 剧本不起作用
【发布时间】:2019-04-08 13:37:36
【问题描述】:

如果 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"
                    }
                ]
            }
        }
    }
}

这是我当前的代码(当我添加“peer-address”时会出现问题):

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

这是我得到的错误:

{"msg": "任务包含一个带有未定义变量的选项。错误是:'dict object' has no attribute 'peer'\n\n错误似乎出现在 '/etc/ansible/Playbook -v2.yml':第 49 行,第 6 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题。\n\n违规行似乎是:\n\n\n - 调试:\n ^ 此处\n"}

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

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

【问题讨论】:

  • 嘿,我可以建议将您的问题的标题更改为:“当 key 包含破折号时循环 json 不起作用”,我认为它会不那么混乱。

标签: ansible


【解决方案1】:

这是你的问题:

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

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

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

【讨论】:

  • 谢谢!我以为我做错了什么
猜你喜欢
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多