Skip to content

Commit ecb90c9

Browse files
committed
Update AWS VPC and subnet ID assignment to support runlevel, then upstream, and finally discovery
Signed-off-by: Webster Mudge <wmudge@cloudera.com>
1 parent b883145 commit ecb90c9

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

roles/platform/tasks/initialize_setup_aws.yml

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
plat__aws_xaccount_external_id: "{{ plat__cdp_xaccount_external_id }}"
4848
plat__aws_xaccount_account_id: "{{ plat__cdp_xaccount_account_id }}"
4949

50-
# TODO - Confirm the two following tasks are the design pattern we want: checking for a set_fact from another role before establishing its own role fact
51-
- name: Discover AWS VPC
52-
when: infra__aws_vpc_id is undefined
50+
# Runlevel first, upstream second, and discover third
51+
- name: Discover AWS VPC if not defined or established by Infrastructure
52+
when: plat__aws_vpc_id == "" and infra__aws_vpc_id is undefined
5353
block:
54-
- name: Query AWS VPC
54+
- name: Query AWS VPC by name
5555
amazon.aws.ec2_vpc_net_info:
5656
region: "{{ plat__region }}"
5757
filters:
@@ -63,49 +63,73 @@
6363
ansible.builtin.set_fact:
6464
plat__aws_vpc_id: "{{ __aws_vpc_info.vpcs[0].id }}"
6565

66-
- name: Set fact for AWS VPC ID by assignment
67-
when: infra__aws_vpc_id is defined
66+
- name: Set fact for AWS VPC ID if established by Infrastructure
67+
when: plat__aws_vpc_id == "" and infra__aws_vpc_id is defined
6868
ansible.builtin.set_fact:
6969
plat__aws_vpc_id: "{{ infra__aws_vpc_id }}"
7070

71-
- name: Discover AWS VPC Subnets
72-
when: infra__aws_subnet_ids is undefined
71+
- name: Handle AWS Subnet IDs if not defined
72+
when: not plat__aws_public_subnet_ids or not plat__aws_private_subnet_ids # Defaults are empty lists
7373
block:
7474
- name: Query AWS Subnets
7575
amazon.aws.ec2_vpc_subnet_info:
7676
region: "{{ plat__region }}"
7777
filters:
78-
"tag:Name": "{{ plat__namespace }}"
78+
vpc-id: "{{ plat__aws_vpc_id }}"
7979
register: __aws_subnets_info
8080

81-
- name: Set fact for AWS Subnet IDs
82-
when: __aws_subnets_info is defined
81+
- name: Assert discovered AWS Subnets
82+
ansible.builtin.assert:
83+
that: __aws_subnets_info.subnets | length > 0
84+
fail_msg: "No subnets discovered for AWS VPC"
85+
quiet: yes
86+
87+
- name: Set fact for AWS Public Subnet IDs if established by Infrastructure
88+
when: not plat__aws_public_subnet_ids and infra__aws_public_subnet_ids is defined
89+
ansible.builtin.set_fact:
90+
plat__aws_public_subnet_ids: "{{ infra__aws_public_subnet_ids }}"
91+
92+
- name: Discover AWS VPC Public Subnets
93+
when: not plat__aws_public_subnet_ids and infra__aws_public_subnet_ids is undefined
8394
ansible.builtin.set_fact:
84-
plat__aws_subnet_ids: "{{ plat__aws_subnet_ids | default([]) | union([__aws_subnet_item.subnet.id | default('')]) }}"
95+
__aws_public_subnet_ids: "{{ __aws_public_subnet_ids | default([]) | union([__aws_subnet_item.subnet_id | default('')]) }}"
8596
loop_control:
8697
loop_var: __aws_subnet_item
87-
label: "{{ __aws_subnet_item.subnet.id }}"
88-
loop: "{{ __aws_subnets_info.subnets }}"
98+
label: "{{ __aws_subnet_item.subnet_id }}"
99+
loop: "{{ __aws_subnets_info.subnets | selectattr('map_public_ip_on_launch') }}"
89100

90-
- name: Set fact for AWS Subnet IDs by assignment
91-
when: infra__aws_subnet_ids is defined
92-
ansible.builtin.set_fact:
93-
plat__aws_subnet_ids: "{{ infra__aws_subnet_ids }}"
101+
- name: Set fact for AWS Private Subnet IDs if established by Infrastructure
102+
when: not plat__aws_private_subnet_ids and infra__aws_private_subnet_ids is defined
103+
ansible.builtin.set_fact:
104+
plat__aws_private_subnet_ids: "{{ infra__aws_private_subnet_ids }}"
105+
106+
- name: Discover AWS VPC Private Subnets
107+
when: not plat__aws_private_subnet_ids and infra__aws_private_subnet_ids is undefined
108+
ansible.builtin.set_fact:
109+
__aws_private_subnet_ids: "{{ __aws_private_subnet_ids | default([]) | union([__aws_subnet_item.subnet_id | default('')]) }}"
110+
loop_control:
111+
loop_var: __aws_subnet_item
112+
label: "{{ __aws_subnet_item.subnet_id }}"
113+
loop: "{{ __aws_subnets_info.subnets | rejectattr('map_public_ip_on_launch') }}"
114+
115+
- name: Set fact for AWS VPC Subnets
116+
ansible.builtin.set_fact:
117+
plat__aws_public_subnet_ids: "{{ __aws_public_subnet_ids | default([]) }}"
118+
plat__aws_private_subnet_ids: "{{ __aws_private_subnet_ids | default([]) }}"
94119

95-
# TODO: Discover AWS VPC Public Subnets if infra__ is not present
96-
- name: Set public subnets for public endpoint access
97-
when: plat__public_endpoint_access
120+
- name: Set fact for AWS Subnet IDs
98121
ansible.builtin.set_fact:
99-
plat__aws_public_subnet_ids: "{{ infra__aws_public_subnet_ids }}"
100-
plat__endpoint_access_scheme: "PUBLIC"
122+
plat__aws_subnet_ids: "{{ plat__aws_public_subnet_ids | union(plat__aws_private_subnet_ids) }}"
101123

124+
# TODO Collapse the two SG queries together
102125
- name: Discover AWS Security Group for Knox
103126
when: infra__aws_security_group_knox_id is undefined
104127
block:
105128
- name: Query AWS Security Group for Knox
106129
amazon.aws.ec2_group_info:
107130
region: "{{ plat__region }}"
108131
filters:
132+
vpc-id: "{{ plat__aws_vpc_id }}"
109133
group-name: "{{ plat__security_group_knox_name }}"
110134
register: __aws_security_group_knox_info
111135

@@ -126,6 +150,7 @@
126150
amazon.aws.ec2_group_info:
127151
region: "{{ plat__region }}"
128152
filters:
153+
vpc-id: "{{ plat__aws_vpc_id }}"
129154
group-name: "{{ plat__security_group_default_name }}"
130155
register: __aws_security_group_default_info
131156

0 commit comments

Comments
 (0)