# Use results from facts

**URL:** <https://forum.ansible.com/t/use-results-from-facts/21205>\
**Category:** Ansible Project\
**Created:** [April 19, 2016, 2:49pm UTC](https://forum.ansible.com/t/use-results-from-facts/21205 "2016-04-19T14:49:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunser](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@lunser](https://forum.ansible.com/u/lunser)\
**Post date:** [April 19, 2016, 2:49pm UTC](https://forum.ansible.com/t/use-results-from-facts/21205/1 "2016-04-19T14:49:21Z")

</div>

Hi,

I am getting facts from the ec2\_vpc\_net\_facts module which I’m trying to grab the vpc-id from the output and use it as a variable to create a security group. This seems that it should be easy enough and I’ve read that ansible creates variables automatically but the example that I found doesn’t not seem to work here. I thought I’d post here to see if someone else has done this sort of thing.

Thanks so much,  
Ladean

---- main.yml

- name: get vpc\_id  
ec2\_vpc\_net\_facts:  
region: “{{ region }}”  
filters:  
“tag:Name”: Svpc

- debug:  
var=ansible\_ec2\_vpc\_vpc\_id

- name: Create VPC Security Groups  
ec2\_group:  
state: present  
name: “{{ env }}sshIn”  
description: allow ssh in from any  
region: “{{ region }}”  
vpc\_id: “{{ ansible\_ec2\_vpc\_vpc\_id }}”  
rules:

- proto: tcp  
cidr\_ip: x.x.x.x/x  
to\_port: 22

---- results

TASK [awsvpc : get vpc\_id] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [awsvpc : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“ansible\_ec2\_vpc.vpcs\_id”: “VARIABLE IS NOT DEFINED!”  
}

TASK [awsvpc : Create VPC Security Groups] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “‘ansible\_ec2\_vpc\_vpc\_id’ is undefined”}

---

<div class="post-metadata">

**Author:** ![lunser](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@lunser](https://forum.ansible.com/u/lunser)\
**Post date:** [April 19, 2016, 4:56pm UTC](https://forum.ansible.com/t/use-results-from-facts/21205/2 "2016-04-19T16:56:27Z")

</div>

After some fighting I found the way to capture the vpc id by registering the output. What I was missing all along in the variable string was the [0].

- name: get vpc\_id  
ec2\_vpc\_net\_facts:  
region: “{{ region }}”  
filters:  
“tag:Name”: Svpc  
register: vpcdata

- debug:  
var=vpcdata.vpcs[0].id

- name: Create VPC Security Groups  
ec2\_group:  
state: present  
name: “{{ env }}sshIn”  
description: allow ssh in from any  
region: “{{ region }}”  
vpc\_id: “{{ vpcdata.vpcs[0].id }}”  
rules:

- proto: tcp  
cidr\_ip: x.x.x.x/x  
from\_port: 22  
to\_port: 22

Thanks
