11
11
12
12
DOCUMENTATION = r"""
13
13
module: proxmox_zone
14
- short_description: Manage Proxmox zone configurations
14
+ short_description: Manage Proxmox zone configurations.
15
15
description:
16
- - list/create/update/delete proxmox sdn zones
16
+ - Create/Update/Delete proxmox sdn zones.
17
17
author: 'Jana Hoch <[email protected] > (!UNKNOWN)'
18
18
attributes:
19
19
check_mode:
28
28
choices:
29
29
- present
30
30
- absent
31
- - update
31
+ default: present
32
32
update:
33
33
description:
34
- - If state is present and zone exists it'll update.
34
+ - If O( state= present) and zone exists it'll update.
35
35
type: bool
36
36
default: true
37
37
type:
77
77
type: bool
78
78
dns:
79
79
description:
80
- - dns api server.
80
+ - DNS api server.
81
81
type: str
82
82
dnszone:
83
83
description:
84
- - dns domain zone.
84
+ - DNS domain zone.
85
85
type: str
86
86
dp_id:
87
87
description:
105
105
type: str
106
106
ipam:
107
107
description:
108
- - use a specific ipam.
109
- type: str
110
- lock_token:
111
- description:
112
- - the token for unlocking the global SDN configuration. If not provided it will generate new token
113
- - If the playbook fails for some reason you can manually clear lock token by deleting `/etc/pve/sdn/.lock`
108
+ - Use a specific ipam.
114
109
type: str
115
110
mac:
116
111
description:
214
209
215
210
def get_proxmox_args ():
216
211
return dict (
217
- state = dict (type = "str" , choices = ["present" , "absent" ], required = True ),
212
+ state = dict (type = "str" , default = "present" , choices = ["present" , "absent" ]),
218
213
update = dict (type = "bool" , default = True ),
219
214
type = dict (type = "str" ,
220
215
choices = ["evpn" , "faucet" , "qinq" , "simple" , "vlan" , "vxlan" ],
@@ -234,7 +229,6 @@ def get_proxmox_args():
234
229
exitnodes_primary = dict (type = "str" , required = False ),
235
230
fabric = dict (type = "str" , required = False ),
236
231
ipam = dict (type = "str" , required = False ),
237
- lock_token = dict (type = "str" , required = False , no_log = False ),
238
232
mac = dict (type = "str" , required = False ),
239
233
mtu = dict (type = "int" , required = False ),
240
234
nodes = dict (type = "str" , required = False ),
@@ -316,7 +310,7 @@ def run(self):
316
310
"exitnodes-primary" : self .params .get ("exitnodes_primary" ),
317
311
"fabric" : self .params .get ("fabric" ),
318
312
"ipam" : self .params .get ("ipam" ),
319
- "lock-token" : self . params . get ( "lock_token" ) ,
313
+ "lock-token" : None ,
320
314
"mac" : self .params .get ("mac" ),
321
315
"mtu" : self .params .get ("mtu" ),
322
316
"nodes" : self .params .get ("nodes" ),
@@ -329,9 +323,6 @@ def run(self):
329
323
"vxlan-port" : self .params .get ("vxlan_port" ),
330
324
}
331
325
332
- if zone_params ['lock-token' ] is None and state is not None :
333
- zone_params ['lock-token' ] = self .get_global_sdn_lock ()
334
-
335
326
if state == "present" :
336
327
self .zone_present (update , ** zone_params )
337
328
@@ -345,67 +336,70 @@ def zone_present(self, update, **kwargs):
345
336
available_zones = {x .get ('zone' ): {'type' : x .get ('type' ), 'digest' : x .get ('digest' )} for x in self .get_zones ()}
346
337
zone_name = kwargs .get ("zone" )
347
338
zone_type = kwargs .get ("type" )
348
- lock = kwargs .get ('lock-token' )
349
339
350
340
# Check if zone already exists
351
341
if zone_name in available_zones .keys () and update :
352
342
if zone_type != available_zones [zone_name ]['type' ]:
353
- self .release_lock (lock )
354
343
self .module .fail_json (
355
344
msg = f'zone { zone_name } exists with different type and we cannot change type post fact.'
356
345
)
357
346
else :
358
347
try :
348
+ kwargs ['lock-token' ] = self .get_global_sdn_lock ()
359
349
kwargs ['digest' ] = available_zones [zone_name ]['digest' ]
350
+ del kwargs ['zone' ]
351
+ del kwargs ['type' ]
352
+
360
353
zone = getattr (self .proxmox_api .cluster ().sdn ().zones (), zone_name )
361
354
zone .put (** kwargs )
362
- self .apply_sdn_changes_and_release_lock (lock )
355
+ self .apply_sdn_changes_and_release_lock (kwargs [ ' lock-token' ] )
363
356
self .module .exit_json (
364
357
changed = True , zone = zone_name , msg = f'Updated zone - { zone_name } '
365
358
)
366
359
except Exception as e :
367
- self .rollback_sdn_changes_and_release_lock (lock )
360
+ self .rollback_sdn_changes_and_release_lock (kwargs [ ' lock-token' ] )
368
361
self .module .fail_json (
369
362
msg = f'Failed to update zone { zone_name } - { e } '
370
363
)
371
364
372
365
elif zone_name in available_zones .keys () and not update :
373
- self .release_lock (lock )
374
366
self .module .exit_json (
375
- changed = False , zone = zone_name , msg = f'Zone { zone_name } already exists and force is false!'
367
+ changed = False , zone = zone_name , msg = f'Zone { zone_name } already exists and update is false!'
376
368
)
377
369
else :
378
370
try :
371
+ kwargs ['lock-token' ] = self .get_global_sdn_lock ()
372
+
379
373
self .proxmox_api .cluster ().sdn ().zones ().post (** kwargs )
380
- self .apply_sdn_changes_and_release_lock (lock )
374
+ self .apply_sdn_changes_and_release_lock (kwargs [ ' lock-token' ] )
381
375
self .module .exit_json (
382
376
changed = True , zone = zone_name , msg = f'Created new Zone - { zone_name } '
383
377
)
384
378
except Exception as e :
385
- self .rollback_sdn_changes_and_release_lock (lock )
379
+ self .rollback_sdn_changes_and_release_lock (kwargs [ ' lock-token' ] )
386
380
self .module .fail_json (
387
381
msg = f'Failed to create zone { zone_name } - { e } '
388
382
)
389
383
390
- def zone_absent (self , zone_name , lock ):
384
+ def zone_absent (self , zone_name , lock = None ):
391
385
available_zones = [x .get ('zone' ) for x in self .get_zones ()]
392
386
params = {'lock-token' : lock }
393
387
394
388
try :
395
389
if zone_name not in available_zones :
396
- self .release_lock (lock )
397
390
self .module .exit_json (
398
- changed = False , zone = zone_name , msg = f"zone { zone_name } already doesn't exist ."
391
+ changed = False , zone = zone_name , msg = f"zone { zone_name } is absent ."
399
392
)
400
393
else :
394
+ params ['lock-token' ] = self .get_global_sdn_lock ()
401
395
zone = getattr (self .proxmox_api .cluster ().sdn ().zones (), zone_name )
402
396
zone .delete (** params )
403
- self .apply_sdn_changes_and_release_lock (lock )
397
+ self .apply_sdn_changes_and_release_lock (params [ ' lock-token' ] )
404
398
self .module .exit_json (
405
399
changed = True , zone = zone_name , msg = f'Successfully deleted zone { zone_name } '
406
400
)
407
401
except Exception as e :
408
- self .rollback_sdn_changes_and_release_lock (lock )
402
+ self .rollback_sdn_changes_and_release_lock (params [ ' lock-token' ] )
409
403
self .module .fail_json (
410
404
msg = f'Failed to delete zone { zone_name } { e } . Rolling back all pending changes.'
411
405
)
0 commit comments