Skip to content

Commit

Permalink
[PR #9573/8f299761 backport][stable-10] Implement #9572 Add parameter…
Browse files Browse the repository at this point in the history
… 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 8f29976)

Co-authored-by: Vladimir Botka <vbotka@gmail.com>
  • Loading branch information
patchback[bot] and vbotka authored Jan 22, 2025
1 parent 45f7661 commit dbd19a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/9573-iocage-inventory-sudo.yml
Original file line number Diff line number Diff line change
@@ -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).
40 changes: 37 additions & 3 deletions plugins/inventory/iocage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand All @@ -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:
Expand All @@ -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}")
Expand Down

0 comments on commit dbd19a5

Please sign in to comment.