Need To Create List Of Dictionary With New Field 'device_id' Added

I have my code written like below .
But i am not able to get proper List of dictionary.

---
- name: Create final list by adding device_id to san_mount_list without exceeding size_gb
  hosts: localhost
  gather_facts: false
  vars:
    my_remain_space: 0
    my_hard_disk_converted_to_GB:
      - device_id: 2
        size: 32212254725
        size_gb: 30
      - device_id: 3
        size: 32212254724
        size_gb: 30

    san_mount_list_converted_to_GB:
      - no_of_vols: 1
        cap_unit: 'BYTES'
        size_gb: 9.74
        size: 10475277143
        mount_point: G

      - no_of_vols: 1
        cap_unit: 'BYTES'
        size_gb: 29.85
        size: 32212254723
        mount_point: H

      - no_of_vols: 1
        cap_unit: 'BYTES'
        size_gb: 9.75
        size: 10475277144
        mount_point: F

      - no_of_vols: 1
        cap_unit: 'BYTES'
        size_gb: 9.78
        size: 10475277145
        mount_point: H


  tasks:
    - name: "Convert 'san_mount_list_converted_to_GB' With Sorted To 'size_gb'"
      ansible.builtin.set_fact:
        my_hard_disk_converted_to_GB_sorted_decending: "{{ my_hard_disk_converted_to_GB | sort(attribute='size') }}"
        san_mount_list_converted_to_GB_sorted_ascending: "{{ san_mount_list_converted_to_GB | sort(attribute='size_gb') }}"




    - name: "Set Fact 'my_final_list'"
      ansible.builtin.set_fact:
        final_full_lun_details: "{{ final_full_lun_details | default([]) + [{ 'no_of_vols': item.1.no_of_vols, 'cap_unit': item.1.cap_unit, 'size': item.1.size, 'size_gb': item.1.size_gb, 'mount_point': item.1.mount_point, 'device_id': item.0.device_id }] }}"
        my_device_id: "{{ my_device_id | default([]) + [item.0.device_id] }}"
        my_remain_space: "{{ my_remain_space | int - item.1.size_gb | int | round(2) }}"
      when:
        - item.0.size_gb | int | round(2) >= item.1.size_gb | int | round(2)
        # - item.0.device_id not in my_device_id | default([])
        - my_remain_space | int > item.1.size_gb | int | round(2)
      with_nested:
        - "{{ my_hard_disk_converted_to_GB_sorted_decending }}"
        - "{{ san_mount_list_converted_to_GB_sorted_ascending }}"
      vars:
        my_disk_size: "{{ item.0.size_gb | int | round(2) }}"
        my_remain_space: "{{ my_disk_size }}"


    - name: "Print my_final_list"
      ansible.builtin.debug:
        var: final_full_lun_details

I have 2 list

my_hard_disk_converted_to_GB:
  - device_id: 2
    size: 32212254725
    size_gb: 30
  - device_id: 3
    size: 32212254724
    size_gb: 30


san_mount_list_converted_to_GB:
  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 29.85
    size: 32212254723
    mount_point: H

  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 9.75
    size: 10475277144
    mount_point: F

  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 9.78
    size: 10475277145
    mount_point: H

  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 9.74
    size: 10475277143
    mount_point: G

I want to create a final_list
which will add device_id on san_mount_list_converted_to_GB and doesn’t cross size_gb of my_hard_disk_converted_to_GB.

san_mount_list_converted_to_GB:
  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 29.85
    size: 32212254723
    mount_point: H
    device_id: 2

  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 9.75
    size: 10475277144
    mount_point: F
    device_id: 3

  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 9.78
    size: 10475277145
    mount_point: H
    device_id: 3

  - no_of_vols: 1
    cap_unit: 'BYTES'
    size_gb: 9.74
    size: 10475277143
    mount_point: G
    device_id: 3

What is the relationship between items in the two lists? Put another way, how are you supposed to know what device_id each item in san_mount_list_converted_to_GB gets?

my_hard_disk_converted_to_GB:
- device_id: 2
size: 32212254725
size_gb: 50
- device_id: 3
size: 32212254724
size_gb: 50
- device_id: 4
size: 32212254723
size_gb: 50

san_mount_list_converted_to_GB:
- no_of_vols: 1
cap_unit: ‘BYTES’
size_gb: 49.84
size: 50475274245
mount_point: G
device_id: 12
- no_of_vols: 1
cap_unit: ‘BYTES’
size_gb: 9.84
size: 10475274245
mount_point: G
device_id: 13
- no_of_vols: 1
cap_unit: ‘BYTES’
size_gb: 39.85
size: 404752742465
mount_point: F
device_id: 13
- no_of_vols: 1
cap_unit: ‘BYTES’
size_gb: 9.87
size: 10475274240
mount_point: F
device_id: 14
- no_of_vols: 1
cap_unit: ‘BYTES’
size_gb: 19.89
size: 11932554240
mount_point: H
device_id: 14
- no_of_vols: 1
cap_unit: ‘BYTES’
size_gb: 19.92
size: 11932554240
mount_point: I
device_id: 14

This is new 2 list I have now … It is having only size sum match … where device_id is different.
So 2 supposed to be replaced 12
So 3 supposed to be replaced 13
So 4 supposed to be replaced 14

Im afraid I still dont understand the relationship between the two lists. I dont see any matching sizes or size_gbs in the two lists

So 2 supposed to be replaced 12
So 3 supposed to be replaced 13
So 4 supposed to be replaced 14

I dont understand this either. Are you saying device_id 2 should be replaced with device_id: 12? Or the second item in the first list should be replaced by the 12th item in the second list?

server 'A" was having 3 LUN DIsk [with ID 12 and 13 and 14] and was having drives created over there. [List : san_mount_list_converted_to_GB].

I got server ‘B’ which is having same count of disk [with ID 2 and 3 and 4]
I want to create same drive over server ‘B’. [List :my_hard_disk_converted_to_GB].

Since Server ‘B’ don’t have 12,13,14 I want them to be replaced by 2,3,4 respectively.

Hope this helps. If you wanted to specify your own device ID mapping, the playbook could be simpler

- hosts: localhost
  gather_facts: false
  vars_files:
    - my_hard_disk_converted_to_GB.yml
    - san_mount_list_converted_to_GB.yml
  tasks:
    - name: Get Unique Device IDs Lists
      ansible.builtin.set_fact:
        my_hard_disk_converted_to_GB_device_ids: >-
          {{ my_hard_disk_converted_to_GB | map(attribute='device_id') | unique }}
        san_mount_list_converted_to_GB_device_ids: >-
          {{ san_mount_list_converted_to_GB | map(attribute='device_id') | unique }}
    - name: Create New san_mount_list_converted_to_GB
      ansible.builtin.set_fact:
        new_san_mount_list_converted_to_GB: >-
          {{ new_san_mount_list_converted_to_GB | default([]) +
          [item | combine({'device_id': _device_id_map[item['device_id']]})] }}
      loop: "{{ san_mount_list_converted_to_GB }}"
      vars:
        _device_id_map: >-
          {{ my_hard_disk_converted_to_GB_device_ids | zip(san_mount_list_converted_to_GB_device_ids) |
          map('reverse') | community.general.dict }}
    - name: Print New List
      ansible.builtin.debug:
        var: new_san_mount_list_converted_to_GB