# Is there anyway to consolidate these set\_fact tasks?

**URL:** <https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888>\
**Category:** Ansible Project\
**Created:** [March 16, 2016, 2:16pm UTC](https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888 "2016-03-16T14:16:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Baydoun](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Michael\_Baydoun](https://forum.ansible.com/u/Michael_Baydoun)\
**Post date:** [March 16, 2016, 2:16pm UTC](https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888/1 "2016-03-16T14:16:53Z")

</div>

- name: create all vpc’s for all accounts and regions  
ec2\_vpc:  
state: “{{ item.internet\_gateway | default(‘no’) }}”  
region: “{{ item.region }}”  
state: “{{ item.state | default(‘present’) }}”  
cidr\_block: “{{ item.cidr\_block }}”  
resource\_tags: “{{ item.resource\_tags }}”  
subnets: “{{ item.subnets }}”  
wait: yes  
when: item.controlEnvironment == env  
with\_items: “{{ vpcList }}”  
register: vpcOut

- set\_fact:  
mainVpcId: “{{ item.vpc\_id }}”  
with\_items: vpcOut.results  
when:

- not item | skipped

- item.item.resource\_tags.WhichRegion == ‘mainRegion’

- set\_fact:  
mainSubnet0: “{{ item.subnets.0.id }}”  
with\_items: vpcOut.results  
when:

- not item | skipped

- item.item.resource\_tags.WhichRegion == ‘mainRegion’

- set\_fact:  
mainSubnet1: “{{ item.subnets.1.id }}”  
with\_items: vpcOut.results  
when:

- not item | skipped

- item.item.resource\_tags.WhichRegion == ‘mainRegion’

- set\_fact:  
mainSubnet2: “{{ item.subnets.2.id }}”  
with\_items: vpcOut.results  
when:

- not item | skipped

- item.item.resource\_tags.WhichRegion == ‘mainRegion’

---

<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 16, 2016, 4:39pm UTC](https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888/2 "2016-03-16T16:39:10Z")

</div>

Have you looked at the Ansible 2.0 modules for deploying a VPC and subnets? It may simplify the setting of facts. I don’t have time to give you an example at the moment but have found the new modules a lot easier to use.

[http://docs.ansible.com/ansible/ec2\_vpc\_net\_module.html](http://docs.ansible.com/ansible/ec2_vpc_net_module.html)

---

<div class="post-metadata">

**Author:** ![Michael\_Baydoun](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Michael\_Baydoun](https://forum.ansible.com/u/Michael_Baydoun)\
**Post date:** [March 16, 2016, 5:14pm UTC](https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888/3 "2016-03-16T17:14:47Z")

</div>

Wouldn’t I still need to register the output and extract the vpc\_id for use in subsequent subnet, security group, and other tasks.

---

<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 16, 2016, 6:47pm UTC](https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888/4 "2016-03-16T18:47:10Z")

</div>

Yes, but it is also possible to lookup the VPC id. This just gets the default, based on the filters, VPC id.

# Get the vpc id

- name: Get the default VPCs details  
ec2\_vpc\_net\_facts:  
aws\_access\_key: “{{ aws\_access\_key }}”  
aws\_secret\_key: “{{ aws\_secret\_key }}”  
region: “{{ aws\_region }}”  
filters:  
is\_default: “true”  
register: returned\_vpc

- name: The VPC id  
debug:  
var: returned\_vpc.vpcs[0].id

---

<div class="post-metadata">

**Author:** ![Michael\_Baydoun](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Michael\_Baydoun](https://forum.ansible.com/u/Michael_Baydoun)\
**Post date:** [March 17, 2016, 2:11pm UTC](https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888/5 "2016-03-17T14:11:17Z")

</div>

I previously tried ec2\_vpc\_net\_facts, but it’s not available until version 2.1 (not release yet).

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 17, 2016, 2:48pm UTC](https://forum.ansible.com/t/is-there-anyway-to-consolidate-these-set-fact-tasks/20888/6 "2016-03-17T14:48:37Z")

</div>

you can set multiple vars in single set\_fact:

- set\_fact:  
mainVpcId: “{{ item.vpc\_id }}”  
mainSubnet0: “{{ [item.subnets.0.id](http://item.subnets.0.id) }}”

mainSubnet1: “{{ [item.subnets.1.id](http://item.subnets.1.id) }}”

mainSubnet2: “{{ [item.subnets.2.id](http://item.subnets.2.id) }}”

with\_items: vpcOut.results  
when:

- not item | skipped
- item.item.resource\_tags.WhichRegion == ‘mainRegion’
