From ca93145e7610cc955cd4560b221156c82edaddcb Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 25 Apr 2022 22:32:46 +0200 Subject: [PATCH] Parse lxc key from api data for lxc containers (#4555) (#4574) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Parse lxc key from api data for lxc containers When configuring containers in the `/etc/pve/lxc/` file, the API adds a 'lxc' key that caused the plugin to crash as it tried to split a list on ','. This commit introduces logic to convert the list of lists in the returned data to a dict as with the other keys. ``` 'lxc': [['lxc.apparmor.profile', 'unconfined'], ['lxc.cgroup.devices.allow', 'a']] ``` becomes ``` "proxmox_lxc": { "apparmor.profile": "unconfined", "cap.drop": "", "cgroup.devices.allow": "a" } ``` * Add changelog fragment Co-authored-by: Felix Fontein Co-authored-by: Philippe Pepos Petitclerc Co-authored-by: Felix Fontein (cherry picked from commit 346bfba9c596c8343db383afabd37f85dd002d62) Co-authored-by: Philippe Pépos-Petitclerc --- changelogs/fragments/4555-proxmox-lxc-key.yml | 4 ++++ plugins/inventory/proxmox.py | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/4555-proxmox-lxc-key.yml diff --git a/changelogs/fragments/4555-proxmox-lxc-key.yml b/changelogs/fragments/4555-proxmox-lxc-key.yml new file mode 100644 index 0000000000..5f598d3e07 --- /dev/null +++ b/changelogs/fragments/4555-proxmox-lxc-key.yml @@ -0,0 +1,4 @@ +bugfixes: + - proxmox inventory plugin - fix error when parsing container with LXC configs (https://github.com/ansible-collections/community.general/issues/4472, https://github.com/ansible-collections/community.general/pull/4472). +minor_changes: + - proxmox inventory plugin - parse LXC configs returned by the proxmox API (https://github.com/ansible-collections/community.general/pull/4472). diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 8dcd5260be..e0734b33e3 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -3,6 +3,7 @@ # Copyright (c) 2018 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) + __metaclass__ = type DOCUMENTATION = ''' @@ -206,6 +207,7 @@ from ansible.module_utils.common._collections_compat import MutableMapping from ansible.errors import AnsibleError from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable from ansible.module_utils.common.text.converters import to_native +from ansible.module_utils.six import string_types from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.utils.display import Display from ansible.template import Templar @@ -405,7 +407,16 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): agent_iface_key = self.to_safe('%s%s' % (key, "_interfaces")) properties[agent_iface_key] = agent_iface_value - if config not in plaintext_configs and not isinstance(value, int) and all("=" in v for v in value.split(",")): + if config == 'lxc': + out_val = {} + for k, v in value: + if k.startswith('lxc.'): + k = k[len('lxc.'):] + out_val[k] = v + value = out_val + + if config not in plaintext_configs and isinstance(value, string_types) \ + and all("=" in v for v in value.split(",")): # split off strings with commas to a dict # skip over any keys that cannot be processed try: