Is there anyway to consolidate these set_fact tasks?

  • 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’

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

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

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

I previously tried ec2_vpc_net_facts, but it’s not available until version 2.1 (not release yet).

you can set multiple vars in single set_fact:

  • set_fact:
    mainVpcId: “{{ item.vpc_id }}”
    mainSubnet0: “{{ item.subnets.0.id }}”

mainSubnet1: “{{ item.subnets.1.id }}”

mainSubnet2: “{{ item.subnets.2.id }}”

with_items: vpcOut.results
when:

  • not item | skipped
  • item.item.resource_tags.WhichRegion == ‘mainRegion’