Проблема c синтаксическая ошибка json_query в поле, содержащем все числовые c значения или нечисловые c значения - PullRequest
1 голос
/ 10 июля 2020

Когда я выполняю задачу ansible, я получаю следующее:

fatal: [baseserver2.jlhimpel.net]: FAILED! => {"msg": "JMESPathError в плагине фильтра json_query: \ ninvalid token: Ошибка синтаксического анализа в столбце 59, токен" 32 "(NUMBER), для выражения: \ n" репозиторий [? os == fedora && architecture == x86_64 && release == 32]. {repoUrl: repoUrl} "\ n

Я не знаю, как исправить синтаксическую ошибку. Также обратите внимание, что значение release может быть полностью numeri c или может быть нечисловым c (обратите внимание на разницу между значениями «fedora» и «ubuntu».

Мы будем очень благодарны за любые предложения.

по умолчанию /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}"

1 Ответ

1 голос
/ 10 июля 2020

Из Ansible документации по json_query:

Примечание Здесь цитирование литералов с использованием обратных кавычек позволяет избежать экранирования кавычек и обеспечивает удобочитаемость.

Источник: 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   
...