AIX Open Source

AIX Open Source

Share your experiences and connect with fellow developers to discover how to build and manage open source software for the AIX operating system


#Operatingsystems
#Opensource
#AIX
#AIX Open Source
 View Only
Expand all | Collapse all

Ansible - Retrieving attributes from ibm.spectrum_vitrualize.ibm_svcinfo_command module

  • 1.  Ansible - Retrieving attributes from ibm.spectrum_vitrualize.ibm_svcinfo_command module

    Posted Wed December 21, 2022 09:54 AM
      |   view attached
    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
    ------------------------------

    Attachment(s)

    json
    lsfcmap.json   14 KB 1 version


  • 2.  RE: Ansible - Retrieving attributes from ibm.spectrum_vitrualize.ibm_svcinfo_command module

    Posted Sun July 13, 2025 09:05 PM

    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
    ------------------------------



  • 3.  RE: Ansible - Retrieving attributes from ibm.spectrum_vitrualize.ibm_svcinfo_command module

    Posted Mon July 14, 2025 06:35 AM

    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>

    YAML
    - 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
    
    - name: Extract target_vdisk_names
      ansible.builtin.set_fact:
        target_vdisk_names: [] # Initialize an empty list
    
    - name: Populate target_vdisk_names
      ansible.builtin.set_fact:
        target_vdisk_names: "{{ target_vdisk_names + [item.stdout.target_vdisk_name] }}"
      loop: "{{ tgtvol_info.results }}"
      loop_control:
        label: "Processing {{ item.item.FC_mapping_name }}" # Added for better debugging output
      when:
        - item.stdout is defined
        - item.stdout | from_json is defined # Ensure the stdout can be parsed as JSON
        - (item.stdout | from_json).target_vdisk_name is defined # Ensure target_vdisk_name exists
        - item.skipped is not defined or not item.skipped # Only process if not skipped
    
    - debug:
        var: target_vdisk_names
    

    <response-element class="" ng-version="0.0.0-PLACEHOLDER"></response-element>

    Explanation:

    1. 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.

    2. 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.

    3. 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.

    4. 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.

    5. 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
    ------------------------------