1

Please help me figure out what I'm wrong with. I'm getting a JSON from Ansible and filtering it, after which I want to save the output and reuse it. But, unfortunately, I get an error that this attribute does not exist. Where did I go wrong?

playbook code:

var:
  query_general: "body.results[].{display_name: display_name, subnets: subnets[]}"
- name: parsing query
  set_fact:  
    myvar: "{{ results | json_query(query_general) }}"
  register: output

- name: qwe
  set_fact: 
    scndjson: "{{ output.myvar[].display_name }}"

- name: print
  debug: 
    msg: "{{ scndjson }}"

I tried the json_query second case as well, but that didn't work either.

in register:output i have:

[
            {
                "display_name": "1test",
                "subnets": [
                    {
                        "gateway_address": "0.0.0.0/25",
                        "network": "0.0.0.0/25"
                    }
                ]
            },
            {
                "display_name": "test",
                "subnets": [
                    {
                        "gateway_address": "0.0.0.1/25",
                        "network": "0.0.0.1/25"
                    }
                ]
            }
            ]

error:

The task includes an option with an undefined variable.

it can be: output, display_name, etc

UPD: I corrected the yaml, there are no errors, but the data is not displayed.

tasks:
   - name: 
     nsxt_rest:
       hostname: anyhost
       username: anyuser 
       password: anypass
       validate_certs: false
       method: get
       path: /policy/api/v1/infra/segments
     register: nsx_results

   - debug: 
         var: nsx_query_general
     vars: 
       nsx_query_general: "{{ nsx_results | json_query('body.results[].{display_name: display_name, subnets: subnets[]}') }}"
     register: output
  
   - debug:
        var: secondjson
     vars: 
       secondjson: "{{ output|json_query('[].display_name') }}"

Output from nsx_query_general:

{
    "nsx_query_general": [
        {
            "display_name": "test",
            "subnets": [
                {
                    "gateway_address": "0.0.0.0/25",
                    "network": "0.0.0.0/25"
                }
            ]
        },
        {
            "display_name": "1test",
            "subnets": [
                {
                    "gateway_address": "0.0.0.1/25",
                    "network": "0.0.0.1/25"
                }
            ]
        }]}

Output from secondjson:

{
    "secondjson": "",
    "_ansible_verbose_always": true,
    "_ansible_no_log": false,
    "changed": false
}

1 Answers1

0

Given the registered variable output

output:
  - display_name: 1test
    subnets:
    - gateway_address: 0.0.0.0/25
      network: 0.0.0.0/25
  - display_name: test
    subnets:
    - gateway_address: 0.0.0.1/25
      network: 0.0.0.1/25

Either use json_query

scndjson: "{{ output|json_query('[].display_name') }}"

, or map attribute

scndjson: "{{ output|map(attribute='display_name')|list }}"

Both declarations create the list

scndjson: [1test, test]

Example of a complete playbook

- hosts: localhost
  vars: 
    output:
      - display_name: 1test
        subnets:
        - gateway_address: 0.0.0.0/25
          network: 0.0.0.0/25
      - display_name: test
        subnets:
        - gateway_address: 0.0.0.1/25
          network: 0.0.0.1/25
  tasks:
    - debug:
        var: scndjson
      vars:
        scndjson: "{{ output|json_query('[].display_name') }}"
    - debug:
        var: scndjson
      vars:
        scndjson: "{{ output|map(attribute='display_name')|list }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Thank you very much for your reply, Vladimir. I also tried using json_query, no errors, but no data =( if I output register:output data via debug:msg , then I can see it. But if I add a query, I get: { "changed": false, "ansible_facts": { "parse": "" }, "_ansible_no_log": false } What else can I add to the question to help sort out the problem? Maybe the difficulty is due to the fact that I'm trying to parse exactly the output of the previous request? – Alexander Shelestov Jun 22 '22 at 12:11
  • You're welcome. [edit] the question and make it [mre]. Post *results*. Post expected result. In *set_fact*, there is no reason to register a variable. Use the variable you set instead. Do not put code into the comment if you can put it into the question. – Vladimir Botka Jun 22 '22 at 12:44