包含所有数值或非数值的字段出现有问题的 json_query 语法错误

Problematic json_query syntax error on a field containing either all numeric values or non-numeric values

当我执行 ansible 任务时,我得到以下信息:

fatal: [baseserver2.jlhimpel.net]: FAILED! => {"msg": "JMESPathError in json_query filter plugin:\ninvalid token: Parse error at column 59, token "32" (NUMBER), for expression:\n"repository[?os==fedora && architecture==x86_64 && release==32].{repoUrl: repoUrl}"\n

我不确定如何更正语法错误。另外,请注意 release 的值可能全是数字,也可能不是数字(注意“fedora”值和“ubuntu”值之间的区别。

如有任何建议,我们将不胜感激。

defaults/main.xml

distributionRepositoryUrl:
  repository:
    - os: fedora
      architecture: aarch64
      release: 32
      repoUrl: https://download.fedoraproject.org:/pub/fedora/linux/releases/32/Server/aarch64/os/
    - os: fedora
      architecture: x86_64
      release: 32
      repoUrl: https://download.fedoraproject.org:/pub/fedora/linux/releases/32/Server/x86_64/os/
    - os: ubuntu
      architecture: amd64
      release: 18_04
      repoUrl: https://archive.ubuntu.com:/ubuntu/dists/bionic/main/installer-amd64/
    - os: ubuntu
      architecture: i386
      release: 18_04
      repoUrl: https://archive.ubuntu.com:/ubuntu/dists/bionic/main/installer-i386/

vars/main.yml

   os: fedora
   architecture: x86_64
   release: 32

tasks/main.yml

- name: Display os architecture release
  debug:
    msg: "os:{{ os }} architecture:{{ architecture }} release:{{ release }}"

- name: Lookup fedora x86_64 32 repoUrl
  debug:
    msg: "repoUtl:{{ fedVar.repoUrl }}"
  loop: "{{ distributionRepositoryUrl | json_query(url_query) | flatten }}"
  loop_control:
    loop_var: fedVar
  vars:
    url_query: "repository[?os=={{ os }} && architecture=={{ architecture }} && release=={{ release }}].{repoUrl: repoUrl}"

来自 json_query 上的 Ansible 文档:

Note
Here, quoting literals using backticks avoids escaping quotes and maintains readability.

来源:https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-query-filter

所以您只是在 JMESPath 表达式中缺少围绕您的值的反引号:

url_query: "repository[?os==`{{ os }}` && architecture==`{{ architecture }}` && release==`{{ release }}`].{repoUrl: repoUrl}"

鉴于剧本:

- hosts: all
  gather_facts: no
  vars:
    distributionRepositoryUrl:
      repository:
        - os: fedora
          architecture: aarch64
          release: 32
          repoUrl: https://download.fedoraproject.org:/pub/fedora/linux/releases/32/Server/aarch64/os/
        - os: fedora
          architecture: x86_64
          release: 32
          repoUrl: https://download.fedoraproject.org:/pub/fedora/linux/releases/32/Server/x86_64/os/
        - os: ubuntu
          architecture: amd64
          release: 18_04
          repoUrl: https://archive.ubuntu.com:/ubuntu/dists/bionic/main/installer-amd64/
        - os: ubuntu
          architecture: i386
          release: 18_04
          repoUrl: https://archive.ubuntu.com:/ubuntu/dists/bionic/main/installer-i386/
    
    os: fedora
    architecture: x86_64
    release: 32
  
  tasks:
    - name: Display os architecture release
      debug:
        msg: "os:{{ os }} architecture:{{ architecture }} release:{{ release }}"

    - name: Lookup fedora x86_64 32 repoUrl
      debug:
        msg: "repoUtl:{{ fedVar.repoUrl }}"
      loop: "{{ distributionRepositoryUrl | json_query(url_query) | flatten }}"
      loop_control:
        loop_var: fedVar
      vars:
        url_query: "repository[?os==`{{ os }}` && architecture==`{{ architecture }}` && release==`{{ release }}`].{repoUrl: repoUrl}"

回顾是

PLAY [all] ********************************************************************************************************

TASK [Display os architecture release] ****************************************************************************
ok: [localhost] => {
    "msg": "os:fedora architecture:x86_64 release:32"
}

TASK [Lookup fedora x86_64 32 repoUrl] ****************************************************************************
ok: [localhost] => (item={'repoUrl': 'https://download.fedoraproject.org:/pub/fedora/linux/releases/32/Server/x86_64/os/'}) => {
    "msg": "repoUtl:https://download.fedoraproject.org:/pub/fedora/linux/releases/32/Server/x86_64/os/"
}

PLAY RECAP ********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0