# Populate list from results (list)

**URL:** <https://forum.ansible.com/t/populate-list-from-results-list/20962>\
**Category:** Ansible Project\
**Created:** [March 22, 2016, 9:21pm UTC](https://forum.ansible.com/t/populate-list-from-results-list/20962 "2016-03-22T21:21:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [March 22, 2016, 9:21pm UTC](https://forum.ansible.com/t/populate-list-from-results-list/20962/1 "2016-03-22T21:21:56Z")

</div>

I’m using the ec2\_remote\_facts module to get a list of instances based on filters.

I want to create another list that contains the ec2\_facts.instances[0-n].private\_ip

This is the logic I’m after

- name: Set server names address for webapp proxy server  
set\_fact:  
servers[item]: “{{ ec2\_facts.instances[item].private\_ip\_address }}”  
with\_sequence: “start=0 end={{ ec2\_facts.instances|(length -1) }}”

but it doesn’t work

“msg”: “ERROR! ‘list object’ has no attribute u’0’”

I can manually set each fact

- name: Set server names address for webapp proxy server  
set\_fact:  
servers: [“{{ ec2\_facts.instances[0].private\_ip\_address }}”, “{{ ec2\_facts.instances[1].private\_ip\_address }}” ]

But this is not optimal as it doesn’t work for variable length lists.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [March 23, 2016, 3:02pm UTC](https://forum.ansible.com/t/populate-list-from-results-list/20962/2 "2016-03-23T15:02:42Z")

</div>

I think you can do this using the map filter

something like (untested)

- name: Set server names address for webapp proxy server  
set\_fact:  
servers: “{{ ec2\_facts.instances|map(‘private\_ip\_address’)|list }}”

Have a look at: [http://jinja.pocoo.org/docs/dev/templates/#map](http://jinja.pocoo.org/docs/dev/templates/#map)

HTH

Jon

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [March 24, 2016, 5:29pm UTC](https://forum.ansible.com/t/populate-list-from-results-list/20962/3 "2016-03-24T17:29:25Z")

</div>

Hi Jon,

That helped, I needed to specific attribute=‘private\_ip\_address’ for a map() arguments and was able to find some more documentation here, [http://docs.ansible.com/ansible/playbooks\_filters.html#other-useful-filters](http://docs.ansible.com/ansible/playbooks_filters.html#other-useful-filters)

I’m now trying to break out subnet ids from subnet\_facts

- name: Get subnets  
ec2\_vpc\_subnet\_facts:  
aws\_access\_key: “{{ aws\_access\_key }}”  
aws\_secret\_key: “{{ aws\_secret\_key }}”  
region: “{{ aws\_region }}”  
security\_token: “{{ security\_token }}”  
filters:  
vpc-id: “{{ returned\_vpc.vpcs[0].id }}”  
“tag:Name”: “{{ item }}”  
register: subnetDMZ\_facts  
with\_items:
- “{{ DMZ\_subnet1 }}”
- “{{ DMZ\_subnet2 }}”
- “{{ DMZ\_subnet3 }}”

I get a data structure like this

ok: [localhost] =\> {  
“subnetDMZ\_facts.results”: [  
{  
“\_ansible\_no\_log”: false,  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“aws\_access\_key”: “XXXXX”,  
“aws\_secret\_key”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“ec2\_url”: null,  
“filters”: {  
“tag:Name”: “SubDMZ1”,  
“vpc-id”: “vpc-XXXXXXXX”  
},  
“profile”: null,  
“region”: “us-west-2”,  
“security\_token”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“validate\_certs”: true  
},  
“module\_name”: “ec2\_vpc\_subnet\_facts”  
},  
“item”: “SubDMZ1”,  
“subnets”: [  
{  
“availability\_zone”: “us-west-2a”,  
“available\_ip\_address\_count”: 57,  
“cidr\_block”: “[10.201.0.0/26](http://10.201.0.0/26)”,  
“default\_for\_az”: “false”,  
“id”: “subnet-XXXXXXX”,  
“map\_public\_ip\_on\_launch”: “false”,  
“state”: “available”,  
“tags”: {  
“Name”: “SubDMZ1”  
},

“vpc\_id”: “vpc-XXXXXXX”  
}  
]  
},  
{…

Subnets is a list. I was previously manually setting each subnet\_id value manually via  
subnet\_ids: [‘{{ subnetDMZ\_facts.results[0].subnets[0].id }}’, ‘{{ subnetDMZ\_facts.results[1].subnets[0].id }}’, ‘{{ subnetDMZ\_facts.results[1].subnets[0].id }}’ ]

I tried using the subnet[0].id as the attribute  
subnets\_map: “{{ subnetDMZ\_facts.results|map(attribute=‘subnets[0].id’)|list }}”

fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “ERROR! ‘dict object’ has no attribute ‘subnets[0]’”}

I can access other vales, just not list items, e.g.  
subnets\_map: “{{ subnetDMZ\_facts.results|map(attribute=‘invocation.module\_args.aws\_access\_key’)|list }}”

TIA

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [March 24, 2016, 5:55pm UTC](https://forum.ansible.com/t/populate-list-from-results-list/20962/4 "2016-03-24T17:55:17Z")

</div>

This works

subnets\_map: “{{ subnetDMZ\_facts.results|map(attribute=‘[subnets.0.id](http://subnets.0.id)’)|list }}”
