From dbd19a5583b47da396652fef98c47ab84ca3757d Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 22 Jan 2025 20:51:37 +0100 Subject: [PATCH] [PR #9573/8f299761 backport][stable-10] Implement #9572 Add parameter sudo to inventory plugin iocage (#9605) Implement #9572 Add parameter sudo to inventory plugin iocage (#9573) * Add parameter sudo to inventory plugin iocage #9572 * Add changelog fragment. * Fix error: Expected string in description of sudo. * Fix No2 error: Expected string in description of sudo. * Fix documentation type bool * Update changelogs/fragments/9573-iocage-inventory-sudo.yml Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Add option sudo_preserve_env default=true * Fix DOCUMENTATION. * Set sudo_preserve_env default=false. * Update changelogs/fragments/9573-iocage-inventory-sudo.yml Co-authored-by: Felix Fontein * Update plugins/inventory/iocage.py Co-authored-by: Felix Fontein * Update plugins/inventory/iocage.py Co-authored-by: Felix Fontein --------- Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> Co-authored-by: Felix Fontein (cherry picked from commit 8f29976102d4ada43c1a639abab398183915cf34) Co-authored-by: Vladimir Botka --- .../fragments/9573-iocage-inventory-sudo.yml | 2 + plugins/inventory/iocage.py | 40 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/9573-iocage-inventory-sudo.yml diff --git a/changelogs/fragments/9573-iocage-inventory-sudo.yml b/changelogs/fragments/9573-iocage-inventory-sudo.yml new file mode 100644 index 00000000000..f636f34055b --- /dev/null +++ b/changelogs/fragments/9573-iocage-inventory-sudo.yml @@ -0,0 +1,2 @@ +minor_changes: + - iocage inventory plugin - the new parameter ``sudo`` of the plugin lets the command ``iocage list -l`` to run as root on the iocage host. This is needed to get the IPv4 of a running DHCP jail (https://github.com/ansible-collections/community.general/issues/9572, https://github.com/ansible-collections/community.general/pull/9573). diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index faa6c827101..31aad309f59 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -45,14 +45,30 @@ O(host) with SSH and execute the command C(iocage list). This option is not required if O(host) is V(localhost). type: str + sudo: + description: + - Enable execution as root. + - This requires passwordless sudo of the command C(iocage list*). + type: bool + default: false + version_added: 10.3.0 + sudo_preserve_env: + description: + - Preserve environment if O(sudo) is enabled. + - This requires C(SETENV) sudoers tag. + type: bool + default: false + version_added: 10.3.0 get_properties: description: - Get jails' properties. Creates dictionary C(iocage_properties) for each added host. - type: boolean + type: bool default: false env: - description: O(user)'s environment on O(host). + description: + - O(user)'s environment on O(host). + - Enable O(sudo_preserve_env) if O(sudo) is enabled. type: dict default: {} notes: @@ -87,6 +103,17 @@ env: CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1 +--- +# execute as root +# sudoers example 'admin ALL=(ALL) NOPASSWD:SETENV: /usr/local/bin/iocage list*' +plugin: community.general.iocage +host: 10.1.0.73 +user: admin +sudo: true +sudo_preserve_env: true +env: + CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1 + --- # enable cache plugin: community.general.iocage @@ -195,6 +222,8 @@ def parse(self, inventory, loader, path, cache=True): def get_inventory(self, path): host = self.get_option('host') + sudo = self.get_option('sudo') + sudo_preserve_env = self.get_option('sudo_preserve_env') env = self.get_option('env') get_properties = self.get_option('get_properties') @@ -207,9 +236,13 @@ def get_inventory(self, path): cmd.append("ssh") cmd.append(f"{user}@{host}") cmd.extend([f"{k}={v}" for k, v in env.items()]) - cmd.append(self.IOCAGE) cmd_list = cmd.copy() + if sudo: + cmd_list.append('sudo') + if sudo_preserve_env: + cmd_list.append('--preserve-env') + cmd_list.append(self.IOCAGE) cmd_list.append('list') cmd_list.append('--long') try: @@ -232,6 +265,7 @@ def get_inventory(self, path): if get_properties: for hostname, host_vars in results['_meta']['hostvars'].items(): cmd_get_properties = cmd.copy() + cmd_get_properties.append(self.IOCAGE) cmd_get_properties.append("get") cmd_get_properties.append("--all") cmd_get_properties.append(f"{hostname}")