【问题标题】:Ansible and JMESPath, escape forward slash in a json_queryAnsible 和 JMESPath,在 json_query 中转义正斜杠
【发布时间】:2019-02-09 22:46:19
【问题描述】:

有一个简单的 JSON 文件,sample.json,内容如下:

{
  "test": {
    "domain": [
      {
        "name": "cluster1"
      }
    ]
  }
}

使用 Ansible,我想查询 test 键,它适用于以下 Ansible 剧本。

---
- hosts: localhost
  vars:
    tmpdata: "{{ lookup('file','sample.json') | from_json }}"

    - debug:
        msg: "{{ tmpdata | json_query('test') }}"

剧情

ok: [localhost] => {
    "msg": {
        "domain": [
            {
                "name": "cluster1"
            }
        ]
    }
}

但是,当他们将 JSON 文件中的键从 test 更改为 test/something,并将 ansible json_query 从 test 更改为 test/something 时,Ansible/JMESPath 会产生错误。

fatal: [localhost]: FAILED! => {"msg": "JMESPathError in json_query filter plugin:\nBad jmespath expression: Unknown token /:\ntest/something\n    ^"}

我查看了JMESpath documentation,但对我来说没有意义。

如何确保 JMESpath 在 Ansible 查询中使用正斜杠。

【问题讨论】:

    标签: ansible jmespath


    【解决方案1】:

    JMESPath 将identifier 定义为unquoted-string / quoted-string

    unquoted-stringA-Za-z_。应该引用其他任何内容。

    在你的情况下:

    - debug:
        msg: "{{ tmpdata | json_query('\"test/something\"') }}"
    

    这里我们转义\",因为我们在YAML双引号msg: "..."中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2015-01-23
      相关资源
      最近更新 更多