selectattr on dict not working as expected

I have a dict that looks like:

`

TASK [debug vpc_subnet_facts.subnets] *******************************************************************************************************************************************************************************
ok: [localhost] => {
“msg”: [
{
“assign_ipv6_address_on_creation”: false,
“availability_zone”: “us-west-1a”,
“available_ip_address_count”: 250,
“cidr_block”: “10.0.1.0/24”,
“default_for_az”: false,
“id”: “subnet-030ff449eaea87042”,
“ipv6_cidr_block_association_set”: ,
“map_public_ip_on_launch”: false,
“state”: “available”,
“subnet_id”: “subnet-030ff449eaea87042”,
“tags”: {
“Name”: “private-a”
},
“vpc_id”: “vpc-0a8c42654fe5f4a2b”
},
{
“assign_ipv6_address_on_creation”: false,
“availability_zone”: “us-west-1a”,
“available_ip_address_count”: 251,
“cidr_block”: “10.0.2.0/24”,
“default_for_az”: false,
“id”: “subnet-0e2db57655e833a66”,
“ipv6_cidr_block_association_set”: ,
“map_public_ip_on_launch”: false,
“state”: “available”,
“subnet_id”: “subnet-0e2db57655e833a66”,
“tags”: {
“Name”: “public-a”
},
“vpc_id”: “vpc-0a8c42654fe5f4a2b”
},
{
“assign_ipv6_address_on_creation”: false,
“availability_zone”: “us-west-1c”,
“available_ip_address_count”: 251,
“cidr_block”: “10.0.5.0/24”,
“default_for_az”: false,
“id”: “subnet-060df17f2871364d2”,
“ipv6_cidr_block_association_set”: ,
“map_public_ip_on_launch”: false,
“state”: “available”,
“subnet_id”: “subnet-060df17f2871364d2”,
“tags”: {
“Name”: “private-c”
},
“vpc_id”: “vpc-0a8c42654fe5f4a2b”
},
{
“assign_ipv6_address_on_creation”: false,
“availability_zone”: “us-west-1c”,
“available_ip_address_count”: 251,
“cidr_block”: “10.0.6.0/24”,
“default_for_az”: false,
“id”: “subnet-0dd9ab6af6ec05f83”,
“ipv6_cidr_block_association_set”: ,
“map_public_ip_on_launch”: false,
“state”: “available”,
“subnet_id”: “subnet-0dd9ab6af6ec05f83”,
“tags”: {
“Name”: “public-c”
},
“vpc_id”: “vpc-0a8c42654fe5f4a2b”
}
]
}

`

and I’m attempting to extract subnet_id for only public subnets like so:

`

  • name: set public vpc subnets
    set_fact:
    vpc_public_subnet_ids: “{{ item | selectattr(‘Name’, ‘search’, ‘public.+’) | map(attribute=‘subnet_id’) | list }}”
    loop: “{{ vpc_subnet_facts.subnets }}”

`

I’ve tried a number of ways to filter out this information but to no avail. The most common error is “Unexpected templating type error occurred on ({{ item | selectattr(‘Name’, ‘search’, ‘public.+’) | map(attribute=‘subnet_id’) | list }}): expected string or buffer”}"
I feel as if I’m making some rather basic mistake here. Any help is appreciated.

I have a dict that looks like:

    "msg": [
        {
            "assign_ipv6_address_on_creation": false,
            "availability_zone": "us-west-1a",
            "available_ip_address_count": 250,
            "cidr_block": "10.0.1.0/24",
            "default_for_az": false,
            "id": "subnet-030ff449eaea87042",
            "ipv6_cidr_block_association_set": ,
            "map_public_ip_on_launch": false,
            "state": "available",
            "subnet_id": "subnet-030ff449eaea87042",
            "tags": {
                "Name": "private-a"
            },
            "vpc_id": "vpc-0a8c42654fe5f4a2b"
        },
        {
            "assign_ipv6_address_on_creation": false,
            "availability_zone": "us-west-1a",
            "available_ip_address_count": 251,
            "cidr_block": "10.0.2.0/24",
            "default_for_az": false,
            "id": "subnet-0e2db57655e833a66",
            "ipv6_cidr_block_association_set": ,
            "map_public_ip_on_launch": false,
            "state": "available",
            "subnet_id": "subnet-0e2db57655e833a66",
            "tags": {
                "Name": "public-a"
            },
            "vpc_id": "vpc-0a8c42654fe5f4a2b"
        },

<snip />

and I'm attempting to extract subnet_id for only public subnets like so:

- name: set public vpc subnets
  set_fact:
    vpc_public_subnet_ids: "{{ item | selectattr('Name', 'search',
'public.+') | map(attribute='subnet_id') | list }}"
  loop: "{{ vpc_subnet_facts.subnets }}"

I've tried a number of ways to filter out this information but to no avail.
The most common error is *"Unexpected templating type error occurred on ({{
item | selectattr('Name', 'search', 'public.+') |
map(attribute='subnet_id') | list }}): expected string or buffer"}"*
I feel as if I'm making some rather basic mistake here. Any help is
appreciated.

selectattr only works on list not dicts.
And you don't have a Name at that level only tags.Name

So this should work

  - name: set public vpc subnets
    set_fact:
      vpc_public_subnet_ids: "{{ vpc_subnet_facts.subnets | selectattr('tags.Name', 'search', 'public.+') | map(attribute='subnet_id') | list }}"

So you where very close to find the solution.

I see what I was doing wrong. Thanks for this, your solution worked.