mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[PR #5714/2d4ce9f2 backport][stable-6] feat: add tags to proxmox containers (#5745)
feat: add tags to proxmox containers (#5714)
* feat: add tags to proxmox containers
* fix: correct version added
* fix: code style
* feat: changelog fragment
* fix: correct version_added
Co-authored-by: Felix Fontein <felix@fontein.de>
* feat: fail on unsupported params, rather than silently ignoring them
* fix: actually check unsupported feature presence before failing
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 2d4ce9f219
)
Co-authored-by: GuillaumeV-cemea <101114641+GuillaumeV-cemea@users.noreply.github.com>
This commit is contained in:
parent
2ab26db197
commit
50b9855ace
2 changed files with 37 additions and 1 deletions
3
changelogs/fragments/5714-proxmox-lxc-tag-support.yml
Normal file
3
changelogs/fragments/5714-proxmox-lxc-tag-support.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
minor_changes:
|
||||
- proxmox - added new module parameter ``tags`` for use with PVE 7+ (https://github.com/ansible-collections/community.general/pull/5714).
|
|
@ -106,6 +106,14 @@ options:
|
|||
description:
|
||||
- sets DNS search domain for a container
|
||||
type: str
|
||||
tags:
|
||||
description:
|
||||
- List of tags to apply to the container.
|
||||
- Tags must start with C([a-z0-9_]) followed by zero or more of the following characters C([a-z0-9_-+.]).
|
||||
- Tags are only available in Proxmox 7+.
|
||||
type: list
|
||||
elements: str
|
||||
version_added: 6.2.0
|
||||
timeout:
|
||||
description:
|
||||
- timeout for operations
|
||||
|
@ -391,6 +399,7 @@ EXAMPLES = r'''
|
|||
state: absent
|
||||
'''
|
||||
|
||||
import re
|
||||
import time
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
||||
|
@ -415,11 +424,25 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
|||
return config['template']
|
||||
|
||||
def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout, clone, **kwargs):
|
||||
|
||||
# Version limited features
|
||||
minimum_version = {
|
||||
'tags': 7,
|
||||
}
|
||||
proxmox_node = self.proxmox_api.nodes(node)
|
||||
|
||||
# Remove all empty kwarg entries
|
||||
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
|
||||
|
||||
version = self.version()
|
||||
pve_major_version = 3 if version < LooseVersion('4.0') else version.version[0]
|
||||
|
||||
# Fail on unsupported features
|
||||
for option, version in minimum_version.items():
|
||||
if pve_major_version < 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_major_version}".
|
||||
format(option=option, version=version, pve_major_version=pve_major_version))
|
||||
|
||||
if VZ_TYPE == 'lxc':
|
||||
kwargs['cpulimit'] = cpus
|
||||
kwargs['rootfs'] = disk
|
||||
|
@ -437,6 +460,14 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
|||
kwargs['cpus'] = cpus
|
||||
kwargs['disk'] = disk
|
||||
|
||||
# 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'])
|
||||
|
||||
if clone is not None:
|
||||
if VZ_TYPE != 'lxc':
|
||||
self.module.fail_json(changed=False, msg="Clone operator is only supported for LXC enabled proxmox clusters.")
|
||||
|
@ -569,6 +600,7 @@ def main():
|
|||
proxmox_default_behavior=dict(type='str', default='no_defaults', choices=['compatibility', 'no_defaults']),
|
||||
clone=dict(type='int'),
|
||||
clone_type=dict(default='opportunistic', choices=['full', 'linked', 'opportunistic']),
|
||||
tags=dict(type='list', elements='str')
|
||||
)
|
||||
module_args.update(proxmox_args)
|
||||
|
||||
|
@ -674,7 +706,8 @@ def main():
|
|||
features=",".join(module.params['features']) if module.params['features'] is not None else None,
|
||||
unprivileged=ansible_to_proxmox_bool(module.params['unprivileged']),
|
||||
description=module.params['description'],
|
||||
hookscript=module.params['hookscript'])
|
||||
hookscript=module.params['hookscript'],
|
||||
tags=module.params['tags'])
|
||||
|
||||
module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']))
|
||||
except Exception as e:
|
||||
|
|
Loading…
Reference in a new issue