26
26
- Desired state of the network configuration.
27
27
- Choices include present (create), absent (delete), or update (modify).
28
28
type: str
29
- choices: ['present', 'absent', 'update' ]
29
+ choices: ['present', 'absent']
30
30
default: present
31
- force :
31
+ update :
32
32
description:
33
- - If true it will create subnet when state is update but subnet is missing and update the subnet when state is present and subnet already exists
33
+ - If O(state=present) then it will update the subnet if needed.
34
34
type: bool
35
- default: False
35
+ default: True
36
36
subnet:
37
37
description:
38
38
- subnet CIDR.
39
39
type: str
40
40
required: true
41
- type:
42
- description:
43
- - Type of network configuration.
44
- - Currently only supports 'subnet'.
45
- type: str
46
- choices: ['subnet']
47
- default: subnet
48
41
vnet:
49
42
description:
50
43
- The virtual network to which the subnet belongs.
113
106
subnet: 10.10.2.0/24
114
107
zone: ans1
115
108
state: present
116
-
117
- - name: Update a subnet
118
- community.proxmox.proxmox_subnet:
119
- api_user: "{{ pc.proxmox.api_user }}"
120
- api_token_id: "{{ pc.proxmox.api_token_id }}"
121
- api_token_secret: "{{ vault.proxmox.api_token_secret }}"
122
- api_host: "{{ pc.proxmox.api_host }}"
123
- validate_certs: no
124
- vnet: test
125
- subnet: 10.10.2.0/24
126
- zone: ans1
127
- state: update
128
109
dhcp_range:
129
110
- start: 10.10.2.5
130
111
end: 10.10.2.50
156
137
"""
157
138
158
139
from ansible .module_utils .basic import AnsibleModule
140
+ from ansible_collections .community .proxmox .plugins .module_utils .proxmox_sdn import ProxmoxSdnAnsible
159
141
from ansible_collections .community .proxmox .plugins .module_utils .proxmox import (
160
142
proxmox_auth_argument_spec ,
161
- ansible_to_proxmox_bool ,
162
- ProxmoxAnsible
143
+ ansible_to_proxmox_bool
163
144
)
164
145
165
146
166
147
def get_proxmox_args ():
167
148
return dict (
168
- state = dict (type = "str" , choices = ["present" , "absent" , "update" ], default = 'present' , required = False ),
169
- force = dict (type = "bool" , default = False , required = False ),
149
+ state = dict (type = "str" , choices = ["present" , "absent" ], default = 'present' , required = False ),
150
+ update = dict (type = "bool" , default = True ),
170
151
subnet = dict (type = "str" , required = True ),
171
- type = dict (type = "str" , choices = ['subnet' ], default = 'subnet' , required = False ),
172
152
vnet = dict (type = "str" , required = True ),
173
153
zone = dict (type = "str" , required = False ),
174
154
dhcp_dns_server = dict (type = "str" , required = False ),
@@ -196,25 +176,24 @@ def get_ansible_module():
196
176
return AnsibleModule (
197
177
argument_spec = module_args ,
198
178
required_if = [
199
- ('state' , 'present' , ['subnet' , 'type' , 'vnet' , 'zone' ]),
200
- ('state' , 'update' , ['zone' , 'vnet' , 'subnet' ]),
179
+ ('state' , 'present' , ['subnet' , 'vnet' , 'zone' ]),
201
180
('state' , 'absent' , ['zone' , 'vnet' , 'subnet' ]),
202
181
]
203
182
)
204
183
205
184
206
- class ProxmoxSubnetAnsible (ProxmoxAnsible ):
185
+ class ProxmoxSubnetAnsible (ProxmoxSdnAnsible ):
207
186
def __init__ (self , module ):
208
187
super (ProxmoxSubnetAnsible , self ).__init__ (module )
209
188
self .params = module .params
210
189
211
190
def run (self ):
212
191
state = self .params .get ("state" )
213
- force = self .params .get ("force " )
192
+ update = self .params .get ("update " )
214
193
215
194
subnet_params = {
216
195
'subnet' : self .params .get ('subnet' ),
217
- 'type' : self . params . get ( 'type' ) ,
196
+ 'type' : 'subnet' ,
218
197
'vnet' : self .params .get ('vnet' ),
219
198
'dhcp-dns-server' : self .params .get ('dhcp_dns_server' ),
220
199
'dhcp-range' : self .get_dhcp_range (),
@@ -225,9 +204,7 @@ def run(self):
225
204
}
226
205
227
206
if state == 'present' :
228
- self .subnet_present (force = force , ** subnet_params )
229
- elif state == 'update' :
230
- self .subnet_update (force = force , ** subnet_params )
207
+ self .subnet_present (update = update , ** subnet_params )
231
208
elif state == 'absent' :
232
209
self .subnet_absent (** subnet_params )
233
210
@@ -237,71 +214,46 @@ def get_dhcp_range(self):
237
214
dhcp_range = [f"start-address={ x ['start' ]} ,end-address={ x ['end' ]} " for x in self .params .get ('dhcp_range' )]
238
215
return dhcp_range
239
216
240
- def subnet_present (self , force , ** subnet_params ):
217
+ def subnet_present (self , update , ** subnet_params ):
241
218
vnet_name = subnet_params ['vnet' ]
242
219
lock = subnet_params ['lock-token' ]
243
- subnet = subnet_params ['subnet' ]
220
+ subnet_cidr = subnet_params ['subnet' ]
244
221
subnet_id = f"{ self .params ['zone' ]} -{ subnet_params ['subnet' ].replace ('/' , '-' )} "
245
222
246
223
try :
247
224
vnet = getattr (self .proxmox_api .cluster ().sdn ().vnets (), vnet_name )
248
225
249
226
# Check if subnet already present
250
227
if subnet_id in [x ['subnet' ] for x in vnet ().subnets ().get ()]:
251
- if force :
252
- self .subnet_update (force = False , ** subnet_params )
228
+ if update :
229
+ subnet = getattr (vnet ().subnets (), subnet_id )
230
+ subnet_params ['digest' ] = subnet .get ()['digest' ]
231
+ subnet_params ['delete' ] = self .params .get ('delete' )
232
+ del subnet_params ['type' ]
233
+ del subnet_params ['subnet' ]
234
+
235
+ subnet .put (** subnet_params )
236
+ self .apply_sdn_changes_and_release_lock (lock = lock )
237
+ self .module .exit_json (
238
+ changed = True , subnet = subnet_id , msg = f'Updated subnet { subnet_id } '
239
+ )
253
240
else :
254
241
self .release_lock (lock = lock )
255
242
self .module .exit_json (
256
- changed = False , subnet = subnet_id , msg = f'subnet { subnet_id } already present and force is false.'
243
+ changed = False , subnet = subnet_id , msg = f'subnet { subnet_id } already present and update is false.'
257
244
)
258
245
else :
259
246
vnet .subnets ().post (** subnet_params )
260
247
self .apply_sdn_changes_and_release_lock (lock = lock )
261
248
self .module .exit_json (
262
- changed = True , subnet = subnet_id , msg = f'Created new subnet { subnet } '
249
+ changed = True , subnet = subnet_id , msg = f'Created new subnet { subnet_cidr } '
263
250
)
264
251
except Exception as e :
265
252
self .rollback_sdn_changes_and_release_lock (lock = lock )
266
253
self .module .fail_json (
267
254
msg = f'Failed to create subnet. Rolling back all changes : { e } '
268
255
)
269
256
270
- def subnet_update (self , force , ** subnet_params ):
271
- lock = subnet_params ['lock-token' ]
272
- vnet_id = subnet_params ['vnet' ]
273
- subnet_id = f"{ self .params ['zone' ]} -{ subnet_params ['subnet' ].replace ('/' , '-' )} "
274
-
275
- try :
276
- vnet = getattr (self .proxmox_api .cluster ().sdn ().vnets (), vnet_id )
277
-
278
- # Check if subnet already present
279
- if subnet_id in [x ['subnet' ] for x in vnet ().subnets ().get ()]:
280
- subnet = getattr (vnet ().subnets (), subnet_id )
281
- subnet_params ['digest' ] = subnet .get ()['digest' ]
282
- subnet_params ['delete' ] = self .params .get ('delete' )
283
- del subnet_params ['type' ]
284
- del subnet_params ['subnet' ]
285
-
286
- subnet .put (** subnet_params )
287
- self .apply_sdn_changes_and_release_lock (lock = lock )
288
- self .module .exit_json (
289
- changed = True , subnet = subnet_id , msg = f'Updated subnet { subnet_id } '
290
- )
291
- else :
292
- if force :
293
- self .subnet_present (force = False , ** subnet_params )
294
- else :
295
- self .release_lock (lock = lock )
296
- self .module .exit_json (
297
- changed = False , subnet = subnet_id , msg = f'subnet { subnet_id } not present and force is false.'
298
- )
299
- except Exception as e :
300
- self .rollback_sdn_changes_and_release_lock (lock = lock )
301
- self .module .fail_json (
302
- msg = f'Failed to update subnet. Rolling back all changes. : { e } '
303
- )
304
-
305
257
def subnet_absent (self , ** subnet_params ):
306
258
vnet_id = subnet_params ['vnet' ]
307
259
lock = subnet_params ['lock-token' ]
0 commit comments