From 421ccd5dc99c9fed441a767e6371476d613a37a0 Mon Sep 17 00:00:00 2001 From: monkz Date: Wed, 16 Mar 2022 13:52:22 +0100 Subject: [PATCH] plugins/inventory/lxd.py: fix listing of containers without os / release (#4351) * plugins/inventory/lxd.py: fix listing of containers without os / release In some cases, a container might be present, that was initialized empty, therefore lacking meta information about the os or the release. Test if the data entry is None to avoid calling lower on it. * Update plugins/inventory/lxd.py Co-authored-by: Felix Fontein * Update plugins/inventory/lxd.py Co-authored-by: Felix Fontein * Create 4351-inventory-lxd-handling_metadata_wo_os_and_release.yml * fix yaml readability of changelog fragment * Update changelogs/fragments/4351-inventory-lxd-handling_metadata_wo_os_and_release.yml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein Co-authored-by: Malte Kuhn --- ...-inventory-lxd-handling_metadata_wo_os_and_release.yml | 4 ++++ plugins/inventory/lxd.py | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/4351-inventory-lxd-handling_metadata_wo_os_and_release.yml diff --git a/changelogs/fragments/4351-inventory-lxd-handling_metadata_wo_os_and_release.yml b/changelogs/fragments/4351-inventory-lxd-handling_metadata_wo_os_and_release.yml new file mode 100644 index 0000000000..f7541a7a61 --- /dev/null +++ b/changelogs/fragments/4351-inventory-lxd-handling_metadata_wo_os_and_release.yml @@ -0,0 +1,4 @@ +bugfixes: + - | + lxd inventory plugin - do not crash if OS and release metadata are not present + (https://github.com/ansible-collections/community.general/pull/4351). diff --git a/plugins/inventory/lxd.py b/plugins/inventory/lxd.py index cdb0fa19b1..912638509d 100644 --- a/plugins/inventory/lxd.py +++ b/plugins/inventory/lxd.py @@ -666,9 +666,13 @@ class InventoryModule(BaseInventoryPlugin): # add network informations self.build_inventory_network(instance_name) # add os - self.inventory.set_variable(instance_name, 'ansible_lxd_os', self._get_data_entry('inventory/{0}/os'.format(instance_name)).lower()) + v = self._get_data_entry('inventory/{0}/os'.format(instance_name)) + if v: + self.inventory.set_variable(instance_name, 'ansible_lxd_os', v.lower()) # add release - self.inventory.set_variable(instance_name, 'ansible_lxd_release', self._get_data_entry('inventory/{0}/release'.format(instance_name)).lower()) + v = self._get_data_entry('inventory/{0}/release'.format(instance_name)) + if v: + self.inventory.set_variable(instance_name, 'ansible_lxd_release', v.lower()) # add profile self.inventory.set_variable(instance_name, 'ansible_lxd_profile', self._get_data_entry('inventory/{0}/profile'.format(instance_name))) # add state