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

javed_khan_siddque
Thu, 03 Oct 2024 12:35:41 +0000
Get Help

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

9 posts - 4 participants