mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
proxmox: basic linting
using black via trunk.io
This commit is contained in:
parent
572caeaa39
commit
ca7214f60e
1 changed files with 491 additions and 237 deletions
|
@ -6,9 +6,10 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = """
|
||||||
---
|
---
|
||||||
module: proxmox
|
module: proxmox
|
||||||
short_description: Management of instances in Proxmox VE cluster
|
short_description: Management of instances in Proxmox VE cluster
|
||||||
|
@ -222,9 +223,9 @@ extends_documentation_fragment:
|
||||||
- community.general.proxmox.documentation
|
- community.general.proxmox.documentation
|
||||||
- community.general.proxmox.selection
|
- community.general.proxmox.selection
|
||||||
- community.general.attributes
|
- community.general.attributes
|
||||||
'''
|
"""
|
||||||
|
|
||||||
EXAMPLES = r'''
|
EXAMPLES = r"""
|
||||||
- name: Create new container with minimal options
|
- name: Create new container with minimal options
|
||||||
community.general.proxmox:
|
community.general.proxmox:
|
||||||
vmid: 100
|
vmid: 100
|
||||||
|
@ -468,31 +469,40 @@ EXAMPLES = r'''
|
||||||
api_password: 1q2w3e
|
api_password: 1q2w3e
|
||||||
api_host: node1
|
api_host: node1
|
||||||
state: absent
|
state: absent
|
||||||
'''
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.common.text.converters import to_native
|
from ansible.module_utils.common.text.converters import to_native
|
||||||
|
|
||||||
from ansible_collections.community.general.plugins.module_utils.proxmox import (
|
from ansible_collections.community.general.plugins.module_utils.proxmox import (
|
||||||
ansible_to_proxmox_bool, proxmox_auth_argument_spec, ProxmoxAnsible)
|
ProxmoxAnsible,
|
||||||
|
ansible_to_proxmox_bool,
|
||||||
|
proxmox_auth_argument_spec,
|
||||||
|
)
|
||||||
|
from ansible_collections.community.general.plugins.module_utils.version import (
|
||||||
|
LooseVersion,
|
||||||
|
)
|
||||||
|
|
||||||
VZ_TYPE = None
|
VZ_TYPE = None
|
||||||
|
|
||||||
|
|
||||||
class ProxmoxLxcAnsible(ProxmoxAnsible):
|
class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
def content_check(self, node, ostemplate, template_store):
|
def content_check(self, node, ostemplate, template_store):
|
||||||
return [True for cnt in self.proxmox_api.nodes(node).storage(template_store).content.get() if cnt['volid'] == ostemplate]
|
return [
|
||||||
|
True
|
||||||
|
for cnt in self.proxmox_api.nodes(node)
|
||||||
|
.storage(template_store)
|
||||||
|
.content.get()
|
||||||
|
if cnt["volid"] == ostemplate
|
||||||
|
]
|
||||||
|
|
||||||
def is_template_container(self, node, vmid):
|
def is_template_container(self, node, vmid):
|
||||||
"""Check if the specified container is a template."""
|
"""Check if the specified container is a template."""
|
||||||
proxmox_node = self.proxmox_api.nodes(node)
|
proxmox_node = self.proxmox_api.nodes(node)
|
||||||
config = getattr(proxmox_node, VZ_TYPE)(vmid).config.get()
|
config = getattr(proxmox_node, VZ_TYPE)(vmid).config.get()
|
||||||
return config.get('template', False)
|
return config.get("template", False)
|
||||||
|
|
||||||
def update_config(self, vmid, node, disk, cpus, memory, swap, **kwargs):
|
def update_config(self, vmid, node, disk, cpus, memory, swap, **kwargs):
|
||||||
if VZ_TYPE != "lxc":
|
if VZ_TYPE != "lxc":
|
||||||
|
@ -547,7 +557,7 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
|
|
||||||
# compare the requested config against the current
|
# compare the requested config against the current
|
||||||
update_config = False
|
update_config = False
|
||||||
for (arg, value) in kwargs.items():
|
for arg, value in kwargs.items():
|
||||||
# if the arg isn't in the current config, it needs to be updated
|
# if the arg isn't in the current config, it needs to be updated
|
||||||
if arg not in current_config:
|
if arg not in current_config:
|
||||||
update_config = True
|
update_config = True
|
||||||
|
@ -568,17 +578,20 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
break
|
break
|
||||||
|
|
||||||
if update_config:
|
if update_config:
|
||||||
getattr(proxmox_node, VZ_TYPE)(vmid).config.put(vmid=vmid, node=node, **kwargs)
|
getattr(proxmox_node, VZ_TYPE)(vmid).config.put(
|
||||||
|
vmid=vmid, node=node, **kwargs
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.module.exit_json(changed=False, msg="Container config is already up to date")
|
self.module.exit_json(
|
||||||
|
changed=False, msg="Container config is already up to date"
|
||||||
|
)
|
||||||
|
|
||||||
def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout, clone, **kwargs):
|
def create_instance(
|
||||||
|
self, vmid, node, disk, storage, cpus, memory, swap, timeout, clone, **kwargs
|
||||||
|
):
|
||||||
|
|
||||||
# Version limited features
|
# Version limited features
|
||||||
minimum_version = {
|
minimum_version = {"tags": "6.1", "timezone": "6.3"}
|
||||||
'tags': '6.1',
|
|
||||||
'timezone': '6.3'
|
|
||||||
}
|
|
||||||
proxmox_node = self.proxmox_api.nodes(node)
|
proxmox_node = self.proxmox_api.nodes(node)
|
||||||
|
|
||||||
# Remove all empty kwarg entries
|
# Remove all empty kwarg entries
|
||||||
|
@ -589,40 +602,47 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
# Fail on unsupported features
|
# Fail on unsupported features
|
||||||
for option, version in minimum_version.items():
|
for option, version in minimum_version.items():
|
||||||
if pve_version < LooseVersion(version) and option in kwargs:
|
if pve_version < LooseVersion(version) and option in kwargs:
|
||||||
self.module.fail_json(changed=False, msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_version}".
|
self.module.fail_json(
|
||||||
format(option=option, version=version, pve_version=pve_version))
|
changed=False,
|
||||||
|
msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_version}".format(
|
||||||
|
option=option, version=version, pve_version=pve_version
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
if VZ_TYPE == 'lxc':
|
if VZ_TYPE == "lxc":
|
||||||
kwargs['cpulimit'] = cpus
|
kwargs["cpulimit"] = cpus
|
||||||
kwargs['rootfs'] = disk
|
kwargs["rootfs"] = disk
|
||||||
if 'netif' in kwargs:
|
if "netif" in kwargs:
|
||||||
kwargs.update(kwargs['netif'])
|
kwargs.update(kwargs["netif"])
|
||||||
del kwargs['netif']
|
del kwargs["netif"]
|
||||||
if 'mounts' in kwargs:
|
if "mounts" in kwargs:
|
||||||
kwargs.update(kwargs['mounts'])
|
kwargs.update(kwargs["mounts"])
|
||||||
del kwargs['mounts']
|
del kwargs["mounts"]
|
||||||
if 'pubkey' in kwargs:
|
if "pubkey" in kwargs:
|
||||||
if self.version() >= LooseVersion('4.2'):
|
if self.version() >= LooseVersion("4.2"):
|
||||||
kwargs['ssh-public-keys'] = kwargs['pubkey']
|
kwargs["ssh-public-keys"] = kwargs["pubkey"]
|
||||||
del kwargs['pubkey']
|
del kwargs["pubkey"]
|
||||||
else:
|
else:
|
||||||
kwargs['cpus'] = cpus
|
kwargs["cpus"] = cpus
|
||||||
kwargs['disk'] = disk
|
kwargs["disk"] = disk
|
||||||
|
|
||||||
# LXC tags are expected to be valid and presented as a comma/semi-colon delimited string
|
# LXC tags are expected to be valid and presented as a comma/semi-colon delimited string
|
||||||
if 'tags' in kwargs:
|
if "tags" in kwargs:
|
||||||
re_tag = re.compile(r'^[a-z0-9_][a-z0-9_\-\+\.]*$')
|
re_tag = re.compile(r"^[a-z0-9_][a-z0-9_\-\+\.]*$")
|
||||||
for tag in kwargs['tags']:
|
for tag in kwargs["tags"]:
|
||||||
if not re_tag.match(tag):
|
if not re_tag.match(tag):
|
||||||
self.module.fail_json(msg='%s is not a valid tag' % tag)
|
self.module.fail_json(msg="%s is not a valid tag" % tag)
|
||||||
kwargs['tags'] = ",".join(kwargs['tags'])
|
kwargs["tags"] = ",".join(kwargs["tags"])
|
||||||
|
|
||||||
if kwargs.get('ostype') == 'auto':
|
if kwargs.get("ostype") == "auto":
|
||||||
kwargs.pop('ostype')
|
kwargs.pop("ostype")
|
||||||
|
|
||||||
if clone is not None:
|
if clone is not None:
|
||||||
if VZ_TYPE != 'lxc':
|
if VZ_TYPE != "lxc":
|
||||||
self.module.fail_json(changed=False, msg="Clone operator is only supported for LXC enabled proxmox clusters.")
|
self.module.fail_json(
|
||||||
|
changed=False,
|
||||||
|
msg="Clone operator is only supported for LXC enabled proxmox clusters.",
|
||||||
|
)
|
||||||
|
|
||||||
clone_is_template = self.is_template_container(node, clone)
|
clone_is_template = self.is_template_container(node, clone)
|
||||||
|
|
||||||
|
@ -630,96 +650,136 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
create_full_copy = not clone_is_template
|
create_full_copy = not clone_is_template
|
||||||
|
|
||||||
# Only accept parameters that are compatible with the clone endpoint.
|
# Only accept parameters that are compatible with the clone endpoint.
|
||||||
valid_clone_parameters = ['hostname', 'pool', 'description']
|
valid_clone_parameters = ["hostname", "pool", "description"]
|
||||||
if self.module.params['storage'] is not None and clone_is_template:
|
if self.module.params["storage"] is not None and clone_is_template:
|
||||||
# Cloning a template, so create a full copy instead of a linked copy
|
# Cloning a template, so create a full copy instead of a linked copy
|
||||||
create_full_copy = True
|
create_full_copy = True
|
||||||
elif self.module.params['storage'] is None and not clone_is_template:
|
elif self.module.params["storage"] is None and not clone_is_template:
|
||||||
# Not cloning a template, but also no defined storage. This isn't possible.
|
# Not cloning a template, but also no defined storage. This isn't possible.
|
||||||
self.module.fail_json(changed=False, msg="Cloned container is not a template, storage needs to be specified.")
|
self.module.fail_json(
|
||||||
|
changed=False,
|
||||||
|
msg="Cloned container is not a template, storage needs to be specified.",
|
||||||
|
)
|
||||||
|
|
||||||
if self.module.params['clone_type'] == 'linked':
|
if self.module.params["clone_type"] == "linked":
|
||||||
if not clone_is_template:
|
if not clone_is_template:
|
||||||
self.module.fail_json(changed=False, msg="'linked' clone type is specified, but cloned container is not a template container.")
|
self.module.fail_json(
|
||||||
|
changed=False,
|
||||||
|
msg="'linked' clone type is specified, but cloned container is not a template container.",
|
||||||
|
)
|
||||||
# Don't need to do more, by default create_full_copy is set to false already
|
# Don't need to do more, by default create_full_copy is set to false already
|
||||||
elif self.module.params['clone_type'] == 'opportunistic':
|
elif self.module.params["clone_type"] == "opportunistic":
|
||||||
if not clone_is_template:
|
if not clone_is_template:
|
||||||
# Cloned container is not a template, so we need our 'storage' parameter
|
# Cloned container is not a template, so we need our 'storage' parameter
|
||||||
valid_clone_parameters.append('storage')
|
valid_clone_parameters.append("storage")
|
||||||
elif self.module.params['clone_type'] == 'full':
|
elif self.module.params["clone_type"] == "full":
|
||||||
create_full_copy = True
|
create_full_copy = True
|
||||||
valid_clone_parameters.append('storage')
|
valid_clone_parameters.append("storage")
|
||||||
|
|
||||||
clone_parameters = {}
|
clone_parameters = {}
|
||||||
|
|
||||||
if create_full_copy:
|
if create_full_copy:
|
||||||
clone_parameters['full'] = '1'
|
clone_parameters["full"] = "1"
|
||||||
else:
|
else:
|
||||||
clone_parameters['full'] = '0'
|
clone_parameters["full"] = "0"
|
||||||
for param in valid_clone_parameters:
|
for param in valid_clone_parameters:
|
||||||
if self.module.params[param] is not None:
|
if self.module.params[param] is not None:
|
||||||
clone_parameters[param] = self.module.params[param]
|
clone_parameters[param] = self.module.params[param]
|
||||||
|
|
||||||
taskid = getattr(proxmox_node, VZ_TYPE)(clone).clone.post(newid=vmid, **clone_parameters)
|
taskid = getattr(proxmox_node, VZ_TYPE)(clone).clone.post(
|
||||||
|
newid=vmid, **clone_parameters
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
taskid = getattr(proxmox_node, VZ_TYPE).create(vmid=vmid, storage=storage, memory=memory, swap=swap, **kwargs)
|
taskid = getattr(proxmox_node, VZ_TYPE).create(
|
||||||
|
vmid=vmid, storage=storage, memory=memory, swap=swap, **kwargs
|
||||||
|
)
|
||||||
|
|
||||||
while timeout:
|
while timeout:
|
||||||
if self.api_task_ok(node, taskid):
|
if self.api_task_ok(node, taskid):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(vmid=vmid, node=node, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' %
|
self.module.fail_json(
|
||||||
proxmox_node.tasks(taskid).log.get()[:1])
|
vmid=vmid,
|
||||||
|
node=node,
|
||||||
|
msg="Reached timeout while waiting for creating VM. Last line in task before timeout: %s"
|
||||||
|
% proxmox_node.tasks(taskid).log.get()[:1],
|
||||||
|
)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def start_instance(self, vm, vmid, timeout):
|
def start_instance(self, vm, vmid, timeout):
|
||||||
taskid = getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.start.post()
|
taskid = getattr(self.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
|
vmid
|
||||||
|
).status.start.post()
|
||||||
while timeout:
|
while timeout:
|
||||||
if self.api_task_ok(vm['node'], taskid):
|
if self.api_task_ok(vm["node"], taskid):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' %
|
self.module.fail_json(
|
||||||
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
vmid=vmid,
|
||||||
|
taskid=taskid,
|
||||||
|
msg="Reached timeout while waiting for starting VM. Last line in task before timeout: %s"
|
||||||
|
% self.proxmox_api.nodes(vm["node"]).tasks(taskid).log.get()[:1],
|
||||||
|
)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def stop_instance(self, vm, vmid, timeout, force):
|
def stop_instance(self, vm, vmid, timeout, force):
|
||||||
if force:
|
if force:
|
||||||
taskid = getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.shutdown.post(forceStop=1)
|
taskid = getattr(self.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
|
vmid
|
||||||
|
).status.shutdown.post(forceStop=1)
|
||||||
else:
|
else:
|
||||||
taskid = getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.shutdown.post()
|
taskid = getattr(self.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
|
vmid
|
||||||
|
).status.shutdown.post()
|
||||||
while timeout:
|
while timeout:
|
||||||
if self.api_task_ok(vm['node'], taskid):
|
if self.api_task_ok(vm["node"], taskid):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' %
|
self.module.fail_json(
|
||||||
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
vmid=vmid,
|
||||||
|
taskid=taskid,
|
||||||
|
msg="Reached timeout while waiting for stopping VM. Last line in task before timeout: %s"
|
||||||
|
% self.proxmox_api.nodes(vm["node"]).tasks(taskid).log.get()[:1],
|
||||||
|
)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def convert_to_template(self, vm, vmid, timeout, force):
|
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:
|
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)
|
self.stop_instance(vm, vmid, timeout, force)
|
||||||
# not sure why, but templating a container doesn't return a taskid
|
# not sure why, but templating a container doesn't return a taskid
|
||||||
getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).template.post()
|
getattr(self.proxmox_api.nodes(vm["node"]), VZ_TYPE)(vmid).template.post()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def umount_instance(self, vm, vmid, timeout):
|
def umount_instance(self, vm, vmid, timeout):
|
||||||
taskid = getattr(self.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.umount.post()
|
taskid = getattr(self.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
|
vmid
|
||||||
|
).status.umount.post()
|
||||||
while timeout:
|
while timeout:
|
||||||
if self.api_task_ok(vm['node'], taskid):
|
if self.api_task_ok(vm["node"], taskid):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' %
|
self.module.fail_json(
|
||||||
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
vmid=vmid,
|
||||||
|
taskid=taskid,
|
||||||
|
msg="Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s"
|
||||||
|
% self.proxmox_api.nodes(vm["node"]).tasks(taskid).log.get()[:1],
|
||||||
|
)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return False
|
return False
|
||||||
|
@ -728,96 +788,131 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
def main():
|
def main():
|
||||||
module_args = proxmox_auth_argument_spec()
|
module_args = proxmox_auth_argument_spec()
|
||||||
proxmox_args = dict(
|
proxmox_args = dict(
|
||||||
vmid=dict(type='int', required=False),
|
vmid=dict(type="int", required=False),
|
||||||
node=dict(),
|
node=dict(),
|
||||||
pool=dict(),
|
pool=dict(),
|
||||||
password=dict(no_log=True),
|
password=dict(no_log=True),
|
||||||
hostname=dict(),
|
hostname=dict(),
|
||||||
ostemplate=dict(),
|
ostemplate=dict(),
|
||||||
disk=dict(type='str'),
|
disk=dict(type="str"),
|
||||||
cores=dict(type='int'),
|
cores=dict(type="int"),
|
||||||
cpus=dict(type='int'),
|
cpus=dict(type="int"),
|
||||||
memory=dict(type='int'),
|
memory=dict(type="int"),
|
||||||
swap=dict(type='int'),
|
swap=dict(type="int"),
|
||||||
netif=dict(type='dict'),
|
netif=dict(type="dict"),
|
||||||
mounts=dict(type='dict'),
|
mounts=dict(type="dict"),
|
||||||
ip_address=dict(),
|
ip_address=dict(),
|
||||||
ostype=dict(default='auto', choices=[
|
ostype=dict(
|
||||||
'auto', 'debian', 'devuan', 'ubuntu', 'centos', 'fedora', 'opensuse', 'archlinux', 'alpine', 'gentoo', 'nixos', 'unmanaged'
|
default="auto",
|
||||||
]),
|
choices=[
|
||||||
onboot=dict(type='bool'),
|
"auto",
|
||||||
features=dict(type='list', elements='str'),
|
"debian",
|
||||||
startup=dict(type='list', elements='str'),
|
"devuan",
|
||||||
storage=dict(default='local'),
|
"ubuntu",
|
||||||
cpuunits=dict(type='int'),
|
"centos",
|
||||||
|
"fedora",
|
||||||
|
"opensuse",
|
||||||
|
"archlinux",
|
||||||
|
"alpine",
|
||||||
|
"gentoo",
|
||||||
|
"nixos",
|
||||||
|
"unmanaged",
|
||||||
|
],
|
||||||
|
),
|
||||||
|
onboot=dict(type="bool"),
|
||||||
|
features=dict(type="list", elements="str"),
|
||||||
|
startup=dict(type="list", elements="str"),
|
||||||
|
storage=dict(default="local"),
|
||||||
|
cpuunits=dict(type="int"),
|
||||||
nameserver=dict(),
|
nameserver=dict(),
|
||||||
searchdomain=dict(),
|
searchdomain=dict(),
|
||||||
timeout=dict(type='int', default=30),
|
timeout=dict(type="int", default=30),
|
||||||
update=dict(type='bool', default=False),
|
update=dict(type="bool", default=False),
|
||||||
force=dict(type='bool', default=False),
|
force=dict(type="bool", default=False),
|
||||||
purge=dict(type='bool', default=False),
|
purge=dict(type="bool", default=False),
|
||||||
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'template']),
|
state=dict(
|
||||||
pubkey=dict(type='str'),
|
default="present",
|
||||||
unprivileged=dict(type='bool', default=True),
|
choices=[
|
||||||
description=dict(type='str'),
|
"present",
|
||||||
hookscript=dict(type='str'),
|
"absent",
|
||||||
timezone=dict(type='str'),
|
"stopped",
|
||||||
clone=dict(type='int'),
|
"started",
|
||||||
clone_type=dict(default='opportunistic', choices=['full', 'linked', 'opportunistic']),
|
"restarted",
|
||||||
tags=dict(type='list', elements='str')
|
"template",
|
||||||
|
],
|
||||||
|
),
|
||||||
|
pubkey=dict(type="str"),
|
||||||
|
unprivileged=dict(type="bool", default=True),
|
||||||
|
description=dict(type="str"),
|
||||||
|
hookscript=dict(type="str"),
|
||||||
|
timezone=dict(type="str"),
|
||||||
|
clone=dict(type="int"),
|
||||||
|
clone_type=dict(
|
||||||
|
default="opportunistic", choices=["full", "linked", "opportunistic"]
|
||||||
|
),
|
||||||
|
tags=dict(type="list", elements="str"),
|
||||||
)
|
)
|
||||||
module_args.update(proxmox_args)
|
module_args.update(proxmox_args)
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=module_args,
|
argument_spec=module_args,
|
||||||
required_if=[
|
required_if=[
|
||||||
('state', 'present', ['node', 'hostname']),
|
("state", "present", ["node", "hostname"]),
|
||||||
# Require one of clone, ostemplate, or update. Together with mutually_exclusive this ensures that we
|
# Require one of clone, ostemplate, or update. Together with mutually_exclusive this ensures that we
|
||||||
# either clone a container or create a new one from a template file.
|
# either clone a container or create a new one from a template file.
|
||||||
('state', 'present', ('clone', 'ostemplate', 'update'), True),
|
("state", "present", ("clone", "ostemplate", "update"), True),
|
||||||
],
|
],
|
||||||
required_together=[
|
required_together=[("api_token_id", "api_token_secret")],
|
||||||
('api_token_id', 'api_token_secret')
|
required_one_of=[("api_password", "api_token_id")],
|
||||||
],
|
mutually_exclusive=[
|
||||||
required_one_of=[('api_password', 'api_token_id')],
|
("clone", "ostemplate", "update")
|
||||||
mutually_exclusive=[('clone', 'ostemplate', 'update')], # Creating a new container is done either by cloning an existing one, or based on a template.
|
], # Creating a new container is done either by cloning an existing one, or based on a template.
|
||||||
)
|
)
|
||||||
|
|
||||||
proxmox = ProxmoxLxcAnsible(module)
|
proxmox = ProxmoxLxcAnsible(module)
|
||||||
|
|
||||||
global VZ_TYPE
|
global VZ_TYPE
|
||||||
VZ_TYPE = 'openvz' if proxmox.version() < LooseVersion('4.0') else 'lxc'
|
VZ_TYPE = "openvz" if proxmox.version() < LooseVersion("4.0") else "lxc"
|
||||||
|
|
||||||
state = module.params['state']
|
state = module.params["state"]
|
||||||
vmid = module.params['vmid']
|
vmid = module.params["vmid"]
|
||||||
node = module.params['node']
|
node = module.params["node"]
|
||||||
disk = module.params['disk']
|
disk = module.params["disk"]
|
||||||
cpus = module.params['cpus']
|
cpus = module.params["cpus"]
|
||||||
memory = module.params['memory']
|
memory = module.params["memory"]
|
||||||
swap = module.params['swap']
|
swap = module.params["swap"]
|
||||||
storage = module.params['storage']
|
storage = module.params["storage"]
|
||||||
hostname = module.params['hostname']
|
hostname = module.params["hostname"]
|
||||||
if module.params['ostemplate'] is not None:
|
if module.params["ostemplate"] is not None:
|
||||||
template_store = module.params['ostemplate'].split(":")[0]
|
template_store = module.params["ostemplate"].split(":")[0]
|
||||||
timeout = module.params['timeout']
|
timeout = module.params["timeout"]
|
||||||
clone = module.params['clone']
|
clone = module.params["clone"]
|
||||||
|
|
||||||
# If vmid not set get the Next VM id from ProxmoxAPI
|
# If vmid not set get the Next VM id from ProxmoxAPI
|
||||||
# If hostname is set get the VM id from ProxmoxAPI
|
# If hostname is set get the VM id from ProxmoxAPI
|
||||||
if not vmid and state == 'present':
|
if not vmid and state == "present":
|
||||||
vmid = proxmox.get_nextvmid()
|
vmid = proxmox.get_nextvmid()
|
||||||
elif not vmid and hostname:
|
elif not vmid and hostname:
|
||||||
vmid = proxmox.get_vmid(hostname)
|
vmid = proxmox.get_vmid(hostname)
|
||||||
elif not vmid:
|
elif not vmid:
|
||||||
module.exit_json(changed=False, msg="Vmid could not be fetched for the following action: %s" % state)
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
msg="Vmid could not be fetched for the following action: %s" % state,
|
||||||
|
)
|
||||||
|
|
||||||
# Create a new container
|
# Create a new container
|
||||||
if state == 'present' and clone is None:
|
if state == "present" and clone is None:
|
||||||
try:
|
try:
|
||||||
if proxmox.get_vm(vmid, ignore_missing=True):
|
if proxmox.get_vm(vmid, ignore_missing=True):
|
||||||
if module.params["update"]:
|
if module.params["update"]:
|
||||||
try:
|
try:
|
||||||
proxmox.update_config(vmid, node, disk, cpus, memory, swap,
|
proxmox.update_config(
|
||||||
|
vmid,
|
||||||
|
node,
|
||||||
|
disk,
|
||||||
|
cpus,
|
||||||
|
memory,
|
||||||
|
swap,
|
||||||
cores=module.params["cores"],
|
cores=module.params["cores"],
|
||||||
hostname=module.params["hostname"],
|
hostname=module.params["hostname"],
|
||||||
netif=module.params["netif"],
|
netif=module.params["netif"],
|
||||||
|
@ -827,16 +922,21 @@ def main():
|
||||||
cpuunits=module.params["cpuunits"],
|
cpuunits=module.params["cpuunits"],
|
||||||
nameserver=module.params["nameserver"],
|
nameserver=module.params["nameserver"],
|
||||||
searchdomain=module.params["searchdomain"],
|
searchdomain=module.params["searchdomain"],
|
||||||
features=",".join(module.params["features"])
|
features=(
|
||||||
|
",".join(module.params["features"])
|
||||||
if module.params["features"] is not None
|
if module.params["features"] is not None
|
||||||
else None,
|
else None
|
||||||
startup=",".join(module.params["startup"])
|
),
|
||||||
|
startup=(
|
||||||
|
",".join(module.params["startup"])
|
||||||
if module.params["startup"] is not None
|
if module.params["startup"] is not None
|
||||||
else None,
|
else None
|
||||||
|
),
|
||||||
description=module.params["description"],
|
description=module.params["description"],
|
||||||
hookscript=module.params["hookscript"],
|
hookscript=module.params["hookscript"],
|
||||||
timezone=module.params["timezone"],
|
timezone=module.params["timezone"],
|
||||||
tags=module.params["tags"])
|
tags=module.params["tags"],
|
||||||
|
)
|
||||||
module.exit_json(
|
module.exit_json(
|
||||||
changed=True,
|
changed=True,
|
||||||
vmid=vmid,
|
vmid=vmid,
|
||||||
|
@ -855,159 +955,313 @@ def main():
|
||||||
msg="VM with vmid = %s is already exists" % vmid,
|
msg="VM with vmid = %s is already exists" % vmid,
|
||||||
)
|
)
|
||||||
# If no vmid was passed, there cannot be another VM named 'hostname'
|
# If no vmid was passed, there cannot be another VM named 'hostname'
|
||||||
if (not module.params['vmid'] and
|
if (
|
||||||
proxmox.get_vmid(hostname, ignore_missing=True) and
|
not module.params["vmid"]
|
||||||
not module.params['force']):
|
and proxmox.get_vmid(hostname, ignore_missing=True)
|
||||||
|
and not module.params["force"]
|
||||||
|
):
|
||||||
vmid = proxmox.get_vmid(hostname)
|
vmid = proxmox.get_vmid(hostname)
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid))
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="VM with hostname %s already exists and has ID number %s"
|
||||||
|
% (hostname, vmid),
|
||||||
|
)
|
||||||
elif not proxmox.get_node(node):
|
elif not proxmox.get_node(node):
|
||||||
module.fail_json(vmid=vmid, msg="node '%s' not exists in cluster" % node)
|
module.fail_json(
|
||||||
elif not proxmox.content_check(node, module.params['ostemplate'], template_store):
|
vmid=vmid, msg="node '%s' not exists in cluster" % node
|
||||||
module.fail_json(vmid=vmid, msg="ostemplate '%s' not exists on node %s and storage %s"
|
)
|
||||||
% (module.params['ostemplate'], node, template_store))
|
elif not proxmox.content_check(
|
||||||
|
node, module.params["ostemplate"], template_store
|
||||||
|
):
|
||||||
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="ostemplate '%s' not exists on node %s and storage %s"
|
||||||
|
% (module.params["ostemplate"], node, template_store),
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e))
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(
|
||||||
|
VZ_TYPE=VZ_TYPE, vmid=vmid, e=e
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone,
|
proxmox.create_instance(
|
||||||
cores=module.params['cores'],
|
vmid,
|
||||||
pool=module.params['pool'],
|
node,
|
||||||
password=module.params['password'],
|
disk,
|
||||||
hostname=module.params['hostname'],
|
storage,
|
||||||
ostemplate=module.params['ostemplate'],
|
cpus,
|
||||||
netif=module.params['netif'],
|
memory,
|
||||||
mounts=module.params['mounts'],
|
swap,
|
||||||
ostype=module.params['ostype'],
|
timeout,
|
||||||
ip_address=module.params['ip_address'],
|
clone,
|
||||||
onboot=ansible_to_proxmox_bool(module.params['onboot']),
|
cores=module.params["cores"],
|
||||||
cpuunits=module.params['cpuunits'],
|
pool=module.params["pool"],
|
||||||
nameserver=module.params['nameserver'],
|
password=module.params["password"],
|
||||||
searchdomain=module.params['searchdomain'],
|
hostname=module.params["hostname"],
|
||||||
force=ansible_to_proxmox_bool(module.params['force']),
|
ostemplate=module.params["ostemplate"],
|
||||||
pubkey=module.params['pubkey'],
|
netif=module.params["netif"],
|
||||||
features=",".join(module.params['features']) if module.params['features'] is not None else None,
|
mounts=module.params["mounts"],
|
||||||
startup=",".join(module.params['startup']) if module.params['startup'] is not None else None,
|
ostype=module.params["ostype"],
|
||||||
unprivileged=ansible_to_proxmox_bool(module.params['unprivileged']),
|
ip_address=module.params["ip_address"],
|
||||||
description=module.params['description'],
|
onboot=ansible_to_proxmox_bool(module.params["onboot"]),
|
||||||
hookscript=module.params['hookscript'],
|
cpuunits=module.params["cpuunits"],
|
||||||
timezone=module.params['timezone'],
|
nameserver=module.params["nameserver"],
|
||||||
tags=module.params['tags'])
|
searchdomain=module.params["searchdomain"],
|
||||||
|
force=ansible_to_proxmox_bool(module.params["force"]),
|
||||||
|
pubkey=module.params["pubkey"],
|
||||||
|
features=(
|
||||||
|
",".join(module.params["features"])
|
||||||
|
if module.params["features"] is not None
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
startup=(
|
||||||
|
",".join(module.params["startup"])
|
||||||
|
if module.params["startup"] is not None
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
unprivileged=ansible_to_proxmox_bool(module.params["unprivileged"]),
|
||||||
|
description=module.params["description"],
|
||||||
|
hookscript=module.params["hookscript"],
|
||||||
|
timezone=module.params["timezone"],
|
||||||
|
tags=module.params["tags"],
|
||||||
|
)
|
||||||
|
|
||||||
module.exit_json(changed=True, vmid=vmid, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']))
|
module.exit_json(
|
||||||
|
changed=True,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="Deployed VM %s from template %s"
|
||||||
|
% (vmid, module.params["ostemplate"]),
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e))
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="Creation of %s VM %s failed with exception: %s"
|
||||||
|
% (VZ_TYPE, vmid, e),
|
||||||
|
)
|
||||||
|
|
||||||
# Clone a container
|
# Clone a container
|
||||||
elif state == 'present' and clone is not None:
|
elif state == "present" and clone is not None:
|
||||||
try:
|
try:
|
||||||
if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']:
|
if proxmox.get_vm(vmid, ignore_missing=True) and not module.params["force"]:
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid)
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="VM with vmid = %s is already exists" % vmid,
|
||||||
|
)
|
||||||
# If no vmid was passed, there cannot be another VM named 'hostname'
|
# If no vmid was passed, there cannot be another VM named 'hostname'
|
||||||
if (not module.params['vmid'] and
|
if (
|
||||||
proxmox.get_vmid(hostname, ignore_missing=True) and
|
not module.params["vmid"]
|
||||||
not module.params['force']):
|
and proxmox.get_vmid(hostname, ignore_missing=True)
|
||||||
|
and not module.params["force"]
|
||||||
|
):
|
||||||
vmid = proxmox.get_vmid(hostname)
|
vmid = proxmox.get_vmid(hostname)
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid))
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="VM with hostname %s already exists and has ID number %s"
|
||||||
|
% (hostname, vmid),
|
||||||
|
)
|
||||||
if not proxmox.get_vm(clone, ignore_missing=True):
|
if not proxmox.get_vm(clone, ignore_missing=True):
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="Container to be cloned does not exist")
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="Container to be cloned does not exist",
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e))
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(
|
||||||
|
VZ_TYPE=VZ_TYPE, vmid=vmid, e=e
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone)
|
proxmox.create_instance(
|
||||||
|
vmid, node, disk, storage, cpus, memory, swap, timeout, clone
|
||||||
|
)
|
||||||
|
|
||||||
module.exit_json(changed=True, vmid=vmid, msg="Cloned VM %s from %s" % (vmid, clone))
|
module.exit_json(
|
||||||
|
changed=True, vmid=vmid, msg="Cloned VM %s from %s" % (vmid, clone)
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e))
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e),
|
||||||
|
)
|
||||||
|
|
||||||
elif state == 'started':
|
elif state == "started":
|
||||||
try:
|
try:
|
||||||
vm = proxmox.get_vm(vmid)
|
vm = proxmox.get_vm(vmid)
|
||||||
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running':
|
if (
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid)
|
getattr(proxmox.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
|
vmid
|
||||||
|
).status.current.get()["status"]
|
||||||
|
== "running"
|
||||||
|
):
|
||||||
|
module.exit_json(
|
||||||
|
changed=False, vmid=vmid, msg="VM %s is already running" % vmid
|
||||||
|
)
|
||||||
|
|
||||||
if proxmox.start_instance(vm, vmid, timeout):
|
if proxmox.start_instance(vm, vmid, timeout):
|
||||||
module.exit_json(changed=True, vmid=vmid, msg="VM %s started" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s started" % vmid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="starting of VM %s failed with exception: %s" % (vmid, e))
|
module.fail_json(
|
||||||
|
vmid=vmid, msg="starting of VM %s failed with exception: %s" % (vmid, e)
|
||||||
|
)
|
||||||
|
|
||||||
elif state == 'stopped':
|
elif state == "stopped":
|
||||||
try:
|
try:
|
||||||
vm = proxmox.get_vm(vmid)
|
vm = proxmox.get_vm(vmid)
|
||||||
|
|
||||||
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted':
|
if (
|
||||||
if module.params['force']:
|
getattr(proxmox.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
|
vmid
|
||||||
|
).status.current.get()["status"]
|
||||||
|
== "mounted"
|
||||||
|
):
|
||||||
|
if module.params["force"]:
|
||||||
if proxmox.umount_instance(vm, vmid, timeout):
|
if proxmox.umount_instance(vm, vmid, timeout):
|
||||||
module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid)
|
module.exit_json(
|
||||||
|
changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=False, vmid=vmid,
|
module.exit_json(
|
||||||
msg=("VM %s is already shutdown, but mounted. You can use force option to umount it.") % vmid)
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg=(
|
||||||
|
"VM %s is already shutdown, but mounted. You can use force option to umount it."
|
||||||
|
)
|
||||||
|
% vmid,
|
||||||
|
)
|
||||||
|
|
||||||
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped':
|
if (
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM %s is already shutdown" % vmid)
|
getattr(proxmox.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
|
vmid
|
||||||
|
).status.current.get()["status"]
|
||||||
|
== "stopped"
|
||||||
|
):
|
||||||
|
module.exit_json(
|
||||||
|
changed=False, vmid=vmid, msg="VM %s is already shutdown" % vmid
|
||||||
|
)
|
||||||
|
|
||||||
if proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']):
|
if proxmox.stop_instance(vm, vmid, timeout, force=module.params["force"]):
|
||||||
module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid)
|
module.exit_json(
|
||||||
|
changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e))
|
module.fail_json(
|
||||||
|
vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e)
|
||||||
|
)
|
||||||
|
|
||||||
elif state == 'template':
|
elif state == "template":
|
||||||
try:
|
try:
|
||||||
vm = proxmox.get_vm(vmid)
|
vm = proxmox.get_vm(vmid)
|
||||||
|
|
||||||
proxmox.convert_to_template(vm, vmid, timeout, force=module.params['force'])
|
proxmox.convert_to_template(vm, vmid, timeout, force=module.params["force"])
|
||||||
module.exit_json(changed=True, msg="VM %s is converted to template" % vmid)
|
module.exit_json(changed=True, msg="VM %s is converted to template" % vmid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="conversion of VM %s to template failed with exception: %s" % (vmid, e))
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="conversion of VM %s to template failed with exception: %s"
|
||||||
|
% (vmid, e),
|
||||||
|
)
|
||||||
|
|
||||||
elif state == 'restarted':
|
elif state == "restarted":
|
||||||
try:
|
try:
|
||||||
vm = proxmox.get_vm(vmid)
|
vm = proxmox.get_vm(vmid)
|
||||||
|
|
||||||
vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status']
|
vm_status = getattr(proxmox.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
if vm_status in ['stopped', 'mounted']:
|
vmid
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid)
|
).status.current.get()["status"]
|
||||||
|
if vm_status in ["stopped", "mounted"]:
|
||||||
|
module.exit_json(
|
||||||
|
changed=False, vmid=vmid, msg="VM %s is not running" % vmid
|
||||||
|
)
|
||||||
|
|
||||||
if (proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']) and
|
if proxmox.stop_instance(
|
||||||
proxmox.start_instance(vm, vmid, timeout)):
|
vm, vmid, timeout, force=module.params["force"]
|
||||||
module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid)
|
) and proxmox.start_instance(vm, vmid, timeout):
|
||||||
|
module.exit_json(
|
||||||
|
changed=True, vmid=vmid, msg="VM %s is restarted" % vmid
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="restarting of VM %s failed with exception: %s" % (vmid, e))
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="restarting of VM %s failed with exception: %s" % (vmid, e),
|
||||||
|
)
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == "absent":
|
||||||
if not vmid:
|
if not vmid:
|
||||||
module.exit_json(changed=False, vmid=vmid, msg='VM with hostname = %s is already absent' % hostname)
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="VM with hostname = %s is already absent" % hostname,
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
vm = proxmox.get_vm(vmid, ignore_missing=True)
|
vm = proxmox.get_vm(vmid, ignore_missing=True)
|
||||||
if not vm:
|
if not vm:
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM %s does not exist" % vmid)
|
module.exit_json(
|
||||||
|
changed=False, vmid=vmid, msg="VM %s does not exist" % vmid
|
||||||
|
)
|
||||||
|
|
||||||
vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status']
|
vm_status = getattr(proxmox.proxmox_api.nodes(vm["node"]), VZ_TYPE)(
|
||||||
if vm_status == 'running':
|
vmid
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM %s is running. Stop it before deletion." % vmid)
|
).status.current.get()["status"]
|
||||||
|
if vm_status == "running":
|
||||||
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="VM %s is running. Stop it before deletion." % vmid,
|
||||||
|
)
|
||||||
|
|
||||||
if vm_status == 'mounted':
|
if vm_status == "mounted":
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM %s is mounted. Stop it with force option before deletion." % vmid)
|
module.exit_json(
|
||||||
|
changed=False,
|
||||||
|
vmid=vmid,
|
||||||
|
msg="VM %s is mounted. Stop it with force option before deletion."
|
||||||
|
% vmid,
|
||||||
|
)
|
||||||
|
|
||||||
delete_params = {}
|
delete_params = {}
|
||||||
|
|
||||||
if module.params['purge']:
|
if module.params["purge"]:
|
||||||
delete_params['purge'] = 1
|
delete_params["purge"] = 1
|
||||||
|
|
||||||
taskid = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE).delete(vmid, **delete_params)
|
taskid = getattr(proxmox.proxmox_api.nodes(vm["node"]), VZ_TYPE).delete(
|
||||||
|
vmid, **delete_params
|
||||||
|
)
|
||||||
|
|
||||||
while timeout:
|
while timeout:
|
||||||
if proxmox.api_task_ok(vm['node'], taskid):
|
if proxmox.api_task_ok(vm["node"], taskid):
|
||||||
module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s removed" % vmid)
|
module.exit_json(
|
||||||
|
changed=True,
|
||||||
|
vmid=vmid,
|
||||||
|
taskid=taskid,
|
||||||
|
msg="VM %s removed" % vmid,
|
||||||
|
)
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s'
|
module.fail_json(
|
||||||
% proxmox.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
vmid=vmid,
|
||||||
|
taskid=taskid,
|
||||||
|
msg="Reached timeout while waiting for removing VM. Last line in task before timeout: %s"
|
||||||
|
% proxmox.proxmox_api.nodes(vm["node"])
|
||||||
|
.tasks(taskid)
|
||||||
|
.log.get()[:1],
|
||||||
|
)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e)))
|
module.fail_json(
|
||||||
|
vmid=vmid,
|
||||||
|
msg="deletion of VM %s failed with exception: %s"
|
||||||
|
% (vmid, to_native(e)),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue