Community.general.json_query returns the text "Hello World!"

When running a query on a json data structure, the return value is “Hello World!”
Query:

- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    var: "{{ ethernet_details | json_query(\"results[*].stdout_lines.ipv4.method\") }}"

You are misusing var; it is intended to be the name of a variable, and can be abused to be a Jinja expression, but because you have wrapped the expression in {{ }} you are passing neither. You should instead be using msg:

- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    msg: "{{ ethernet_details | json_query(\"results[*].stdout_lines.ipv4.method\") }}"

The next part of your problem is that your json_query is returning an empty string. You didn’t provide any sample data so it’s impossible to say definitively what you’re doing wrong, but as a general piece of advice: json_query() is rarely necessary and does not directly help you deal with JSON data.

Taking a wild guess at the actual data structure you’re working with, you might have meant something more like this:

- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    msg: "{{ ethernet_details.results | map(attribute='stdout') | map('from_json') | map(attribute='ipv4.method') }}"
2 Likes

I find that using JMESPath is often easier and quicker than chaining Jinja2 filters together because you can use jp and even better jpterm for working out the JMESPath query without using Ansible (eg jpterm foo.json)…

Perhaps this might work?

- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    var: ethernet_details | community.general.json_query('results[*].stdout_lines.ipv4.method')

However without knowing the content of ethernet_details I don’t know for sure…

Turned out that JMESPath wasn’t parsing it correctly the input data was a mess. It was the output from nmcli and was pseudo-json but missing some syntax items. Basically the output was more like this:

"stdout_lines": [
                "connection.id:                          Wired connection 4",
                "connection.uuid:                        3b6cdbf8-5cc2-3a94-a4f6-476359e2444d",
                "connection.stable-id:                   --",
                "connection.type:                        802-3-ethernet",
                "connection.interface-name:              enp0s9",
                "connection.autoconnect:                 yes",
                "connection.autoconnect-priority:        -999",
                "connection.autoconnect-retries:         -1 (default)",
                "connection.multi-connect:               0 (default)",
                "connection.auth-retries:                -1",
                "connection.timestamp:                   1732214614",
                "connection.read-only:                   no",
                "connection.permissions:                 --",
                "connection.zone:                        --",
                "connection.master:                      --",
                "connection.slave-type:                  --",
                "connection.autoconnect-slaves:          -1 (default)",
                "connection.secondaries:                 --",
                "connection.gateway-ping-timeout:        0",
                "connection.metered:                     unknown",
                "connection.lldp:                        default",
                "connection.mdns:                        -1 (default)",
                "connection.llmnr:                       -1 (default)",
                "connection.dns-over-tls:                -1 (default)",
                "connection.wait-device-timeout:         -1",
                "802-3-ethernet.port:                    --",
                "802-3-ethernet.speed:                   0",
                "802-3-ethernet.duplex:                  --",
                "802-3-ethernet.auto-negotiate:          no",
                "802-3-ethernet.mac-address:             --",
                "802-3-ethernet.cloned-mac-address:      --",
                "802-3-ethernet.generate-mac-address-mask:--",
                "802-3-ethernet.mac-address-blacklist:   --",
                "802-3-ethernet.mtu:                     auto",
                "802-3-ethernet.s390-subchannels:        --",
                "802-3-ethernet.s390-nettype:            --",
                "802-3-ethernet.s390-options:            --",
                "802-3-ethernet.wake-on-lan:             default",
                "802-3-ethernet.wake-on-lan-password:    --",
                "802-3-ethernet.accept-all-mac-addresses:-1 (default)",
                "ipv4.method:                            auto",
                "ipv4.dns:                               --",
                "ipv4.dns-search:                        --",
                "ipv4.dns-options:                       --",
                "ipv4.dns-priority:                      0",
                "ipv4.addresses:                         --",
                "ipv4.gateway:                           --",
                "ipv4.routes:                            --",
                "ipv4.route-metric:                      -1",
                "ipv4.route-table:                       0 (unspec)",
                "ipv4.routing-rules:                     --",
                "ipv4.ignore-auto-routes:                no",
                "ipv4.ignore-auto-dns:                   no",
                "ipv4.dhcp-client-id:                    --",
                "ipv4.dhcp-iaid:                         --",
                "ipv4.dhcp-timeout:                      0 (default)",
                "ipv4.dhcp-send-hostname:                yes",
                "ipv4.dhcp-hostname:                     --",
                "ipv4.dhcp-fqdn:                         --",
                "ipv4.dhcp-hostname-flags:               0x0 (none)",
                "ipv4.never-default:                     no",
                "ipv4.may-fail:                          yes",
                "ipv4.required-timeout:                  -1 (default)",
                "ipv4.dad-timeout:                       -1 (default)",
                "ipv4.dhcp-vendor-class-identifier:      --",
                "ipv4.dhcp-reject-servers:               --",
                "ipv6.method:                            auto",
                "ipv6.dns:                               --",
                "ipv6.dns-search:                        --",
                "ipv6.dns-options:                       --",
                "ipv6.dns-priority:                      0",
                "ipv6.addresses:                         --",
                "ipv6.gateway:                           --",
                "ipv6.routes:                            --",
                "ipv6.route-metric:                      -1",
                "ipv6.route-table:                       0 (unspec)",
                "ipv6.routing-rules:                     --",
                "ipv6.ignore-auto-routes:                no",
                "ipv6.ignore-auto-dns:                   no",
                "ipv6.never-default:                     no",
                "ipv6.may-fail:                          yes",
                "ipv6.required-timeout:                  -1 (default)",
                "ipv6.ip6-privacy:                       -1 (unknown)",
                "ipv6.addr-gen-mode:                     stable-privacy",
                "ipv6.ra-timeout:                        0 (default)",
                "ipv6.dhcp-duid:                         --",
                "ipv6.dhcp-iaid:                         --",
                "ipv6.dhcp-timeout:                      0 (default)",
                "ipv6.dhcp-send-hostname:                yes",
                "ipv6.dhcp-hostname:                     --",
                "ipv6.dhcp-hostname-flags:               0x0 (none)",
                "ipv6.token:                             --",
                "proxy.method:                           none",
                "proxy.browser-only:                     no",
                "proxy.pac-url:                          --",
                "proxy.pac-script:                       --"
            ]

I’m a little disappointed, but what can you do. I ended up having chain it in the end anyhow. I wish it would have allowed me to post it as an attachment in the end.

This was what I ended up with: when: item.stdout_lines is defined and item.stdout | regex_findall("ipv4.method:\\s+auto") | length > 0

1 Like