Extract a single value from ONTAP output

172 Views Asked by At

How can I extract the version from the lengthy output of the module netapp.ontap.na_ontap_rest_info?

This is my playbook and its output is quite lengthy

---
- hosts: localhost
  collections:
    - netapp.ontap
  tasks:
  - name: run ONTAP gather facts for cluster_node_info
    netapp.ontap.na_ontap_rest_info:
      hostname: "{{ lookup('env', 'ClusterName') }}"
      username: "{{ lookup('env', 'ontap_admin_usr') }}"
      password: "{{ lookup('env', 'ontap_admin_pwd') }}"
      use_rest: always
      https: true
      validate_certs: false
      gather_subset:
        - cluster_node_info
      fields:
        - 'version'

From the example output below, I only need the first part of the version full, namely NetApp Release 9.12.1P4

ok: [localhost] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "cert_filepath": null,
            "feature_flags": null,
            "fields": [
                "version"
            ],
            "force_ontap_version": null,
            "gather_subset": [
                "cluster_node_info"
            ],
            "hostname": "clusterhostname",
            "http_port": null,
            "https": true,
            "ignore_api_errors": null,
            "key_filepath": null,
            "max_records": 1024,
            "ontapi": null,
            "owning_resource": null,
            "parameters": null,
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "state": null,
            "use_python_keys": false,
            "use_rest": "always",
            "username": "svc_ans-ontap",
            "validate_certs": false
        }
    },
    "ontap_info": {
        "cluster/nodes": {
            "_links": {
                "self": {
                    "href": "/api/cluster/nodes?max_records=1024&fields=version"
                }
            },
            "num_records": 2,
            "records": [
                {
                    "_links": {
                        "self": {
                            "href": "/api/cluster/nodes/578658e0-87fc-11e9-b8bd-00a098f2ea00"
                        }
                    },
                    "name": "node2",
                    "uuid": "578658e0-87fc-11e9-b8bd-00a098f2ea00",
                    "version": {
                        "full": "NetApp Release 9.12.1P4: Tue Jun 06 20:41:34 UTC 2023",
                        "generation": 9,
                        "major": 12,
                        "minor": 1
                    }
                },
                {
                    "_links": {
                        "self": {
                            "href": "/api/cluster/nodes/6aae0abe-a410-11e9-ae1d-00a098f2ea72"
                        }
                    },
                    "name": "node1",
                    "uuid": "6aae0abe-a410-11e9-ae1d-00a098f2ea72",
                    "version": {
                        "full": "NetApp Release 9.12.1P4: Tue Jun 06 20:41:34 UTC 2023",
                        "generation": 9,
                        "major": 12,
                        "minor": 1
                    }
                }
            ]
        }
    }
}

What do I need to add to the playbook to process the version output to return only NetApp Release 9.12.1P4?

1

There are 1 best solutions below

2
On

You should, first, register the output of your task.

Then, you can access the different keys with either the dot or array notations, and since you do have a slash in the key cluster/nodes, you will at least have to use the array notation there.

Since you do have a list in records, you might want to use the map filter in order to get version for all the items of that list.

Then you can further use the map filter to split the string and extract the part you're interested in.

So, in the end your tasks should be:

- name: run ONTAP gather facts for cluster_node_info
  netapp.ontap.na_ontap_rest_info:
    hostname: "{{ lookup('env', 'ClusterName') }}"
    username: "{{ lookup('env', 'ontap_admin_usr') }}"
    password: "{{ lookup('env', 'ontap_admin_pwd') }}"
    use_rest: always
    https: true
    validate_certs: false
    gather_subset:
      - cluster_node_info
    fields:
      - 'version'
  register: cluster_node_info

- debug:
    var: >- 
      cluster_node_info.ontap_info['cluster/nodes'].records
        | map(attribute='version.full')
        | map('split', ':')
        | map(attribute=0)

Which would yield

ok: [localhost] => 
  ? |-
    cluster_node_info.ontap_info['cluster/nodes'].records
      | map(attribute='version.full')
      | map('split', ':')
      | map(attribute=0)
  : - NetApp Release 9.12.1P4
    - NetApp Release 9.12.1P4