selectattr on dict not working as expected

29 views
Skip to first unread message

jus...@fubo.tv

unread,
Aug 23, 2018, 3:03:15 PM8/23/18
to Ansible Project
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. 

Kai Stian Olstad

unread,
Aug 23, 2018, 4:53:14 PM8/23/18
to ansible...@googlegroups.com
On Thursday, 23 August 2018 21.03.14 CEST jus...@fubo.tv wrote:
> I have a dict that looks like:
>
<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.

--
Kai Stian Olstad


Justin Potts

unread,
Aug 23, 2018, 5:05:56 PM8/23/18
to ansible...@googlegroups.com
I see what I was doing wrong. Thanks for this, your solution worked.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3103612.W4BkT6hQ3H%40x1.
For more options, visit https://groups.google.com/d/optout.


--
Justin Potts | Senior Software Engineer
t: 347.932.5082 | jus...@fubo.tv
Reply all
Reply to author
Forward
0 new messages