List/Dictionary - Combine elements based on item value

20 views
Skip to first unread message

Adam McGill

unread,
Jun 29, 2020, 4:38:26 PM6/29/20
to Ansible Project
Hello,
Basically want to add the items of net_interface into aggregate_info or create new list/dictionary based on key value of node_name.  Looking for assistance on how i can achieve this please and thank you.
Outcome of what want it listed below.

list called: aggregate_info
{
  "name": "agg_Data1_Cluster01",
   "node_name": "TEST_NA_CLUSTER_01-01",
   "percent_used_capacity": "46",
   "size_available": 8141263818752,
   "size_avaliable_80%": 5122753242726,
   "size_total": 15092552880128,
   "size_total_80%": 12074042304102,
   "size_used": 6951289061376
},
{
   "name": "agg_Data1_Cluster02",
   "node_name": "TEST_NA_CLUSTER_01-02",
   "percent_used_capacity": "34",
   "size_available": 5116275568640,
   "size_avaliable_80%": 3561215869747,
   "size_total": 7775298494464,
   "size_total_80%": 6220238795571,
   "size_used": 2659022925824
}

list called: net_interface
{
    "address": "XX.XXX.X.XXX",
    "address4oct": "224",
    "data_protcol": "none",
    "failover_group": "Management",
    "node_name": "TEST_NA_CLUSTER_01-01",
    "role": "node_mgmt",
    "vserver": "TEST_NA_CLUSTER_01"
},
{
    "address": "XX.XXX.X.XXX",
    "address4oct": "226",
    "data_protcol": "none",
    "failover_group": "Management",
    "node_name": "IQ-REG_NA_CLUSTER_01-02",
    "role": "node_mgmt",
    "vserver": "TEST_NA_CLUSTER_01"
}


Out Come want:
Dictionary called: aggregate_info (original) or new name
{
  "name": "agg_Data1_Cluster01",
   "node_name": "TEST_NA_CLUSTER_01-01",
   "percent_used_capacity": "46",
   "size_available": 8141263818752,
   "size_avaliable_80%": 5122753242726,
   "size_total": 15092552880128,
   "size_total_80%": 12074042304102,
   "size_used": 6951289061376
   "address": "XX.XXX.X.XXX",
   "address4oct": "224",
   "data_protcol": "none",
   "failover_group": "Management",
   "role": "node_mgmt",
   "vserver": "TEST_NA_CLUSTER_01"
},
{
   "name": "agg_Data1_Cluster02",
   "node_name": "TEST_NA_CLUSTER_01-02",
   "percent_used_capacity": "34",
   "size_available": 5116275568640,
   "size_avaliable_80%": 3561215869747,
   "size_total": 7775298494464,
   "size_total_80%": 6220238795571,
   "size_used": 2659022925824
   "address": "XX.XXX.X.XXX",
   "address4oct": "226",
   "data_protcol": "none",
   "failover_group": "Management",
   "role": "node_mgmt",
   "vserver": "TEST_NA_CLUSTER_01"
}

Adam McGill

unread,
Jun 29, 2020, 6:32:49 PM6/29/20
to Ansible Project
Adding further to above, i know i can get the value i want by doing this.

- debug:
        msg: "{% for address in net_interface if address.node_name == 'IQ-REG_NA_CLUSTER_01-01' %}{{ address.address }}{% endfor %}"

Anyway to add that logic when creating the aggregate_info list. for example, but when trying to do this i get a template error.  Don't know if its possible to do or i am missing something with my syntax.

- name: Get_Aggregate_Info
      #no_log: True
      set_fact:
        aggregate_info: "{{ aggregate_info | 
          d([]) + [ {
          'name': item.value.aggregate_name, 
          'size_available': item.value.aggr_space_attributes.size_available | int,
          'size_avaliable_80%': (((item.value.aggr_space_attributes.size_total | int) * 0.8) - (item.value.aggr_space_attributes.size_used | int)) | round | int,
          'size_total': item.value.aggr_space_attributes.size_total | int,
          'size_total_80%': ((item.value.aggr_space_attributes.size_total | int) * 0.8 ) | round | int,
          'size_used': item.value.aggr_space_attributes.size_used | int,
          'node_name': item.value.nodes.node_name,
          'percent_used_capacity': item.value.aggr_space_attributes.percent_used_capacity
          'test': {% for address in net_interface if address.node_name == item.value.nodes.node_name %}{{ address.address }}{% endfor %}
          } ] }}" 
      with_items: "{{ netapp_info.ontap_info.aggregate_info | dict2items }}"
      when:
        - ('agg_' in item.value.aggregate_name)

Adam McGill

unread,
Jun 29, 2020, 11:24:28 PM6/29/20
to Ansible Project
Figured out how to merge the lists based on values in each list.  

namemerge lists
      set_fact:
        merged_list"{{ merged_list|default([]) + [{ 
          'name': item[0].name, 
          'size_available': item[0].size_available,
          'size_avaliable_80': item[0].size_avaliable_80,
          'size_total': item[0].size_total,
          'size_total_80': item[0].size_total_80,
          'size_used': item[0].size_used,
          'node_name': item[0].node_name,
          'percent_used_capacity': item[0].percent_used_capacity,
          'current_node': item[1].current_node,
          'address': item[1].address,
          'address4oct': item[1].address4oct,
          'failover_group': item[1].failover_group,
          'data_protcol': item[1].data_protcol| default(''),
          'role': item[1].role,
          'vserver': item[1].vserver
          }] }}"
      when"item[0].node_name == item[1].current_node"
      loop"{{ query('nested', aggregate_info, net_interface) }}"
Reply all
Reply to author
Forward
0 new messages