#use rest_info to rename things

1 messages · Page 1 of 1 (latest)

grim pasture
#

trying to use the rest_info module to input into other tasks to rename things such as nodes and LIFs.

- name: Get info from cluster na_ontap_rest_info: use_rest: always gather_subset: - cluster/nodes fields: - 'name' <<: *login register: ontap_info - debug: msg: "{{ ontap_info.ontap_info}}"

- name: Rename Nodes na_ontap_node: use_rest: always from_name: "{{ ontap_info.ontap_info.records.name }}" name: "{{ node1name }}" <<: *login
This prints the nodename and other things, i cant seem to figure out how to narrow the output down to just the 'name'. when specifying fields it still shows uuid and other items in structure.

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Node not found: {'cluster/nodes': {'records': [{'uuid': '76ca839a-bc22-11ed-a1c9-000c29957a37', 'name': 'cluster0300-01', '_links': {'self': {'href': '/api/cluster/nodes/76ca839a-bc22-11ed-a1c9-000c29957a37'}}}], 'num_records': 1, '_links': {'self': {'href': '/api/cluster/nodes?max_records=1024&fields=name'}}}}"}

I realise this is not a netapp ansible problem, but if anyone can help my format the json output that would be awsome.

empty quartz
#

I've been wrestling with this myself and this is how I finally solved how to grab the node names:

    netapp.ontap.na_ontap_rest_info:
        gather_subset: "cluster_node_info"
    register: ontap_info

  - name: Extracting name of nodes
    set_fact:
      ontap_nodes: "{{ ontap_info.ontap_info['cluster/nodes'].records | json_query(query) }}"
    vars:
      query: "[*].name"

  - debug:
     msg: "{{ ontap_nodes }}"
     verbosity: 1```

I think it has something to do with Ansible not being able to read JSON so it has to be converted, or something like that.
grim pasture
#

thanks mate, i had use json_query before and was hoping for a method that didnt require it as its another package the customer will need. but seems like thats the recommended ansible way so lets go with it.

kindred widget
#

@empty quartz @grim pasture
I'd appreciated it very much if you could please help me out on the playbook. Your playbook sounds a great one for me to learn. Or, if you could please post the final version of yours.
`-
name: Practice
hosts: localhost
gather_facts: False
collection:
- netapp.ontap
tasks:
- name: Get cluster node info
netapp.ontap.na_ontap_rest_info:
gather_subset: "cluster_node_info"
hostname: x.x.x.x
username: admin
password: netapp123
register: ontap_info
- name: Extracting name of nodes
https: true
validate_certs: false
set_fact:
ontap_nodes: "{{ ontap_info.ontap_info['cluster/nodes'].records | json_query(query) }}"
vars:
query: "[*].name"

  - debug:
      msg: "{{ ontap_nodes }}"
      verbosity: 1`
empty quartz
grim pasture
#

this issue im seeing with this playbook is that the nodename is passed with [] around it and causing error. think its to do with the [*].name query as if i change to [0] it works but then this will let allow for multiple items. fatal: [localhost]: FAILED! => {"changed": false, "msg": "Node not found: ['oldnodename']"}

#

using -vvv shows this in more detail "from_name": "['oldnodename']",

grim pasture
# kindred widget <@1019515080947281951> <@1006755555253166102> I'd appreciated it very much if y...

try this. i stole it from another post in here seems to work well and doesnt use json_query.

` tasks:
- name: Get list of nodes in the cluster
na_ontap_rest_info:
<<: *login
gather_subset:
- cluster/nodes
register: node_list

