Skip to content

Commit 8cf7407

Browse files
committed
Improve VPC info handling in DigitalOcean tasks
Replaced direct access to vpc_info.data with vpc_info.get('data', []) to prevent errors when 'data' is missing. Updated conditional checks and set_fact tasks for more robust handling of VPC information.
1 parent 12a6a01 commit 8cf7407

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

automation/roles/cloud_resources/tasks/digitalocean.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@
143143
ansible.builtin.set_fact:
144144
default_ip_range: >-
145145
{{
146-
vpc_info.data
146+
vpc_info.get('data', [])
147147
| selectattr('region', 'equalto', server_location)
148148
| selectattr('default', 'equalto', true)
149149
| map(attribute='ip_range')
150150
| first
151151
}}
152152
when:
153153
- server_network | length < 1
154-
- vpc_info.data | selectattr('region', 'equalto', server_location) | selectattr('default', 'equalto', true) | list | length > 0
154+
- vpc_info.get('data', []) | selectattr('region', 'equalto', server_location) | selectattr('default', 'equalto', true) | list | length > 0
155155

156156
# if server_network is not specified and there is no default VPC, create a network
157157
- name: "DigitalOcean: Create a VPC '{{ digital_ocean_vpc_name | default('network-' + server_location | default('')) }}'"
@@ -163,32 +163,41 @@
163163
register: digital_ocean_vpc
164164
when:
165165
- server_network | length < 1
166-
- vpc_info.data | selectattr('region', 'equalto', server_location) | selectattr('default', 'equalto', true) | list | length == 0
166+
- vpc_info.get('data', []) | selectattr('region', 'equalto', server_location) | selectattr('default', 'equalto', true) | list | length == 0
167167

168168
- name: "Set variable: server_network"
169169
ansible.builtin.set_fact:
170170
server_network: "{{ digital_ocean_vpc_name | default('network-' + server_location) }}"
171-
when: digital_ocean_vpc is changed
171+
when:
172+
- digital_ocean_vpc is defined
173+
- digital_ocean_vpc is changed
172174

173175
- name: "DigitalOcean: Gather information about VPC"
174176
community.digitalocean.digital_ocean_vpc_info:
175177
oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}"
176178
register: vpc_info
177-
when: digital_ocean_vpc is changed
179+
when:
180+
- digital_ocean_vpc is defined
181+
- digital_ocean_vpc is changed
178182

179183
# if server_network is specified
180184
- name: "Fail if no VPC found in the specified region"
181185
ansible.builtin.fail:
182186
msg: "No VPC found with name '{{ server_network }}' in region '{{ server_location }}'"
183187
when:
184188
- server_network | length > 0
185-
- vpc_info.data | selectattr('region', 'equalto', server_location) | selectattr('name', 'equalto', server_network) | list | length == 0
189+
- (vpc_info.get('data', [])
190+
| selectattr('region', 'equalto', server_location)
191+
| selectattr('name', 'equalto', server_network)
192+
| list
193+
| length
194+
) == 0
186195

187196
- name: Extract ip_range from VPC "{{ server_network | default('') }}"
188197
ansible.builtin.set_fact:
189198
vpc_ip_range: >-
190199
{{
191-
vpc_info.data
200+
vpc_info.get('data', [])
192201
| selectattr('region', 'equalto', server_location)
193202
| selectattr('name', 'equalto', server_network)
194203
| map(attribute='ip_range')
@@ -200,7 +209,7 @@
200209
ansible.builtin.set_fact:
201210
vpc_id: >-
202211
{{
203-
vpc_info.data
212+
vpc_info.get('data', [])
204213
| selectattr('region', 'equalto', server_location)
205214
| selectattr('name', 'equalto', server_network)
206215
| map(attribute='id')

0 commit comments

Comments
 (0)