I thought there might be a slightly simpler way to achieve this using community.general.counter.
This is rough but:
tasks:
- name: Append to our count list
ansible.builtin.set_fact:
storage_counter: '{{ storage_counter|default([]) + [ "Servername:" + item.servername + "," + item.size + "GB"] }}'
loop: '{{ storage_details_test_capacity }}'
- name: Debug data structure
ansible.builtin.debug:
msg: '{{ storage_counter | community.general.counter }}'
Results in:
TASK [Append to our count list] ******************************************************************************************************************************
ok: [localhost] => (item={'servername': 'server1', 'cap_unit': 'GB', 'size': '10'})
ok: [localhost] => (item={'servername': 'server1', 'cap_unit': 'GB', 'size': '11'})
ok: [localhost] => (item={'servername': 'server1', 'cap_unit': 'GB', 'size': '11'})
ok: [localhost] => (item={'cap_unit': 'GB', 'servername': 'server2', 'size': '12'})
ok: [localhost] => (item={'cap_unit': 'GB', 'servername': 'server2', 'size': '13'})
ok: [localhost] => (item={'cap_unit': 'GB', 'servername': '', 'size': '14'})
ok: [localhost] => (item={'cap_unit': 'GB', 'servername': '', 'size': '14'})
ok: [localhost] => (item={'cap_unit': 'GB', 'servername': '', 'size': '14'})
ok: [localhost] => (item={'cap_unit': 'GB', 'servername': '', 'size': '15'})
TASK [Debug data structure] **********************************************************************************************************************************
ok: [localhost] => {
"msg": {
"Servername:,14GB": 3,
"Servername:,15GB": 1,
"Servername:server1,10GB": 1,
"Servername:server1,11GB": 2,
"Servername:server2,12GB": 1,
"Servername:server2,13GB": 1
}
}
Which with some additional refinement, might the same thing as Todd's method but slightly less mind-bending? :D