#Problem looping through JSON output

1 messages · Page 1 of 1 (latest)

kindred jacinth
#

Could someone please tell me what I'm doing wrong here?
I am using the na_ontap_rest_info module to pull info from a cluster, and then I want to loop through the data to perform an action on each item.

I am able to do this with the nodes of the cluster, no problem (see the first attachment with output: get_cifs_servers_rest.yml). But, when I try to do this with CIFs servers, it tells me that there is no 'name' attribute (see code and output: get_cifs_servers_rest.yml).

If I display "{{ cifs_list['ontap_info']['protocols/cifs/services']['records'][0]['name'] }}" it returns the name of the CIFs server. So, I don't understand why it's not seeing it when I try to do the loop?

Any help would be appreciated!

willow ruin
#

could you trying printing out just item to see what it has in item

kindred jacinth
lean stream
#

Pretty sure you are missing one level of the information, "item" in this case is each record so that would mean "item.svm.name" is the SVM name in each record.
Ie. ['records'][0]['name'] -> ['records'][0]['svm']['name']

kindred jacinth
# lean stream Pretty sure you are missing one level of the information, "item" in this case is...

I thought that as well, but having ['svm']['name'] doesn't work either.
If you look at a single record, you'll notice that 'name' is listed twice, once at the root of the record, and again under 'svm', so I would think that either should work? But, neither is working.

{ "_links": { "self": { "href": "/api/protocols/cifs/services/ac674f21-8d6b-11ee-b2b8-d039eaa7e243" } }, "ad_domain": { "fqdn": "DOMAIN.NET", "organizational_unit": "OU=Computers,DC=DOMAIN,DC=NET" }, "name": "VS04", "svm": { "_links": { "self": { "href": "/api/svm/svms/ac674f21-8d6b-11ee-b2b8-d039eaa7e243" } }, "name": "vs04", "uuid": "ac674f21-8d6b-11ee-b2b8-d039eaa7e243" } },

willow ruin
kindred jacinth
#

Thanks, Chris.
I tried that, but it didn't change anything.

I changed the gather_subset from "protocols/cifs/services" to "cifs_services_info"

and the loop to:

`loop:

  • "{{ cifs_list['ontap_info']['protocols_cifs_services']['records'] }}"`

For the msg, if I just have:

msg: "{{ item }}"

It returns the entire item, but if I try to return any of the sub-elements, it gives the same error that the attribute doesn't exist. These are the syntaxes I've tried:

msg: "{{ item.name }}"
msg: "{{ item.svm.name }}"
msg: "{{ item['name'] }}"
msg: "{{ item['svm']['name'] }}"

willow ruin
#

is msg: "{{ item }}" showing all records or just a single one?

kindred jacinth
#

This is what the output looks like if I just specify msg: "{{ item }}" while looping through the records.

willow ruin
#

It feels like the loop isn't working.

Item should be a single item like this
{ "_links": { "self": { "href": "/api/protocols/cifs/services/0cdd6616-84ae-11ee-b2b8-d039eaa7e243" } }, "ad_domain": { "fqdn": "DOMAIN.NET", "organizational_unit": "OU=Computers,DC=DOMAIN,DC=NET" }, "name": "VS01", "svm": { "_links": { "self": { "href": "/api/svm/svms/0cdd6616-84ae-11ee-b2b8-d039eaa7e243" } }, "name": "vs01", "uuid": "0cdd6616-84ae-11ee-b2b8-d039eaa7e243" } },

but it appear to be the entire list.

astral sentinel
#

Using loop: can be 'touchy' sometimes ... I did this to get the results you're looking for I think. One task uses with_items and the last task uses loop: with a 'flatten' pipe on the result.

#

TASK [Display CIFS Server Names (with_items)] **********************************************************************************************************
ok: [localhost] => (item=SVMCIFS) => {
"msg": "CIFS Server: SVMCIFS SVM: svmCIFS"
}

TASK [Display CIFS Server Names (loop flatten)] ********************************************************************************************************
ok: [localhost] => (item=SVMCIFS) => {
"msg": "CIFS Server: SVMCIFS SVM: svmCIFS"
}

kindred jacinth
#

Thank you, @astral sentinel, that is exactly what I needed!
I'm still trying to wrap my head around why it works though. I find the Ansible documentation to be rather cryptic, so I'm going to re-read it again in the morning and see if it makes any more sense. What's confusing to me is: Why does the loop work for the JSON output for nodes, but doesn't work for the CIFs servers? Is it because there is that additional level beneath "svm:" that includes "name:" and "uuid:", and it has to flatten that?

It seems that RedHat/Ansible wants us to use "loop" instead of "with_items" though. Any idea why one is better than the other? Seems like just using "with_items" all the time would avoid problems like this. I realize that loop: "{{ list_var | flatten(levels=1) }}" accomplishes the same thing, but seems like an unnecessary complication.