1

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?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
George
  • 45
  • 3

1 Answers1

1

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
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thank you, when I run the new playbook, the debug task did not return the values: ansible-playbook NetApp_version.yml -vvv
    TASK [debug] task path: /var/lib/jenkins/workspace/Infra-NetApp_ONTAP_Upgrade/NetApp_version.yml:21 ok: [localhost] => { "cluster_node_info.ontap_info['cluster/nodes'].records\n | map(attribute='version.full')\n | map('split', ':')\n | map(attribute=0)": "" } META: ran handlers META: ran handlers
    – George Jun 28 '23 at 12:32
  • You are on a pretty old version of Ansible and you should consider upgrading. But on an old version like this, just throw a `| list` at the end to cast the generator into a list. – β.εηοιτ.βε Jun 28 '23 at 20:22