[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 <felix@fontein.de>

* Update plugins/inventory/iocage.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/inventory/iocage.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 8f29976102)

Co-authored-by: Vladimir Botka <vbotka@gmail.com>
This commit is contained in:
patchback[bot] 2025-01-22 20:51:37 +01:00 committed by GitHub
parent 45f7661249
commit dbd19a5583
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 3 deletions

View file

@ -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).

View file

@ -45,14 +45,30 @@ DOCUMENTATION = '''
O(host) with SSH and execute the command C(iocage list). O(host) with SSH and execute the command C(iocage list).
This option is not required if O(host) is V(localhost). This option is not required if O(host) is V(localhost).
type: str 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: get_properties:
description: description:
- Get jails' properties. - Get jails' properties.
Creates dictionary C(iocage_properties) for each added host. Creates dictionary C(iocage_properties) for each added host.
type: boolean type: bool
default: false default: false
env: 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 type: dict
default: {} default: {}
notes: notes:
@ -87,6 +103,17 @@ user: admin
env: env:
CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1 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 # enable cache
plugin: community.general.iocage plugin: community.general.iocage
@ -195,6 +222,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def get_inventory(self, path): def get_inventory(self, path):
host = self.get_option('host') host = self.get_option('host')
sudo = self.get_option('sudo')
sudo_preserve_env = self.get_option('sudo_preserve_env')
env = self.get_option('env') env = self.get_option('env')
get_properties = self.get_option('get_properties') get_properties = self.get_option('get_properties')
@ -207,9 +236,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
cmd.append("ssh") cmd.append("ssh")
cmd.append(f"{user}@{host}") cmd.append(f"{user}@{host}")
cmd.extend([f"{k}={v}" for k, v in env.items()]) cmd.extend([f"{k}={v}" for k, v in env.items()])
cmd.append(self.IOCAGE)
cmd_list = cmd.copy() 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('list')
cmd_list.append('--long') cmd_list.append('--long')
try: try:
@ -232,6 +265,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
if get_properties: if get_properties:
for hostname, host_vars in results['_meta']['hostvars'].items(): for hostname, host_vars in results['_meta']['hostvars'].items():
cmd_get_properties = cmd.copy() cmd_get_properties = cmd.copy()
cmd_get_properties.append(self.IOCAGE)
cmd_get_properties.append("get") cmd_get_properties.append("get")
cmd_get_properties.append("--all") cmd_get_properties.append("--all")
cmd_get_properties.append(f"{hostname}") cmd_get_properties.append(f"{hostname}")