help with with_items

What I want is the separate lists, like this:

First time through loop, item should be this:

{
“AllocationId”: “eipalloc-09644c10a5aac6a39”,
“SubnetId”: “subnet-c1580ba5”

}

Second time through loop, item should be this:

{
“AllocationId”: “eipalloc-09644c10a5aac6a39”,
“SubnetId”: “subnet-c1580ba5”
}

What I get is the name of the variable! As shown below. I get the same effect when I use e.g. set_facts.

Why? And more importantly, how can I write a loop that will allow me to access e.g. item.SubnetID?

Regards, K.

Hello Karl,

Can you try like this?

  • hosts: localhost

vars:
nlb_subnet_mappings:

  • SubnetId: fred
    AllocationId: 100
  • SubnetId: mary
    AllocationId: 200

tasks:

  • debug:
    var: item
    with_items: “{{ nlb_subnet_mappings }}”

I hope this is these results you are looking for :

PLAY [localhost] **********************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] **************************************************************************************************************************************************************************************************
ok: [localhost] => (item={u’SubnetId’: u’fred’, u’AllocationId’: 100}) => {
“item”: {
“AllocationId”: 100,
“SubnetId”: “fred”
}
}
ok: [localhost] => (item={u’SubnetId’: u’mary’, u’AllocationId’: 200}) => {
“item”: {
“AllocationId”: 200,
“SubnetId”: “mary”
}
}

PLAY RECAP ****************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

Best Regards

Thanks!