Search code examples
ansiblejson-query

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


When I execute the ansible task, I get the following:

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

I'm not sure how to correct the syntax error. Also, please note that the value of release may be all numeric or may be non-numeric (Note difference between "fedora" values and "ubuntu" values.

Any suggestions would be greatly appreciated.

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}"

Solution

  • From Ansible documentation on json_query:

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

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

    So your your are just missing backticks around your values in your JMESPath expression:

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

    Given the playbook:

    - 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}"
    

    The recap would be

    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