ansiblejmespathjson-query

Ansible how to filter dict key with double colon ::


Trying to filter "10.17.101.149" with a json_query. From this data:

{
  "fact": { 
    "network::ipv4_address": "10.17.101.149",
    "network": "test"
  }
}

My json_query:

- name: "get ip address"
  hosts: test
  connection: local
  vars:
    vm: {
  "facts": { 
    "network::ipv4_address": "10.17.101.149",
    "network": "test"
  }
} 

  tasks:

    - debug:
        msg: "{{ vm.facts | community.general.json_query(jms) }}"
      vars:
        jms: "network\"::\"ipv4_address"

I do not find the right way to escape the double colon "::" in "network::ipv4_address". I tried different ways to escape but no correct ones. jms: "network\":\"\":\"ipv4_address" jms: "network\":\":\"ipv4_address" jms: "nework\"*\"ipv4_address"

I've checked the docs. Ansible yaml does not give much info about a colon. https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html


Solution

  • Escaping characters in Jmspath/json, should be be done by enclosing the whole "key" by quotes.

    This one works. jms: '"network::ipv4_address"'

    This post gave me the answer. Escape '.' in JMESPath

    For some reason the official doc, did not help. Looking again at the official doc, i find this https://jmespath.org/proposals/raw-string-literals.html but its unfindable when your are not searching for raw string literals. Basically chicken and egg issue when you do not know "programming" theory and the right terminology. Luckily SO is here.