This should be able to be done with filters
required_space being how much you need
"{{ info.datastores | selectattr('summary.freeSpace', 'gt' , required_space) | map(attribute='name') | first }}"
That should select the first one it finds that is large enough.
You could also throw in a sort to get the datastore with the most available space.
"{{ info.datastores | sort(attribute='summary.freeSpace', reverse=true) | selectattr('summary.freeSpace', 'gt' , required_space) | map(attribute='name') | first }}"
I ran a test against one of our datacenters:
- name: Name
hosts: thathost
gather_facts: false
tasks:
- name: Retrieve information on free space in each datastore
environment: "{{ vcenter_env }}"
community.vmware.vmware_datastore_info:
schema: vsphere
properties:
- name
- summary
delegate_to: localhost
register: info
- name: show total length
ansible.builtin.debug:
var: info.datastores | length
- name: unsorted first
ansible.builtin.debug:
var: output
vars:
output:
candidates: "{{ info.datastores | selectattr('summary.freeSpace', 'gt', 9999999999999 ) | map(attribute='summary.freeSpace') }}"
first_num: "{{ info.datastores | selectattr('summary.freeSpace', 'gt', 9999999999999 ) | map(attribute='summary.freeSpace') | first }}"
first_name: "{{ info.datastores | selectattr('summary.freeSpace', 'gt', 9999999999999 ) | map(attribute='name') | first }}"
- name: sorted first
ansible.builtin.debug:
var: output
vars:
output:
candidates: "{{ info.datastores | selectattr('summary.freeSpace', 'gt', 9999999999999 ) | sort(attribute='summary.freeSpace', reverse=true) | map(attribute='summary.freeSpace') }}"
first_num: "{{ info.datastores | selectattr('summary.freeSpace', 'gt', 9999999999999 ) | sort(attribute='summary.freeSpace', reverse=true) | map(attribute='summary.freeSpace') | first }}"
first_name: "{{ info.datastores | selectattr('summary.freeSpace', 'gt', 9999999999999 ) | sort(attribute='summary.freeSpace', reverse=true) | map(attribute='name') | first }}"
And output:
PLAY [Name] *********************************************************************************************************************************************************************************************************************************************
Saturday 25 May 2024 11:51:14 -0400 (0:00:00.019) 0:00:00.019 **********
TASK [Retrieve information on free space in each datastore] *********************************************************************************************************************************************************************************************
ok: [thathost -> localhost]
Saturday 25 May 2024 11:51:22 -0400 (0:00:07.445) 0:00:07.465 **********
TASK [show total length] ********************************************************************************************************************************************************************************************************************************
ok: [thathost] =>
info.datastores | length: '56'
Saturday 25 May 2024 11:51:22 -0400 (0:00:00.108) 0:00:07.573 **********
TASK [unsorted first]
ok: [thathost] =>
**output:
candidates:
- 28662707519488
- 18981516738560
- 31680767197184
- 15031020290048
- 10736076062720
- 57689932562432
first_name: AKT_500_UGN_DR_MMM
first_num: '28662707519488'
**
Saturday 25 May 2024 11:51:22 -0400 (0:00:00.342) 0:00:07.915 **********
TASK [sorted first]
ok: [thathost] =>
**output:
candidates:
- 57689932562432
- 31680767197184
- 28662707519488
- 18981516738560
- 15031020290048
- 10736076062720
first_name: KTR_DR_FGND
first_num: '57689932562432'
**
PLAY RECAP
thathost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Playbook run took 0 days, 0 hours, 0 minutes, 8 seconds
Saturday 25 May 2024 11:51:22 -0400 (0:00:00.229) 0:00:08.145 **********
===============================================================================
You can see that it filtered out 50 of our datastores based on size (I hope I never deploy any one thing that big). And you can see the differences between sorting.
That should allow you to just specify the parameter for deployment rather than looping.