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

36 views
Skip to first unread message

Gregory Mirsky

unread,
Apr 22, 2018, 7:30:15 PM4/22/18
to Ansible Project
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.


---
- hosts: local
  vars:
    "subnet_facts": {
                      "changed": false,
                      "msg": "All items completed",
                      "results": [
                          {
                              "_ansible_ignore_errors": null,
                              "_ansible_item_result": true,
                              "_ansible_no_log": false,
                              "_ansible_parsed": true,
                              "changed": false,
                              "failed": false,
                              "invocation": {
                                  "module_args": {
                                      "aws_access_key": "XXXXXXXXXXXXXXXXXXXXX",
                                      "aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                                      "ec2_url": null,
                                      "filters": {
                                          "tag:Name": "test-subnet-us-east-2a-public",
                                          "vpc-id": "vpc-1227b97a"
                                      },
                                      "profile": null,
                                      "region": "us-east-2",
                                      "security_token": null,
                                      "subnet_ids": [],
                                      "validate_certs": true
                                  }
                              },
                              "item": "test-subnet-us-east-2a-public",
                              "subnets": [
                                  {
                                      "assign_ipv6_address_on_creation": false,
                                      "availability_zone": "us-east-2a",
                                      "available_ip_address_count": 251,
                                      "cidr_block": "10.0.1.0/24",
                                      "default_for_az": false,
                                      "id": "subnet-fd6bfa95",
                                      "ipv6_cidr_block_association_set": [],
                                      "map_public_ip_on_launch": true,
                                      "state": "available",
                                      "subnet_id": "subnet-fd6bfa95",
                                      "tags": {
                                          "DeleteTag": "test-g...@xxxx.com",
                                          "Name": "test-subnet-us-east-2a-public",
                                          "SubnetType": "public"
                                      },
                                      "vpc_id": "vpc-1227b97a"
                                  }
                              ]
                          },
                          {
                              "_ansible_ignore_errors": null,
                              "_ansible_item_result": true,
                              "_ansible_no_log": false,
                              "_ansible_parsed": true,
                              "changed": false,
                              "failed": false,
                              "invocation": {
                                  "module_args": {
                                      "aws_access_key": "XXXXXXXXXXXXXXXXXXXXX",
                                      "aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                                      "ec2_url": null,
                                      "filters": {
                                          "tag:Name": "test-subnet-us-east-2b-public",
                                          "vpc-id": "vpc-1227b97a"
                                      },
                                      "profile": null,
                                      "region": "us-east-2",
                                      "security_token": null,
                                      "subnet_ids": [],
                                      "validate_certs": true
                                  }
                              },
                              "item": "test-subnet-us-east-2b-public",
                              "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"
                                  }
                              ]
                          },
                          {
                              "_ansible_ignore_errors": null,
                              "_ansible_item_result": true,
                              "_ansible_no_log": false,
                              "_ansible_parsed": true,
                              "changed": false,
                              "failed": false,
                              "invocation": {
                                  "module_args": {
                                      "aws_access_key": "XXXXXXXXXXXXXXXXXXXXX",
                                      "aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                                      "ec2_url": null,
                                      "filters": {
                                          "tag:Name": "test-subnet-us-east-2c-public",
                                          "vpc-id": "vpc-1227b97a"
                                      },
                                      "profile": null,
                                      "region": "us-east-2",
                                      "security_token": null,
                                      "subnet_ids": [],
                                      "validate_certs": true
                                  }
                              },
                              "item": "test-subnet-us-east-2c-public",
                              "subnets": [
                                  {
                                      "assign_ipv6_address_on_creation": false,
                                      "availability_zone": "us-east-2c",
                                      "available_ip_address_count": 251,
                                      "cidr_block": "10.0.3.0/24",
                                      "default_for_az": false,
                                      "id": "subnet-7c8bb031",
                                      "ipv6_cidr_block_association_set": [],
                                      "map_public_ip_on_launch": true,
                                      "state": "available",
                                      "subnet_id": "subnet-7c8bb031",
                                      "tags": {
                                          "DeleteTag": "test-g...@xxxx.com",
                                          "Name": "test-subnet-us-east-2c-public",
                                          "SubnetType": "public"
                                      },
                                      "vpc_id": "vpc-1227b97a"
                                  }
                              ]
                          }
                      ]
                  }
  tasks:
  - name: Debug for subnet_facts...
    debug:
      var: subnet_facts
  - name: Debug for subnet_facts.results.subnets.subnet_id parse
    debug:
      var: "{{ subnet_facts | selectattr('results.subnets.id') | list }}"
      #var: "{{ subnet_facts.results|map(attribute='subnets')|map(attribute='subnet_id')|list }}"
      #var: "{{ subnet_facts.results|map(attribute='subnets.subnet_id')| list }}"
      #var: "{{ subnet_facts.results | selectattr('subnets') |  map(attribute='subnet_id') | list }}"
      #var: "{{ subnet_facts|json_query('results[*].subnets.subnet_id') }}"
  - meta: end_play

flowerysong

unread,
Apr 22, 2018, 10:25:37 PM4/22/18
to Ansible Project


On Sunday, April 22, 2018 at 7:30:15 PM UTC-4, Gregory Mirsky wrote:
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.)

Gregory Mirsky

unread,
Apr 23, 2018, 10:10:09 AM4/23/18
to ansible...@googlegroups.com
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"

    ]

}



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/-Yu9baw5cCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1e8c11c4-622e-42f7-8c91-913a0666ec65%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Gregory N. Mirsky
gmi...@gmail.com
Reply all
Reply to author
Forward
0 new messages