|
40 | 40 | ansible.builtin.set_fact: |
41 | 41 | infra__aws_igw_id: "{{ __aws_igw.gateway_id }}" |
42 | 42 |
|
43 | | -- name: Create AWS VPC Subnets |
| 43 | +- name: Create AWS VPC Public Subnets |
44 | 44 | amazon.aws.ec2_vpc_subnet: |
45 | 45 | region: "{{ infra__region }}" |
46 | 46 | vpc_id: "{{ infra__aws_vpc_id }}" |
47 | | - cidr: "{{ __aws_subnet_create_item.cidr }}" |
| 47 | + cidr: "{{ __aws_public_subnet_item.cidr }}" |
48 | 48 | state: present |
49 | | - tags: "{{ infra__tags | combine(__aws_subnet_create_item.tags, recursive=True) }}" |
| 49 | + tags: "{{ infra__tags | combine(__aws_public_subnet_item.tags, recursive = true) }}" |
50 | 50 | map_public: yes |
51 | 51 | az: "{{ __aws_az_info.availability_zones[__aws_subnet_index % infra__aws_vpc_az_count | int].zone_name }}" |
52 | 52 | loop_control: |
53 | | - loop_var: __aws_subnet_create_item |
| 53 | + loop_var: __aws_public_subnet_item |
54 | 54 | index_var: __aws_subnet_index |
55 | | - label: __aws_subnet_create_item.name |
56 | | - loop: "{{ infra__vpc_public_subnets_info | union(infra__vpc_private_subnets_info) }}" |
57 | | - register: __aws_subnets |
| 55 | + loop: "{{ infra__vpc_public_subnets_info }}" |
| 56 | + register: __aws_public_subnets |
58 | 57 |
|
59 | | -- name: Set fact for AWS Subnet IDs |
| 58 | +- name: Set fact for AWS Public Subnet IDs |
60 | 59 | ansible.builtin.set_fact: |
61 | | - infra__aws_subnet_ids: "{{ infra__aws_subnet_ids | default([]) | union([__aws_subnet_item.subnet.id | default('')]) }}" |
| 60 | + infra__aws_public_subnet_ids: "{{ infra__aws_public_subnet_ids | default([]) | union([__aws_subnet_item.subnet.id | default('')]) }}" |
62 | 61 | loop_control: |
63 | 62 | loop_var: __aws_subnet_item |
64 | 63 | label: "{{ __aws_subnet_item.subnet.id }}" |
65 | | - loop: "{{ __aws_subnets.results }}" |
| 64 | + loop: "{{ __aws_public_subnets.results }}" |
66 | 65 |
|
67 | 66 | - name: List the Route Tables for the VPC |
68 | 67 | community.aws.ec2_vpc_route_table_info: |
|
71 | 70 | vpc-id: "{{ infra__aws_vpc_id }}" |
72 | 71 | register: __aws_route_table_list |
73 | 72 |
|
74 | | -- name: Configure the Route Table |
| 73 | +- name: Configure the Public Route Table |
75 | 74 | community.aws.ec2_vpc_route_table: |
76 | 75 | region: "{{ infra__region }}" |
77 | 76 | vpc_id: "{{ infra__aws_vpc_id }}" |
78 | 77 | route_table_id: "{{ __aws_route_table_id }}" |
79 | 78 | lookup: id |
80 | 79 | state: present |
81 | | - tags: "{{ infra__tags | combine({ 'Name': infra__aws_route_table_name }, recursive=True) }}" |
| 80 | + tags: "{{ infra__tags | combine({ 'Name': infra__aws_public_route_table_name }, recursive=True) }}" |
82 | 81 | routes: |
83 | 82 | - dest: "0.0.0.0/0" |
84 | 83 | gateway_id: "{{ infra__aws_igw_id }}" |
85 | | - subnets: "{{ infra__aws_subnet_ids }}" |
| 84 | + subnets: "{{ infra__aws_public_subnet_ids }}" |
86 | 85 | vars: |
87 | | - __aws_route_table_id: "{{ __aws_route_table_list.route_tables[0].associations[0].route_table_id }}" |
| 86 | + __aws_route_table_id: "{{ __aws_route_table_list.route_tables | json_query(__aws_rtb_jq) | flatten | first }}" |
| 87 | + __aws_rtb_jq: "[*].associations[?main == `true` ].route_table_id" |
| 88 | + |
| 89 | +- name: Set the fact for Subnet Ids |
| 90 | + when: not infra__tunnel |
| 91 | + ansible.builtin.set_fact: |
| 92 | + infra__aws_subnet_ids: "{{ infra__aws_public_subnet_ids }}" |
| 93 | + |
| 94 | +- name: Setup for private networking |
| 95 | + when: infra__tunnel |
| 96 | + block: |
| 97 | + - name: Create AWS VPC Private Subnets |
| 98 | + amazon.aws.ec2_vpc_subnet: |
| 99 | + region: "{{ infra__region }}" |
| 100 | + vpc_id: "{{ infra__aws_vpc_id }}" |
| 101 | + cidr: "{{ __aws_private_subnet_item.cidr }}" |
| 102 | + state: present |
| 103 | + tags: "{{ infra__tags | combine(__aws_private_subnet_item.tags, recursive = true) }}" |
| 104 | + map_public: no |
| 105 | + az: "{{ __aws_az_info.availability_zones[__aws_subnet_index % infra__aws_vpc_az_count | int].zone_name }}" |
| 106 | + loop_control: |
| 107 | + loop_var: __aws_private_subnet_item |
| 108 | + index_var: __aws_subnet_index |
| 109 | + label: "{{ __aws_private_subnet_item.name }}" |
| 110 | + loop: "{{ infra__vpc_private_subnets_info }}" |
| 111 | + register: __aws_private_subnets |
| 112 | + |
| 113 | + - name: Set fact for AWS Private Subnet IDs |
| 114 | + ansible.builtin.set_fact: |
| 115 | + infra__aws_private_subnet_ids: "{{ infra__aws_private_subnet_ids | default([]) | union([__aws_subnet_item.subnet.id | default('')]) }}" |
| 116 | + loop_control: |
| 117 | + loop_var: __aws_subnet_item |
| 118 | + loop: "{{ __aws_private_subnets.results }}" |
| 119 | + |
| 120 | + - name: Set fact for Subnet Ids |
| 121 | + ansible.builtin.set_fact: |
| 122 | + infra__aws_subnet_ids: "{{ infra__aws_public_subnet_ids | union(infra__aws_private_subnet_ids) }}" |
| 123 | + |
| 124 | + - name: Creates NAT gateways and allocates EIPs |
| 125 | + community.aws.ec2_vpc_nat_gateway: |
| 126 | + state: present |
| 127 | + subnet_id: "{{ __aws_public_subnet_id }}" |
| 128 | + wait: true |
| 129 | + if_exist_do_not_create: true |
| 130 | + region: "{{ infra__region }}" |
| 131 | + tags: "{{ infra__tags | combine({ 'Name': '-'.join([infra__aws_nat_gateway_name, __aws_public_subnet_index | string]) }, recursive=True) }}" |
| 132 | + loop_control: |
| 133 | + loop_var: __aws_public_subnet_id |
| 134 | + index_var: __aws_public_subnet_index |
| 135 | + loop: "{{ infra__aws_public_subnet_ids }}" |
| 136 | + register: __aws_ngws |
| 137 | + |
| 138 | + - name: Configure Private Route Tables |
| 139 | + community.aws.ec2_vpc_route_table: |
| 140 | + vpc_id: "{{ infra__aws_vpc_id }}" |
| 141 | + region: "{{ infra__region }}" |
| 142 | + tags: "{{ infra__tags | combine({ 'Name': '-'.join([infra__aws_private_route_table_name, __aws_private_subnet_id_index | string]) }, recursive=True) }}" |
| 143 | + subnets: "{{ __aws_private_subnet_id_item }}" |
| 144 | + routes: |
| 145 | + - dest: "0.0.0.0/0" |
| 146 | + nat_gateway_id: "{{ __aws_ngws.results[ __aws_private_subnet_id_index % __aws_ngws.results | length ].nat_gateway_id }}" |
| 147 | + loop_control: |
| 148 | + loop_var: __aws_private_subnet_id_item |
| 149 | + index_var: __aws_private_subnet_id_index |
| 150 | + loop: "{{ infra__aws_private_subnet_ids }}" |
88 | 151 |
|
89 | 152 | - name: Create Security Groups |
90 | 153 | amazon.aws.ec2_group: |
|
0 commit comments