1

I need to build role that automatically creates subnets for VPC based on amount of AZ defined.

- name: Setup "{{ aws.vpc.name }}" VPC
  ec2_vpc:
    state: present
    cidr_block: 172.20.0.0/16
    resource_tags: { "Name": "{{aws.vpc.name}}" }
    subnets:
      - cidr: 172.20.1.0/24
        az: us-east-1d
        resource_tags: { "Name": "{{aws.vpc.name}}-1d" }
      - cidr: 172.20.2.0/24
        az: us-east-1c
        resource_tags: { "Name": "{{aws.vpc.name}}-1c" }
    internet_gateway: True
    route_tables:
      - subnets:
          - 172.20.1.0/24
          - 172.20.2.0/24
        routes:
          - dest: 0.0.0.0/0
            gw: igw
    region: {{ aws.vpc.region }}

How to generate automatically block like this one if I have only AZ name like

aws.vpc.az = ['a', 'c', 'e']

      - cidr: 172.20.1.0/24
        az: us-east-1d
        resource_tags: { "Name": "{{aws.vpc.name}}-1d" }

So, I have checked http://docs.ansible.com/ansible/playbooks_filters.html. But I didn't find a way to generate this block dynamically

  • I feel confused with question. Do you intend to have a list variable `az` in dictionary `aws.vpc` for list of AZs, and then you want to dynamically access that list and perform this task over that list? If that's not what you mean, can you elaborate? – rk2 Oct 14 '16 at 18:52

1 Answers1

0

Essentially you need another task that uses a loop to generate an Ansible list into a variable. This can then be used in the main task:

- subnets: "{{ subnet_list }}"

See this answer for an example using hosts. Your case is more complex as each item in subnet_list is itself a dictionary (cidr, az, and resource_tags being the keys).

Community
  • 1
  • 1
RichVel
  • 7,030
  • 6
  • 32
  • 48