I didn't but times have changed an AI can help now.
Here's what Google Gemini says:
You're on the right track using register and from_json to get the output. The key to extracting the target_vdisk_name from tgtvol_info when it's registered from a loop is understanding its structure.
When you register a variable within a loop, Ansible stores the results of each iteration in a results list within the registered variable. Each item in this results list will contain the output from that specific loop iteration.
Here's how you can access the target_vdisk_name values:
<response-element class="" ng-version="0.0.0-PLACEHOLDER"></response-element>
<response-element class="" ng-version="0.0.0-PLACEHOLDER"></response-element>
Explanation:
-
set_fact: target_vdisk_names: []: We initialize an empty list called target_vdisk_names. This is good practice to ensure the variable exists and is a list before we start appending to it.
-
loop: "{{ tgtvol_info.results }}": This loop iterates over the results list within your tgtvol_info registered variable. Each item in this loop will represent the output from one iteration of your previous ibm_svcinfo_command task.
-
when: conditions:
-
item.stdout is defined: Checks if stdout exists for the current loop item. This is important because ignore_errors: True might lead to some entries in results not having a stdout if the command truly failed.
-
item.stdout | from_json is defined: Ensures that the stdout content can actually be parsed as JSON. If the command returned non-JSON output, from_json would fail.
-
(item.stdout | from_json).target_vdisk_name is defined: This is the crucial part. It attempts to parse the stdout of the current item as JSON and then checks if the target_vdisk_name key exists within that parsed JSON.
-
item.skipped is not defined or not item.skipped: This condition specifically filters out the entries that were skipped by the when: "'FC_mapping_name' in tgtvol" in your original task.
-
target_vdisk_names: "{{ target_vdisk_names + [(item.stdout | from_json).target_vdisk_name] }}":
-
For each item that passes the when conditions, we take its stdout.
-
We apply | from_json to parse the string stdout into a JSON object (or Python dictionary).
-
Then, we access the target_vdisk_name key from that parsed JSON.
-
Finally, we append this target_vdisk_name to our target_vdisk_names list.
-
debug: var=target_vdisk_names: This will print the final list containing all the extracted target_vdisk_name values, which should be your desired TEST_SGC_TEST_l01 to l05.
This approach ensures that you only try to access target_vdisk_name from valid JSON outputs that actually contain the FC_mapping_name and were not skipped.
------------------------------
Glenn Robinson
------------------------------
Original Message:
Sent: Sun July 13, 2025 09:04 PM
From: DOMINIC LANCASTER
Subject: Ansible - Retrieving attributes from ibm.spectrum_vitrualize.ibm_svcinfo_command module
Hi Glenn, did you ever resolve this issue ? - happy to assist if not resolved
I'd use something like:
tasks:
- debug:
msg: "{{ result.stdout | json_query(target_query) }}"
vars:
target_query: "[*].target_vdisk_name"
------------------------------
DOMINIC LANCASTER
Original Message:
Sent: Wed December 21, 2022 09:54 AM
From: Glenn Robinson
Subject: Ansible - Retrieving attributes from ibm.spectrum_vitrualize.ibm_svcinfo_command module
I've written some Ansible roles to create/delete Flashcopy consistency groups on an SV storage unit.
I want to get the target_vdisk_name attribute from stdout, see attached Json file.
The Json is generated by the following lines in my role:
- name: Retrieve the target volume name for each FC mapping ibm.spectrum_virtualize.ibm_svcinfo_command: clustername: "{{ clustername }}" username: "{{ username }}" password: "{{ password }}" command: "svcinfo lsfcmap {{ tgtvol.FC_mapping_name }}" loop: "{{ (lsfcconsistgrp.stdout | from_json) }}" when: "'FC_mapping_name' in tgtvol" register: tgtvol_info loop_control: loop_var: tgtvol label: "{{ tgtvol.FC_mapping_name }}" ignore_errors: True- debug: var=tgtvol_info
The ignore_errors is there because the first entry in the lsfcconsistgrp.stdout register does not contain "FC_mapping_name" so I want to ignore that and continue.
What I'm after is the stdout.target_vdisk_name value from the tgtvol_info register. There should be five of these, TEST_SGC_TEST_l01 to l05.
I've been trying to access these values for hours and getting nowhere.
Any help appreciated.
------------------------------
Glenn Robinson
------------------------------
#AIXOpenSource