- name: Rename Nodes
  na_ontap_node:
    use_rest: never
    from_name: "{{ item.name }}"
    name: "{{ node1name }}"
    <<: *login
  loop: "{{ node_list['ontap_info']['cluster/nodes']['records'] }}"`
kindred widget
#

I am missing something here. But, what is "node1name" here?
TASK [Rename nodes] ****************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'node1name' is undefined\n\nThe error appears to be in....

grim pasture
#

items lists within "{{ }}" are variables. in this example the node1name variable is defined in a separete vars files not show in the above example.

kindred widget
#

@grim pasture Excuse my persistence!

  1. Could you please show me the contents of "variables.yml" as defined in my code?
  2. What your code should result in? from_name: "{{ item.name }}" should print out old node names, and name: "{{ node1name }}" should simulate new names that we want nodes to be renamed, right?
    `---
  • hosts: localhost
    collections:
    • netapp.ontap
      gather_facts: False
      vars_files:
    • variables.yml
      name: PRACTICE
      tasks:
    • name: Get cluster node info
      netapp.ontap.na_ontap_rest_info:
      hostname: "x.x.x.x"
      username: "admin"
      password: "netapp123"
      https: true
      validate_certs: false
      gather_subset:
      - cluster/nodes
      register: node_list
    • name: Rename nodes
      na_ontap_node:
      use_rest: never
      from_name: "{{ item.name }}"
      name: "{{ node1name }}"
      loop: "{{ node_list['ontap_info']['cluster/nodes']['records'] }}"
      `
rotund vector
#

I was working with someone on a similar problem but with SVM Let me show it here since i think it will probably be helpful

So i have the following playbook with 3 different print statements

` tasks:
- name: Get info
netapp.ontap.na_ontap_rest_info:
use_python_keys: true
use_rest: Always
gather_subset:
- vserver_info
<<: *login
register: results

- name: Print return information from the previous task
  ansible.builtin.debug:
    var: results

- name: Get info
  netapp.ontap.na_ontap_rest_info:
    use_python_keys: true
    parameters:
      uuid:
        5ab678b1-d3f2-11ed-8f2b-005056b3357c
    use_rest: Always
    gather_subset:
      - vserver_info
    <<: *login
  register: results2

- name: Print the first name on the list
  ansible.builtin.debug:
    var: results2.ontap_info.svm_svms.records[0].name

- name: Print all names on a list
  ansible.builtin.debug:
    msg: "Hest are the names: {{ item.name }}"
  with_items: "{{results.ontap_info.svm_svms.records}}"`
#

The first one is going to return everything

ok: [localhost] => { "results": { "changed": false, "failed": false, "ontap_info": { "svm_svms": { "_links": { "self": { "href": "/api/svm/svms?max_records=1024&fields=" } }, "num_records": 4, "records": [ { "_links": { "self": { "href": "/api/svm/svms/5a9021d3-d57e-11ed-8f2b-005056b3357c" } }, "name": "terraform", "uuid": "5a9021d3-d57e-11ed-8f2b-005056b3357c" }, { "_links": { "self": { "href": "/api/svm/svms/5ab678b1-d3f2-11ed-8f2b-005056b3357c" } }, "name": "nfs-test", "uuid": "5ab678b1-d3f2-11ed-8f2b-005056b3357c" }, { "_links": { "self": { "href": "/api/svm/svms/7a7eca52-92d0-11ed-8f2b-005056b3357c" } }, "name": "ansibleSVM", "uuid": "7a7eca52-92d0-11ed-8f2b-005056b3357c" } ] } } } }

#

The second one since i know only had 1 result i've hard coded it to return just the name of the first thing in records.

#

TASK [Print the first name on the list] ********************************************************************** ok: [localhost] => { "results2.ontap_info.svm_svms.records[0].name": "nfs-test" }

#

The last one i'm doing a for loop to get the name of all 4 svms

TASK [Print all names on a list] ***************************************************************************** ok: [localhost] => (item={'uuid': '5a9021d3-d57e-11ed-8f2b-005056b3357c', 'name': 'terraform', '_links': {'self': {'href': '/api/svm/svms/5a9021d3-d57e-11ed-8f2b-005056b3357c'}}}) => { "msg": "Hest are the names: terraform" } ok: [localhost] => (item={'uuid': '5ab678b1-d3f2-11ed-8f2b-005056b3357c', 'name': 'nfs-test', '_links': {'self': {'href': '/api/svm/svms/5ab678b1-d3f2-11ed-8f2b-005056b3357c'}}}) => { "msg": "Hest are the names: nfs-test" } ok: [localhost] => (item={'uuid': '7a7eca52-92d0-11ed-8f2b-005056b3357c', 'name': 'ansibleSVM', '_links': {'self': {'href': '/api/svm/svms/7a7eca52-92d0-11ed-8f2b-005056b3357c'}}}) => { "msg": "Hest are the names: ansibleSVM" } ok: [localhost] => (item={'uuid': '84770f95-92d0-11ed-8f2b-005056b3357c', 'name': 'ansibleSVM_cifs', '_links': {'self': {'href': '/api/svm/svms/84770f95-92d0-11ed-8f2b-005056b3357c'}}}) => { "msg": "Hest are the names: ansibleSVM_cifs" }