#ansible json_query help

1 messages · Page 1 of 1 (latest)

lilac badger
#

Hi all, bit of a newbie to json out etc, anyone help me with command to get these onto one line each, as in one field from each group, many thanks

TASK [Display all devices] **********************************************************************************************************************************
ok: [localhost] => {
"msg": [
[
"MIATFLADQH004.edg.centurylink.net (fc:96:43:37:e5:40)",
"MIATFLADQH004.edg.centurylink.net (fc:96:43:37:e5:40)",
"LONDENEHQH003.edg.centurylink.net (00:cc:34:af:10:00)",
"LONDENEHQH003.edg.centurylink.net (00:cc:34:af:10:00)"
],
[
"MIATFLADSN0201SNC02",
"MIATFLADSN0201SNC01",
"LONDENEHSN0201SNC01",
"LONDENEHSN0201SNC02"
],
[
"et-0/0/2",
"et-0/0/0",
"et-0/0/0",
"et-0/0/2"
],
[
"e1a",
"e1a",
"e1a",
"e1a"
]
]
}

my msg params are as follows;

  • name: Display all devices
    debug:
    msg: "{{ echo | community.general.json_query('[results[].msg.records[].discovered_device,results[].msg.records[].node,results[].msg.records[].interface,results[].msg.records[].port]' ) }}"
native thorn
#

Can you give a bit more context what you are trying to achieve?

lilac badger
#

yes sorry, i didnt think I explained it well

#

MIATFLADSN0201SNC02, et-0/0/2, e1a

#

would like to see it in this format for each entry a line of results

lilac badger
#

i can do it through a jq statement from a json file but would like to get it straight from the playbook

acoustic tapir
#

your playbook prints json. If you don't want to print json, you need to change the debug: part in your playbook. loop over the number of elements and do something like echo | results[].msg.records[i].node, results[].msg.records[i].port or something similar

lilac badger
acoustic tapir
#

yeah, as I said, I think you need to put the debug statement in a loop that iterates over the number of entries, and then print each entry by itself

#

I don't think you can do elaborate json transforms in the playbook itself

subtle patio
subtle patio
#

Thinking cap back on

acoustic tapir
#

I think you do something like with_sequence: start=1 end={{ something }} and then use {{ item }} in your debug statement for the index in the array... the something I'm not sure of, something about the length of results[].msg.records but not sure about the correct syntax there