Debian Codegraph @

ansible_collections/amazon/aws/tests/integration/targets/ec2_vpc_vgw/tasks/main.yml

raw
1 ---
2 - name: Ec2_vpc_vgw integration tests
3 module_defaults:
4 group/aws:
5 access_key: "{{ aws_access_key }}"
6 secret_key: "{{ aws_secret_key }}"
7 session_token: "{{ security_token | default(omit) }}"
8 region: "{{ aws_region }}"
9 block:
10 # ============================================================
11 - name: Create a VPC
12 amazon.aws.ec2_vpc_net:
13 name: "{{ vpc_name }}-{{ item }}"
14 state: present
15 cidr_block: "{{ vpc_cidr }}"
16 tags:
17 Description: Created by ansible-test for IGW tests
18 register: vpc_result
19 loop: [1, 2]
20
21 - name: Use set fact for vpc ids
22 ansible.builtin.set_fact:
23 vpc_id_1: "{{ vpc_result.results.0.vpc.id }}"
24 vpc_id_2: "{{ vpc_result.results.1.vpc.id }}"
25
26 # ============================================================
27 - name: Create vpn gateway and attach it to vpc - check_mode
28 amazon.aws.ec2_vpc_vgw:
29 state: present
30 vpc_id: "{{ vpc_id_1 }}"
31 name: "{{ vgw_name }}"
32 register: vgw_check_mode
33 check_mode: true
34
35 - name: Assert check_mode result - no vgw creation
36 ansible.builtin.assert:
37 that:
38 - vgw_check_mode.changed
39 - not vgw_check_mode.failed
40 - "'ec2:CreateVpnGateway' not in vgw_check_mode.resource_actions"
41 - "'ec2:AttachVpnGateway' not in vgw_check_mode.resource_actions"
42
43 - name: Create vpn gateway and attach it to vpc
44 amazon.aws.ec2_vpc_vgw:
45 state: present
46 vpc_id: "{{ vpc_id_1 }}"
47 name: "{{ vgw_name }}"
48 register: vgw
49
50 - name: Use set fact for vgw ids
51 ansible.builtin.set_fact:
52 vgw_id: "{{ vgw.vgw.id }}"
53
54 - name: Assert result - vgw creation
55 ansible.builtin.assert:
56 that:
57 - vgw.changed
58 - vgw.vgw.vpc_id == vpc_id_1
59 - vgw.vgw.tags.Name == vgw_name
60
61 - name: Test idempotence
62 amazon.aws.ec2_vpc_vgw:
63 state: present
64 vpc_id: "{{ vpc_id_1 }}"
65 name: "{{ vgw_name }}"
66 register: vgw
67
68 - name: Assert idempotence result - no change
69 ansible.builtin.assert:
70 that:
71 - not vgw.changed
72 - vgw.vgw.id == vgw_id
73
74 # ============================================================
75 - name: Attach vpn gateway to the other VPC - check_mode
76 amazon.aws.ec2_vpc_vgw:
77 state: present
78 vpc_id: "{{ vpc_id_2 }}"
79 name: "{{ vgw_name }}"
80 register: vgw_check_mode
81 check_mode: true
82
83 - name: Assert check_mode result - no create/attach/detach
84 ansible.builtin.assert:
85 that:
86 - vgw_check_mode.changed
87 - not vgw_check_mode.failed
88 - "'ec2:CreateVpnGateway' not in vgw_check_mode.resource_actions"
89 - "'ec2:AttachVpnGateway' not in vgw_check_mode.resource_actions"
90 - "'ec2:DetachVpnGateway' not in vgw_check_mode.resource_actions"
91
92 - name: Attach vpn gateway to the other VPC
93 amazon.aws.ec2_vpc_vgw:
94 state: present
95 vpc_id: "{{ vpc_id_2 }}"
96 name: "{{ vgw_name }}"
97 register: vgw
98
99 - name: Assert attach result
100 ansible.builtin.assert:
101 that:
102 - vgw.changed
103 - vgw.vgw.id == vgw_id
104 - vgw.vgw.vpc_id == vpc_id_2
105
106 - name: Attach vpn gateway to the other VPC - idempotency
107 amazon.aws.ec2_vpc_vgw:
108 state: present
109 vpc_id: "{{ vpc_id_2 }}"
110 name: "{{ vgw_name }}"
111 register: vgw
112
113 - name: Assert idempotency result - no change
114 ansible.builtin.assert:
115 that:
116 - not vgw.changed
117 - not vgw.failed
118 - "'ec2:CreateVpnGateway' not in vgw_check_mode.resource_actions"
119 - "'ec2:AttachVpnGateway' not in vgw_check_mode.resource_actions"
120 - "'ec2:DetachVpnGateway' not in vgw_check_mode.resource_actions"
121
122 # ============================================================
123
124 - name: Get VPC VGW facts by ID (CHECK)
125 amazon.aws.ec2_vpc_vgw_info:
126 vpn_gateway_ids: ["{{ vgw_id }}"]
127 register: vgw_info
128 check_mode: true
129
130 - name: Verify expected facts
131 vars:
132 vgw_details: "{{ vgw_info.virtual_gateways[0] }}"
133 attach_1_description:
134 state: detached
135 vpc_id: "{{ vpc_id_1 }}"
136 attach_2_description:
137 state: attached
138 vpc_id: "{{ vpc_id_2 }}"
139 ansible.builtin.assert:
140 that:
141 - vgw_info.virtual_gateways | length == 1
142 - '"resource_tags" in vgw_details'
143 - '"state" in vgw_details'
144 - '"tags" in vgw_details'
145 - '"type" in vgw_details'
146 - '"vpc_attachments" in vgw_details'
147 - '"vpn_gateway_id" in vgw_details'
148 - vgw_details.vpn_gateway_id == vgw_id
149 - vgw_details.type == 'ipsec.1'
150 - vgw_details.state == 'available'
151 - '"Name" in vgw_details.resource_tags'
152 - vgw_details.resource_tags.Name == vgw_name
153 - ( attach_1_description in vgw_details.vpc_attachments and vgw_details.vpc_attachments | length == 2 ) or ( vgw_details.vpc_attachments | length == 1 )
154 - attach_2_description in vgw_details.vpc_attachments
155
156 - name: Get VPC VGW facts by Tag
157 amazon.aws.ec2_vpc_vgw_info:
158 filters:
159 tag:Name: "{{ vgw_name }}"
160 register: vgw_info
161
162 - name: Verify expected facts
163 vars:
164 vgw_details: "{{ vgw_info.virtual_gateways[0] }}"
165 attach_1_description:
166 state: detached
167 vpc_id: "{{ vpc_id_1 }}"
168 attach_2_description:
169 state: attached
170 vpc_id: "{{ vpc_id_2 }}"
171 ansible.builtin.assert:
172 that:
173 - vgw_info.virtual_gateways | length == 1
174 - '"resource_tags" in vgw_details'
175 - '"state" in vgw_details'
176 - '"tags" in vgw_details'
177 - '"type" in vgw_details'
178 - '"vpc_attachments" in vgw_details'
179 - '"vpn_gateway_id" in vgw_details'
180 - vgw_details.vpn_gateway_id == vgw_id
181 - vgw_details.type == 'ipsec.1'
182 - vgw_details.state == 'available'
183 - '"Name" in vgw_details.resource_tags'
184 - vgw_details.resource_tags.Name == vgw_name
185 - ( attach_1_description in vgw_details.vpc_attachments and vgw_details.vpc_attachments | length == 2 ) or ( vgw_details.vpc_attachments | length == 1 )
186 - attach_2_description in vgw_details.vpc_attachments
187
188 # ============================================================
189
190 - name: Get all VGWs
191 amazon.aws.ec2_vpc_vgw_info:
192 register: vgw_info
193
194 - name: Verify test VGW is in the results
195 vars:
196 vgw_id_list: '{{ vgw_info.virtual_gateways | map(attribute="vpn_gateway_id") | list }}'
197 ansible.builtin.assert:
198 that:
199 - vgw_id in vgw_id_list
200
201 # ============================================================
202 - name: Detach vpn gateway - check_mode
203 amazon.aws.ec2_vpc_vgw:
204 state: present
205 name: "{{ vgw_name }}"
206 register: vgw_check_mode
207 check_mode: true
208
209 - name: Assert check_mode results - no detach action
210 ansible.builtin.assert:
211 that:
212 - vgw_check_mode.changed
213 - not vgw_check_mode.failed
214 - "'ec2:DetachVpcGateway' not in vgw_check_mode.resource_actions"
215
216 - name: Detach vpn gateway
217 amazon.aws.ec2_vpc_vgw:
218 state: present
219 name: "{{ vgw_name }}"
220 register: vgw
221
222 - name: Assert results
223 ansible.builtin.assert:
224 that:
225 - vgw.changed
226 - not vgw.vgw.vpc_id
227
228 - name: Test idempotence
229 amazon.aws.ec2_vpc_vgw:
230 state: present
231 name: "{{ vgw_name }}"
232 register: vgw
233
234 - name: Assert idempotency result - no change
235 ansible.builtin.assert:
236 that:
237 - not vgw.changed
238
239 # ============================================================
240
241 - ansible.builtin.include_tasks: tags.yml
242 - name: Delete vpn gateway - check_mode
243 amazon.aws.ec2_vpc_vgw:
244 state: absent
245 name: "{{ vgw_name }}"
246 register: vgw_check_mode
247 check_mode: true
248
249 - name: Assert check_mode result - no delete action
250 ansible.builtin.assert:
251 that:
252 - vgw_check_mode.changed
253 - not vgw_check_mode.failed
254 - "'ec2:DeleteVpnGateway' not in vgw_check_mode.resource_actions"
255
256 - name: Delete vpn gateway
257 amazon.aws.ec2_vpc_vgw:
258 state: absent
259 name: "{{ vgw_name }}"
260 register: vgw
261
262 - name: Assert result
263 ansible.builtin.assert:
264 that:
265 - vgw.changed
266
267 - name: Test idempotence
268 amazon.aws.ec2_vpc_vgw:
269 state: absent
270 name: "{{ vgw_name }}"
271 register: vgw
272
273 - name: Assert idempotency result - no change
274 ansible.builtin.assert:
275 that:
276 - not vgw.changed
277
278 always:
279 - ansible.builtin.debug:
280
281 msg: "Removing test dependencies"
282 - name: Delete vpn gateway
283 amazon.aws.ec2_vpc_vgw:
284 state: absent
285 vpn_gateway_id: "{{ vgw.vgw.id | default(vgw_id) }}"
286 ignore_errors: true
287
288 - name: Delete vpc
289 amazon.aws.ec2_vpc_net:
290 name: "{{ vpc_name }}-{{ item }}"
291 state: absent
292 cidr_block: "{{ vpc_cidr }}"
293 loop: [1, 2]
294 register: result
295 retries: 10
296 delay: 5
297 until: result is not failed
298 ignore_errors: true