Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow update of proxmox container configuration #7540

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7540-proxmox-update config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- proxmox - adds ``update`` parameter, allowing update of an already existing containers configuration (https://github.com/ansible-collections/community.general/pull/7540).
110 changes: 105 additions & 5 deletions plugins/modules/proxmox.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@
- timeout for operations
type: int
default: 30
update:
description:
- If V(true), the container will be updated with new values.
type: bool
default: false
version_added: 8.1.0
force:
description:
- Forcing operations.
Expand Down Expand Up @@ -384,6 +390,16 @@
hostname: clone.example.org
storage: local

- name: Update container configuration
community.general.proxmox:
vmid: 100
node: uk-mc02
api_user: root@pam
api_password: 1q2w3e
api_host: node1
netif: '{"net0":"name=eth0,gw=192.168.0.1,ip=192.168.0.3/24,bridge=vmbr0"}'
update: true

- name: Start container
community.general.proxmox:
vmid: 100
Expand Down Expand Up @@ -478,6 +494,53 @@ def is_template_container(self, node, vmid):
config = getattr(proxmox_node, VZ_TYPE)(vmid).config.get()
return config.get('template', False)

def update_config(self, vmid, node, disk, cpus, memory, swap, **kwargs):
if VZ_TYPE != "lxc":
self.module.fail_json(
changed=False,
msg="Updating configuration is only supported for LXC enabled proxmox clusters.",
)

# Version limited features
minimum_version = {"tags": "6.1", "timezone": "6.3"}
proxmox_node = self.proxmox_api.nodes(node)

pve_version = self.version()

# Fail on unsupported features
for option, version in minimum_version.items():
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}".format(
option=option, version=version, pve_version=pve_version
),
)

# Remove all empty kwarg entries
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)

kwargs["cpulimit"] = cpus
kwargs["rootfs"] = disk
if "netif" in kwargs:
kwargs.update(kwargs["netif"])
del kwargs["netif"]
if "mounts" in kwargs:
kwargs.update(kwargs["mounts"])
del kwargs["mounts"]

# LXC tags are expected to be valid and presented as a comma/semi-colon delimited string
if "tags" in kwargs:
re_tag = re.compile(r"^[a-z0-9_][a-z0-9_\-\+\.]*$")
for tag in kwargs["tags"]:
if not re_tag.match(tag):
self.module.fail_json(msg="%s is not a valid tag" % tag)
kwargs["tags"] = ",".join(kwargs["tags"])

getattr(proxmox_node, VZ_TYPE)(vmid).config.put(
vmid=vmid, node=node, memory=memory, swap=swap, **kwargs
)

def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout, clone, **kwargs):

# Version limited features
Expand Down Expand Up @@ -658,6 +721,7 @@ def main():
nameserver=dict(),
searchdomain=dict(),
timeout=dict(type='int', default=30),
update=dict(type='bool', default=False),
force=dict(type='bool', default=False),
purge=dict(type='bool', default=False),
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'template']),
Expand All @@ -678,14 +742,15 @@ def main():
argument_spec=module_args,
required_if=[
('state', 'present', ['node', 'hostname']),
('state', 'present', ('clone', 'ostemplate'), True), # Require one of clone and ostemplate. Together with mutually_exclusive this ensures that we
# either clone a container or create a new one from a template file.
# 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.
('state', 'present', ('clone', 'ostemplate', 'update'), True),
],
required_together=[
('api_token_id', 'api_token_secret')
],
required_one_of=[('api_password', 'api_token_id')],
mutually_exclusive=[('clone', 'ostemplate')], # Creating a new container is done either by cloning an existing one, or based on a template.
mutually_exclusive=[('clone', 'ostemplate', 'update')], # Creating a new container is done either by cloning an existing one, or based on a template.
)

proxmox = ProxmoxLxcAnsible(module)
Expand Down Expand Up @@ -733,8 +798,43 @@ def main():
# Create a new container
if state == 'present' and clone is None:
try:
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)
if proxmox.get_vm(vmid, ignore_missing=True):
if module.params["update"]:
try:
proxmox.update_config(vmid, node, disk, cpus, memory, swap,
cores=module.params["cores"],
hostname=module.params["hostname"],
netif=module.params["netif"],
mounts=module.params["mounts"],
ip_address=module.params["ip_address"],
onboot=ansible_to_proxmox_bool(module.params["onboot"]),
cpuunits=module.params["cpuunits"],
nameserver=module.params["nameserver"],
searchdomain=module.params["searchdomain"],
features=",".join(module.params["features"])
if module.params["features"] is not None
else None,
description=module.params["description"],
hookscript=module.params["hookscript"],
timezone=module.params["timezone"],
tags=module.params["tags"])
module.exit_json(
changed=True,
etrombly marked this conversation as resolved.
Show resolved Hide resolved
vmid=vmid,
msg="Configured VM %s" % (vmid),
)
except Exception as e:
module.fail_json(
vmid=vmid,
msg="Configuration of %s VM %s failed with exception: %s"
% (VZ_TYPE, vmid, e),
)
if not module.params["force"]:
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 (not module.params['vmid'] and
proxmox.get_vmid(hostname, ignore_missing=True) and
Expand Down