map()...What am I doing wrong here?

I’ve been trying to extract the values for subnet_id from the dictionary subnet_facts but keep getting undefined (3 times instead of the values) as a return value. I should be getting [subnet-fd6bfa95, subnet-7c8bb031, subnet-7c8bb031]

Any ideas?

Below is the local host testbed playbook i’ve been using trying to get this jinja filter to work.

I’ve been trying to extract the values for subnet_id from the dictionary subnet_facts but keep getting undefined (3 times instead of the values) as a return value. I should be getting [subnet-fd6bfa95, subnet-7c8bb031, subnet-7c8bb031]

Any ideas?

Below is the local host testbed playbook i’ve been using trying to get this jinja filter to work.

‘subnets’ is a list (observe the .)

“subnets”: [
{
“assign_ipv6_address_on_creation”: false,
“availability_zone”: “us-east-2b”,
“available_ip_address_count”: 251,
“cidr_block”: “10.0.2.0/24”,
“default_for_az”: false,
“id”: “subnet-6ad01910”,
“ipv6_cidr_block_association_set”: ,
“map_public_ip_on_launch”: true,
“state”: “available”,
“subnet_id”: “subnet-6ad01910”,
“tags”: {
“DeleteTag”: “test-g...@xxxx.com”,
“Name”: “test-subnet-us-east-2b-public”,
“SubnetType”: “public”
},
“vpc_id”: “vpc-1227b97a”
}
]

var: “{{ subnet_facts | selectattr(‘results.subnets.id’) | list }}”

Here, you appear to be trying to get the ‘id’ attribute of ‘subnets’. It doesn’t have one, because it’s a list.

`
var: “subnet_facts.results | map(attribute=‘subnets’) | flatten | map(attribute=‘id’) | list”

`

(The flatten filter is new in Ansible 2.5; on older versions you’d need to use the flatten lookup.)

Thank you so much! This worked perfectly!

ok: [localhost] => {

“subnet_facts.results | map(attribute=‘subnets’) | flatten | map(attribute=‘id’) | list”: [

“subnet-fd6bfa95”,

“subnet-6ad01910”,

“subnet-7c8bb031”

]

}