Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit e6405e2

Browse files
0livdalikins
authored andcommitted
Fetch vmid from the ProxmoxAPI when not set (ansible#3591)
The vmid is no longer a required parameter For the 'present' state: If not set, the next available one will be fetched from the API For the 'started', 'stopped', 'restarted' and 'absent' states: If not set, the module will try to fetch it from the API based on the hostname Inspired from the behavior of the proxmox_kvm module
1 parent 461cabd commit e6405e2

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

cloud/misc/proxmox.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040
vmid:
4141
description:
4242
- the instance id
43+
- if not set, the next available VM ID will be fetched from ProxmoxAPI.
44+
- if not set, will be fetched from PromoxAPI based on the hostname
4345
default: null
44-
required: true
46+
required: false
4547
validate_certs:
4648
description:
4749
- enable / disable https certificate verification
@@ -71,6 +73,7 @@
7173
description:
7274
- the instance hostname
7375
- required only for C(state=present)
76+
- must be unique if vmid is not passed
7477
default: null
7578
required: false
7679
ostemplate:
@@ -186,6 +189,9 @@
186189
hostname: example.org
187190
ostemplate: 'local:vztmpl/ubuntu-14.04-x86_64.tar.gz'
188191
192+
# Create new container automatically selecting the next available vmid.
193+
- proxmox: node='uk-mc02' api_user='root@pam' api_password='1q2w3e' api_host='node1' password='123456' hostname='example.org' ostemplate='local:vztmpl/ubuntu-14.04-x86_64.tar.gz'
194+
189195
# Create new container with minimal options with force(it will rewrite existing container)
190196
- proxmox:
191197
vmid: 100
@@ -297,6 +303,16 @@
297303

298304
VZ_TYPE=None
299305

306+
def get_nextvmid(proxmox):
307+
try:
308+
vmid = proxmox.cluster.nextid.get()
309+
return vmid
310+
except Exception as e:
311+
module.fail_json(msg="Unable to get next vmid. Failed with exception: %s")
312+
313+
def get_vmid(proxmox, hostname):
314+
return [ vm['vmid'] for vm in proxmox.cluster.resources.get(type='vm') if vm['name'] == hostname ]
315+
300316
def get_instance(proxmox, vmid):
301317
return [ vm for vm in proxmox.cluster.resources.get(type='vm') if vm['vmid'] == int(vmid) ]
302318

@@ -386,7 +402,7 @@ def main():
386402
api_host = dict(required=True),
387403
api_user = dict(required=True),
388404
api_password = dict(no_log=True),
389-
vmid = dict(required=True),
405+
vmid = dict(required=False),
390406
validate_certs = dict(type='bool', default='no'),
391407
node = dict(),
392408
pool = dict(),
@@ -426,6 +442,7 @@ def main():
426442
memory = module.params['memory']
427443
swap = module.params['swap']
428444
storage = module.params['storage']
445+
hostname = module.params['hostname']
429446
if module.params['ostemplate'] is not None:
430447
template_store = module.params['ostemplate'].split(":")[0]
431448
timeout = module.params['timeout']
@@ -445,10 +462,22 @@ def main():
445462
except Exception as e:
446463
module.fail_json(msg='authorization on proxmox cluster failed with exception: %s' % e)
447464

465+
# If vmid not set get the Next VM id from ProxmoxAPI
466+
# If hostname is set get the VM id from ProxmoxAPI
467+
if not vmid and state == 'present':
468+
vmid = get_nextvmid(proxmox)
469+
elif not vmid and hostname:
470+
vmid = get_vmid(proxmox, hostname)[0]
471+
elif not vmid:
472+
module.exit_json(changed=False, msg="Vmid could not be fetched for the following action: %s" % state)
473+
448474
if state == 'present':
449475
try:
450476
if get_instance(proxmox, vmid) and not module.params['force']:
451477
module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid)
478+
# If no vmid was passed, there cannot be another VM named 'hostname'
479+
if not module.params['vmid'] and get_vmid(proxmox, hostname) and not module.params['force']:
480+
module.exit_json(changed=False, msg="VM with hostname %s already exists and has ID number %s" % (hostname, get_vmid(proxmox, hostname)[0]))
452481
elif not (node, module.params['hostname'] and module.params['password'] and module.params['ostemplate']):
453482
module.fail_json(msg='node, hostname, password and ostemplate are mandatory for creating vm')
454483
elif not node_check(proxmox, node):

0 commit comments

Comments
 (0)