From e9453bbc827c84230f6ae2f0dca137c27e6a052a Mon Sep 17 00:00:00 2001 From: Sagi Shnaidman Date: Wed, 13 Aug 2025 12:34:46 +0300 Subject: [PATCH] Add suggestions Signed-off-by: Sagi Shnaidman --- plugins/inventory/buildah_containers.py | 16 ++-------------- plugins/inventory/podman_containers.py | 25 +++++-------------------- plugins/module_utils/inventory/utils.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 34 deletions(-) create mode 100644 plugins/module_utils/inventory/utils.py diff --git a/plugins/inventory/buildah_containers.py b/plugins/inventory/buildah_containers.py index 962bb75..a406897 100644 --- a/plugins/inventory/buildah_containers.py +++ b/plugins/inventory/buildah_containers.py @@ -46,10 +46,10 @@ import json import fnmatch import shutil import subprocess -import os from ansible.errors import AnsibleParserError from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable +from ansible_collections.containers.podman.plugins.module_utils.inventory.utils import verify_inventory_file class InventoryModule(BaseInventoryPlugin, Cacheable): @@ -58,19 +58,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): def verify_file(self, path: str) -> bool: if not super(InventoryModule, self).verify_file(path): return False - unused, ext = os.path.splitext(path) - if ext not in (".yml", ".yaml"): - return False - try: - with open(path, "r", encoding="utf-8") as f: - header = f.read(2048) - return ( - (f"plugin: {self.NAME}\n" in header) - or (f"plugin: '{self.NAME}'" in header) - or (f'plugin: "{self.NAME}"' in header) - ) - except Exception: - return False + return verify_inventory_file(self, path) def parse(self, inventory, loader, path, cache=True): super(InventoryModule, self).parse(inventory, loader, path) diff --git a/plugins/inventory/podman_containers.py b/plugins/inventory/podman_containers.py index 7c8aed4..0123b08 100644 --- a/plugins/inventory/podman_containers.py +++ b/plugins/inventory/podman_containers.py @@ -90,12 +90,10 @@ import json import fnmatch import shutil import subprocess -import os -from ansible.utils.display import Display -from typing import Dict, List from ansible.errors import AnsibleParserError from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable +from ansible_collections.containers.podman.plugins.module_utils.inventory.utils import verify_inventory_file class InventoryModule(BaseInventoryPlugin, Cacheable): @@ -103,24 +101,11 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): def __init__(self): super(InventoryModule, self).__init__() - self.display = Display() def verify_file(self, path: str) -> bool: if not super(InventoryModule, self).verify_file(path): return False - unused, ext = os.path.splitext(path) - if ext not in (".yml", ".yaml"): - return False - try: - with open(path, "r", encoding="utf-8") as f: - header = f.read(2048) - return ( - (f"plugin: {self.NAME}\n" in header) - or (f"plugin: '{self.NAME}'" in header) - or (f'plugin: "{self.NAME}"' in header) - ) - except Exception: - return False + return verify_inventory_file(self, path) def parse(self, inventory, loader, path, cache=True): super(InventoryModule, self).parse(inventory, loader, path) @@ -129,10 +114,10 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): executable = config.get("executable", "podman") include_stopped = bool(config.get("include_stopped", False)) name_patterns = list(config.get("name_patterns", []) or []) - label_selectors: Dict[str, str] = dict(config.get("label_selectors", {}) or {}) + label_selectors = dict(config.get("label_selectors", {}) or {}) connection_plugin = config.get("connection_plugin", "containers.podman.podman") group_by_image = bool(config.get("group_by_image", True)) - group_by_label: List[str] = list(config.get("group_by_label", []) or []) + group_by_label = list(config.get("group_by_label", []) or []) verbose_output = bool(config.get("verbose_output", False)) strict = bool(config.get("strict", False)) keyed_groups = list(config.get("keyed_groups", []) or []) @@ -268,7 +253,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): # Resolve dotted key path against hostvars value = None cur = hostvars - for part in str(key_expr).split('.'): + for part in str(key_expr).split("."): if isinstance(cur, dict) and part in cur: cur = cur.get(part) else: diff --git a/plugins/module_utils/inventory/utils.py b/plugins/module_utils/inventory/utils.py new file mode 100644 index 0000000..8cd39f9 --- /dev/null +++ b/plugins/module_utils/inventory/utils.py @@ -0,0 +1,18 @@ +import os + + +def verify_inventory_file(self, path: str) -> bool: + + unused, ext = os.path.splitext(path) + if ext not in (".yml", ".yaml"): + return False + try: + with open(path, "r", encoding="utf-8") as f: + header = f.read(2048) + return ( + (f"plugin: {self.NAME}\n" in header) + or (f"plugin: '{self.NAME}'" in header) + or (f'plugin: "{self.NAME}"' in header) + ) + except Exception: + return False