159
159
- 10.0.0.1
160
160
- 10.0.0.2
161
161
- 10.0.0.3
162
+ - name: Create a Proxmox Backup Server storage type
163
+ proxmox_storage:
164
+ name: pbs1
165
+ type: pbs
166
+ content: [ "backup" ]
167
+ server: 192.168.122.2
168
+ username: user@pbs
169
+ password: PBSPassword1
170
+ datastore: main
171
+ fingerprint: f2:fb:85:76:d2:2a:c4:96:5c:6e:d8:71:37:36:06:17:09:55:f7:04:e3:74:bb:aa:9e:26:85:92:63:c8:b9:23
172
+ encryption_key: autogen
162
173
- name: Create a ZFS storage type
163
174
proxmox_storage:
164
175
name: zfs1
175
186
from ansible .module_utils ._text import to_text
176
187
from ansible .module_utils .pvesh import ProxmoxShellError
177
188
import ansible .module_utils .pvesh as pvesh
189
+ import re
190
+ import json
191
+ from json import JSONDecodeError , loads as parse_json
192
+
178
193
179
194
class ProxmoxStorage (object ):
180
195
def __init__ (self , module ):
181
196
self .module = module
182
197
self .name = module .params ['name' ]
183
198
self .state = module .params ['state' ]
184
- self . type = module . params [ 'type' ]
199
+ # Globally applicable PVE API arguments
185
200
self .disable = module .params ['disable' ]
186
201
self .content = module .params ['content' ]
187
202
self .nodes = module .params ['nodes' ]
203
+ self .type = module .params ['type' ]
204
+ # Remaining PVE API arguments (depending on type) past this point
205
+ self .datastore = module .params ['datastore' ]
206
+ self .encryption_key = module .params ['encryption_key' ]
207
+ self .fingerprint = module .params ['fingerprint' ]
208
+ self .password = module .params ['password' ]
188
209
self .path = module .params ['path' ]
189
210
self .pool = module .params ['pool' ]
190
211
self .monhost = module .params ['monhost' ]
@@ -199,6 +220,24 @@ def __init__(self, module):
199
220
self .sparse = module .params ['sparse' ]
200
221
self .is_mountpoint = module .params ['is_mountpoint' ]
201
222
223
+ # Validate the parameters given to us
224
+ fingerprint_re = re .compile ('^([A-Fa-f0-9]{2}:){31}[A-Fa-f0-9]{2}$' )
225
+ if self .fingerprint is not None and not fingerprint_re .match (self .fingerprint ):
226
+ self .module .fail_json (msg = (f"fingerprint must be of the format, "
227
+ f"{ fingerprint_re .pattern } ." ))
228
+
229
+ if self .type == 'pbs' :
230
+ if self .content != ['backup' ]:
231
+ self .module .fail_json (msg = "PBS storage type only supports the "
232
+ "'backup' content type." )
233
+ try :
234
+ if self .encryption_key not in ["autogen" , None ]:
235
+ parse_json (self .encryption_key )
236
+ except JSONDecodeError :
237
+ self .module .fail_json (msg = ("encryption_key needs to be valid "
238
+ "JSON or set to 'autogen'." ))
239
+
240
+ # Attempt to retrieve current/live storage definitions
202
241
try :
203
242
self .existing_storages = pvesh .get ("storage" )
204
243
except ProxmoxShellError as e :
@@ -233,6 +272,16 @@ def prepare_storage_args(self):
233
272
args ['nodes' ] = ',' .join (self .nodes )
234
273
if self .disable is not None :
235
274
args ['disable' ] = 1 if self .disable else 0
275
+ if self .datastore is not None :
276
+ args ['datastore' ] = self .datastore
277
+ if self .encryption_key is not None :
278
+ args ['encryption-key' ] = self .encryption_key
279
+ if self .fingerprint is not None :
280
+ args ['fingerprint' ] = self .fingerprint
281
+ if self .master_pubkey is not None :
282
+ args ['master-pubkey' ] = self .master_pubkey
283
+ if self .password is not None :
284
+ args ['password' ] = self .password
236
285
if self .path is not None :
237
286
args ['path' ] = self .path
238
287
if self .pool is not None :
@@ -329,13 +378,20 @@ def main():
329
378
# Refer to https://pve.proxmox.com/pve-docs/api-viewer/index.html
330
379
module_args = dict (
331
380
name = dict (type = 'str' , required = True , aliases = ['storage' , 'storageid' ]),
381
+ state = dict (default = 'present' , choices = ['present' , 'absent' ], type = 'str' ),
382
+ # Globally applicable PVE API arguments
332
383
content = dict (type = 'list' , required = True , aliases = ['storagetype' ]),
384
+ disable = dict (required = False , type = 'bool' , default = False ),
333
385
nodes = dict (type = 'list' , required = False , default = None ),
334
386
type = dict (default = None , type = 'str' , required = True ,
335
387
choices = ["dir" , "nfs" , "rbd" , "lvm" , "lvmthin" , "cephfs" ,
336
- "zfspool" , "btrfs" ]),
337
- disable = dict (required = False , type = 'bool' , default = False ),
338
- state = dict (default = 'present' , choices = ['present' , 'absent' ], type = 'str' ),
388
+ "zfspool" , "btrfs" , "pbs" ]),
389
+ # Remaining PVE API arguments (depending on type) past this point
390
+ datastore = dict (default = None , type = 'str' , required = False ),
391
+ encryption_key = dict (default = None , type = 'str' , required = False ),
392
+ fingerprint = dict (default = None , type = 'str' , required = False ),
393
+ master_pubkey = dict (default = None , type = 'str' , required = False ),
394
+ password = dict (default = None , type = 'str' , required = False ),
339
395
path = dict (default = None , required = False , type = 'str' ),
340
396
pool = dict (default = None , type = 'str' , required = False ),
341
397
monhost = dict (default = None , type = 'list' , required = False ),
@@ -363,7 +419,11 @@ def main():
363
419
["type" , "lvmthin" , ["vgname" , "thinpool" , "content" ]],
364
420
["type" , "zfspool" , ["pool" , "content" ]],
365
421
["type" , "btrfs" , ["path" , "content" ]],
366
- ]
422
+ ["type" , "pbs" , ["server" , "username" , "password" , "datastore" ]]
423
+ ],
424
+ required_by = {
425
+ "master_pubkey" : "encryption_key"
426
+ }
367
427
)
368
428
storage = ProxmoxStorage (module )
369
429
0 commit comments