#netapp_info , get aggregates sort by freespace
1 messages · Page 1 of 1 (latest)
Not grabbing the one with the most free space but this is what I am using:
netapp.ontap.na_ontap_rest_info:
gather_subset:
- storage/aggregates
parameters:
snaplock_type: non_snaplock
state: online
#block_storage.storage_type: ssd # Added in ONTAP 9.11
space.block_storage.physical_used_percent: "<85"
fields:
- 'space.block_storage.physical_used_percent'
register: aggr_info
- name: Debug - Aggregates found
debug:
msg: "{{ aggr_info }}"
verbosity: 2
- name: Creating list of eligible aggregates
set_fact:
aggr_list: "{{ aggr_list + [ item.name ] }}"
with_items: "{{ aggr_info.ontap_info['storage/aggregates'].records }}"
- name: Debug - Aggregate list
debug:
msg: "{{ aggr_list }}"
verbosity: 1
- name: Select random aggregate from list
set_fact:
aggr_name: "{{ item }}"
with_random_choice: "{{ aggr_list }}"```
Then using "aggr_name" in the "volume create" task, this way it fails if there are no eligible aggregates.
Would love to see a nicer way of doing it though.
I have a role, that does that, here is what those tasks would look like in a playbook. Just get the JSON from ontap_rest_info, extract the values you need and sort the new list by the available field
- name: 'get least full aggregate'
hosts: all
connection: local
gather_facts: false
collections:
- netapp.ontap
vars:
netapp_hostname: "{{ inventory_hostname }}"
tasks:
- name: 'get ontap info'
na_ontap_rest_info:
hostname: "{{ netapp_hostname }}"
username: "{{ netapp_username }}"
password: "{{ netapp_password }}"
https: true
validate_certs: false
use_python_keys: true
gather_subset:
- storage/aggregates
fields:
- 'name'
- 'node.name'
- 'space.block_storage.size'
- 'space.block_storage.used'
- 'space.block_storage.available'
register: aggregate_info
- name: 'create clean list of dicts from aggregate json'
set_fact:
aggregate_info: "{{ aggregate_info| json_query(jmespath_expression) }}"
vars:
jmespath_expression: "ontap_info.storage_aggregates.records[*].{ name: name, space_total: space.block_storage.size, space_used: space.block_storage.used, space_available: space.block_storage.available }"
- name: 'sort the list by space_available'
set_fact:
aggregate_info: "{{ aggregate_info | sort(attribute='space_available', reverse=true) }}"
- name: 'write the dict with the most free space to least_full_aggregate'
set_fact:
least_full_aggregate: aggregate_info[0]
you can also do maths afterwards to check wether the volume you want to provision would still fit on that aggregate and at what percentage the aggregate would be afterwards. That's why I included the size, used fields in the dict
thank you @clear kiln and @heavy moat for feedback!
awesome work - @heavy moat your playbook fits perfect for my use case and it is working fine. thank you!
@clear kiln @heavy moat @icy hatch
I got errors on "aggr_list" variable is not defined. Can you please share the line on how to define “aggr_list” variable in @clear kiln playbook? Thank you!
Anybody please help me out on the question above?
From this playbook #1103301527650717866 message
It looks like the first play is to rest info and it registers aggr_info
And the second play uses that aggr_info.
I would have to see what your playbook looks like to see what causing the issue for you
@primal path Thanks for your response.
The error is not on "aggr_info", but "aggr_list", as shown below, the variable was not defined before used, and caused errors. My playbook is the same. Should we define it before use? How and in where?
- name: Creating list of eligible aggregates set_fact: aggr_list: "{{ aggr_list + [ item.name ] }}" with_items: "{{ aggr_info.ontap_info['storage/aggregates'].records }}"
TASK [Creating list of eligible aggregates] *************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'aggr_list' is undefined\n\nThe error appears to be in './aggr_freespace1.yml': line 32, column 7, …
aggr_list is not defined before, but we can have a default value if its not already defined
name: Creating list of eligible aggregates set_fact: aggr_list: "{{ aggr_list | default([]) + [ item.name ] }}" with_items: "{{ aggr_info.ontap_info['storage/aggregates'].records }}"
you can find more info filters
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#providing-default-values
@unique thorn It works, Thanks!
Just checked the link, and understand how "{{ some_variable | default(5) }}" works, in general.
But, what I don't understand about is that what variable "[]" inside "default([])" was actually defined? and I don't see a value as default in it. ?
Sorry, forgot to mention that I define "aggr_list" as empty at the beginning of the playbook:
aggr_list: []```
But Mohans solution looks much better. 😃
aggr_list: [] is a list type or dict type?
Not sure why you would need to know that but it is a list.
{} is dict and [] is list..