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



当我执行ansible任务时,我得到以下内容:

致命:[baseserver2.jlhimpel.net]:失败=>{quot;msg":json_query筛选器插件中的JMESPathError:无效令牌:分析第59列的令牌"32"(NUMBER(处的错误,用于表达式:\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

任务/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文档

注意
在这里,使用反引号引用文字可以避免转义引号并保持可读性。

来源:https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-查询过滤器

因此,在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   

最新更新