# Need To Create List Of Dictionary With New Field 'device\_id' Added

**URL:** <https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655>\
**Category:** Get Help\
**Tags:** loop\
**Created:** [October 3, 2024, 12:35pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655 "2024-10-03T12:35:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [October 3, 2024, 12:35pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/1 "2024-10-03T12:35:41Z")

</div>

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

```auto
---
- 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

```

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [October 3, 2024, 5:24pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/2 "2024-10-03T17:24:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [October 4, 2024, 12:41pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/3 "2024-10-04T12:41:21Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [October 4, 2024, 4:36pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/4 "2024-10-04T16:36:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [October 4, 2024, 5:04pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/5 "2024-10-04T17:04:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [October 4, 2024, 6:21pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/6 "2024-10-04T18:21:52Z")

</div>

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

```auto
- 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

```

---

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [November 25, 2024, 12:21pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/7 "2024-11-25T12:21:34Z")

</div>

Apology For late response  
This is probably the best solution.

* * *

- name: “Mapping SAN Mount Drive With Source/Old Device ID And Target/new Device ID”  
hosts: localhost  
gather\_facts: false  
vars:

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 25, 2024, 12:52pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/8 "2024-11-25T12:52:34Z")

</div>

Could you enclose the YAML in three backticks to improve the formatting? See for example the [GitHub guide](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 25, 2024, 12:52pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655/9 "2024-12-25T12:52:48Z")

</div>

Closed on December 25, 2024, 12:52pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
