mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-21 01:41:25 -07:00
[PR #7143/07a47c04 backport][stable-8] add template option to proxmox and proxmox_kvm (#7488)
add template option to proxmox and proxmox_kvm (#7143)
* add template option to proxmox and proxmox_kvm
* make recommended updates
* fix tests
* resolve comments on PR
* save changes to changelog fragment
* Update changelogs/fragments/7143-proxmox-template.yml
Co-authored-by: Felix Fontein <felix@fontein.de>
---------
Co-authored-by: Eric Trombly <etrombly@iomaxis.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 07a47c047b
)
Co-authored-by: Eric Trombly <etrombly@yahoo.com>
This commit is contained in:
parent
e0489d738a
commit
79cfc48dd5
3 changed files with 89 additions and 6 deletions
|
@ -144,8 +144,9 @@ options:
|
|||
state:
|
||||
description:
|
||||
- Indicate desired state of the instance
|
||||
- V(template) was added in community.general 8.1.0.
|
||||
type: str
|
||||
choices: ['present', 'started', 'absent', 'stopped', 'restarted']
|
||||
choices: ['present', 'started', 'absent', 'stopped', 'restarted', 'template']
|
||||
default: present
|
||||
pubkey:
|
||||
description:
|
||||
|
@ -419,6 +420,23 @@ EXAMPLES = r'''
|
|||
api_host: node1
|
||||
state: restarted
|
||||
|
||||
- name: Convert container to template
|
||||
community.general.proxmox:
|
||||
vmid: 100
|
||||
api_user: root@pam
|
||||
api_password: 1q2w3e
|
||||
api_host: node1
|
||||
state: template
|
||||
|
||||
- name: Convert container to template (stop container if running)
|
||||
community.general.proxmox:
|
||||
vmid: 100
|
||||
api_user: root@pam
|
||||
api_password: 1q2w3e
|
||||
api_host: node1
|
||||
state: template
|
||||
force: true
|
||||
|
||||
- name: Remove container
|
||||
community.general.proxmox:
|
||||
vmid: 100
|
||||
|
@ -581,6 +599,13 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
|||
time.sleep(1)
|
||||
return False
|
||||
|
||||
def convert_to_template(self, vm, vmid, timeout, force):
|
||||
if getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running' and force:
|
||||
self.stop_instance(vm, vmid, timeout, force)
|
||||
# not sure why, but templating a container doesn't return a taskid
|
||||
getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).template.post()
|
||||
return True
|
||||
|
||||
def umount_instance(self, vm, vmid, timeout):
|
||||
taskid = getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.umount.post()
|
||||
while timeout:
|
||||
|
@ -621,7 +646,7 @@ def main():
|
|||
timeout=dict(type='int', default=30),
|
||||
force=dict(type='bool', default=False),
|
||||
purge=dict(type='bool', default=False),
|
||||
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted']),
|
||||
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'template']),
|
||||
pubkey=dict(type='str'),
|
||||
unprivileged=dict(type='bool', default=True),
|
||||
description=dict(type='str'),
|
||||
|
@ -791,6 +816,15 @@ def main():
|
|||
except Exception as e:
|
||||
module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e))
|
||||
|
||||
elif state == 'template':
|
||||
try:
|
||||
vm = proxmox.get_vm(vmid)
|
||||
|
||||
proxmox.convert_to_template(vm, vmid, timeout, force=module.params['force'])
|
||||
module.exit_json(changed=True, msg="VM %s is converted to template" % vmid)
|
||||
except Exception as e:
|
||||
module.fail_json(vmid=vmid, msg="conversion of VM %s to template failed with exception: %s" % (vmid, e))
|
||||
|
||||
elif state == 'restarted':
|
||||
try:
|
||||
vm = proxmox.get_vm(vmid)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue