From 8f182b896992de332f1895764f84326a57e9a99d Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Thu, 30 Jan 2025 14:04:10 +0100 Subject: [PATCH 1/8] Add parameter hooks to inventory plugin iocage. --- plugins/inventory/iocage.py | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index 31aad309f59..126e31e3998 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -71,6 +71,15 @@ - Enable O(sudo_preserve_env) if O(sudo) is enabled. type: dict default: {} + hooks: + description: + - List of paths to the hook files in a jail. + - Content of the hook files is stored in the items of the list C(iocage_hooks). + - If a hook file is not available the item keeps the dash character C(-). + - The variable C(iocage_hooks) is not created if O(hooks) is empty. + type: list + elements: path + version_added: 10.3.0 notes: - You might want to test the command C(ssh user@host iocage list -l) on the controller before using this inventory plugin with O(user) specified @@ -142,6 +151,18 @@ key: iocage_release - prefix: state key: iocage_state + +--- +# Read the file /var/db/dhclient-hook.address.epair0b in the jails and use it as ansible_host +plugin: community.general.iocage +host: 10.1.0.73 +user: admin +hooks: + - /var/db/dhclient-hook.address.epair0b +compose: + ansible_host: iocage_hooks.0 +groups: + test: inventory_hostname.startswith('test') ''' import re @@ -226,6 +247,7 @@ def get_inventory(self, path): sudo_preserve_env = self.get_option('sudo_preserve_env') env = self.get_option('env') get_properties = self.get_option('get_properties') + hooks = self.get_option('hooks') cmd = [] my_env = os.environ.copy() @@ -286,6 +308,50 @@ def get_inventory(self, path): self.get_properties(t_stdout, results, hostname) + if hooks: + cmd_get_pool = cmd.copy() + cmd_get_pool.append(self.IOCAGE) + cmd_get_pool.append('get') + cmd_get_pool.append('--pool') + try: + p = Popen(cmd_get_pool, stdout=PIPE, stderr=PIPE, env=my_env) + stdout, stderr = p.communicate() + if p.returncode != 0: + raise AnsibleError( + f'Failed to run cmd={cmd_get_pool}, rc={p.returncode}, stderr={to_native(stderr)}') + try: + iocage_pool = to_text(stdout, errors='surrogate_or_strict').strip() + except UnicodeError as e: + raise AnsibleError(f'Invalid (non unicode) input returned: {e}') from e + except Exception as e: + raise AnsibleError(f'Failed to get pool: {e}') from e + + for hostname, host_vars in results['_meta']['hostvars'].items(): + iocage_hooks = [] + for hook in hooks: + path = "/" + iocage_pool + "/iocage/jails/" + hostname + "/root" + hook + cmd_cat_hook = cmd.copy() + cmd_cat_hook.append('cat') + cmd_cat_hook.append(path) + try: + p = Popen(cmd_cat_hook, stdout=PIPE, stderr=PIPE, env=my_env) + stdout, stderr = p.communicate() + if p.returncode != 0: + iocage_hooks.append('-') + continue + + try: + iocage_hook = to_text(stdout, errors='surrogate_or_strict').strip() + except UnicodeError as e: + raise AnsibleError(f'Invalid (non unicode) input returned: {e}') from e + + except Exception: + iocage_hooks.append('-') + else: + iocage_hooks.append(iocage_hook) + + results['_meta']['hostvars'][hostname]['iocage_hooks'] = iocage_hooks + return results def get_jails(self, t_stdout, results): From 1b4318583ec9a87f296b20631880debe80c277ca Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Thu, 30 Jan 2025 14:38:47 +0100 Subject: [PATCH 2/8] Add changelog fragment. --- changelogs/fragments/9651-iocage-inventory-hooks.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/9651-iocage-inventory-hooks.yml diff --git a/changelogs/fragments/9651-iocage-inventory-hooks.yml b/changelogs/fragments/9651-iocage-inventory-hooks.yml new file mode 100644 index 00000000000..cf197547d73 --- /dev/null +++ b/changelogs/fragments/9651-iocage-inventory-hooks.yml @@ -0,0 +1,2 @@ +minor_changes: + - iocage inventory plugin - the new parameter ``hooks`` of the plugin is a list of files inside a jail that provide configuration parameters for the inventory. The inventory plugin reads the files from the jails and put the contents into the items of created variable ``iocage_hooks`` (https://github.com/ansible-collections/community.general/issues/9650, https://github.com/ansible-collections/community.general/pull/9651). From 97197b04520b86cde95da878e84131508dba402c Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Fri, 31 Jan 2025 23:17:34 +0100 Subject: [PATCH 3/8] Update plugins/inventory/iocage.py Co-authored-by: Felix Fontein --- plugins/inventory/iocage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index 126e31e3998..58743ee3e6e 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -79,7 +79,7 @@ - The variable C(iocage_hooks) is not created if O(hooks) is empty. type: list elements: path - version_added: 10.3.0 + version_added: 10.4.0 notes: - You might want to test the command C(ssh user@host iocage list -l) on the controller before using this inventory plugin with O(user) specified From 318238bd3edc0b5213e947d99496d27f8055faed Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Sat, 1 Feb 2025 12:07:22 +0100 Subject: [PATCH 4/8] Parameter renamed to hooks_results --- plugins/inventory/iocage.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index 58743ee3e6e..b9fe6cbf0a6 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -71,12 +71,12 @@ - Enable O(sudo_preserve_env) if O(sudo) is enabled. type: dict default: {} - hooks: + hooks_results: description: - - List of paths to the hook files in a jail. - - Content of the hook files is stored in the items of the list C(iocage_hooks). - - If a hook file is not available the item keeps the dash character C(-). - - The variable C(iocage_hooks) is not created if O(hooks) is empty. + - List of paths to the files in a jail. + - Content of the files is stored in the items of the list C(iocage_hooks). + - If a file is not available the item keeps the dash character C(-). + - The variable C(iocage_hooks) is not created if O(hooks_results) is empty. type: list elements: path version_added: 10.4.0 @@ -157,7 +157,7 @@ plugin: community.general.iocage host: 10.1.0.73 user: admin -hooks: +hooks_results: - /var/db/dhclient-hook.address.epair0b compose: ansible_host: iocage_hooks.0 @@ -247,7 +247,7 @@ def get_inventory(self, path): sudo_preserve_env = self.get_option('sudo_preserve_env') env = self.get_option('env') get_properties = self.get_option('get_properties') - hooks = self.get_option('hooks') + hooks_results = self.get_option('hooks_results') cmd = [] my_env = os.environ.copy() @@ -308,7 +308,7 @@ def get_inventory(self, path): self.get_properties(t_stdout, results, hostname) - if hooks: + if hooks_results: cmd_get_pool = cmd.copy() cmd_get_pool.append(self.IOCAGE) cmd_get_pool.append('get') @@ -328,7 +328,7 @@ def get_inventory(self, path): for hostname, host_vars in results['_meta']['hostvars'].items(): iocage_hooks = [] - for hook in hooks: + for hook in hooks_results: path = "/" + iocage_pool + "/iocage/jails/" + hostname + "/root" + hook cmd_cat_hook = cmd.copy() cmd_cat_hook.append('cat') From a1d172ce487c380b66f568cd26edbcd13b634a65 Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Tue, 4 Feb 2025 01:03:19 +0100 Subject: [PATCH 5/8] Fix DOCUMENTATION YAML 4-space indentation. --- plugins/inventory/iocage.py | 62 ++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index b9fe6cbf0a6..bd479ae3250 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -6,7 +6,7 @@ from __future__ import annotations -DOCUMENTATION = ''' +DOCUMENTATION = r''' name: iocage short_description: iocage inventory source version_added: 10.2.0 @@ -28,9 +28,9 @@ options: plugin: description: - - The name of this plugin, it should always be set to - V(community.general.iocage) for this plugin to recognize - it as its own. + - The name of this plugin, it should always be set to + V(community.general.iocage) for this plugin to recognize + it as its own. required: true choices: ['community.general.iocage'] type: str @@ -40,60 +40,60 @@ default: localhost user: description: - - C(iocage) user. - It is expected that the O(user) is able to connect to the - O(host) with SSH and execute the command C(iocage list). - This option is not required if O(host) is V(localhost). + - C(iocage) user. + It is expected that the O(user) is able to connect to the + 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*). + - 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. + - 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. + - Get jails' properties. + Creates dictionary C(iocage_properties) for each added host. type: bool default: false env: description: - - O(user)'s environment on O(host). - - Enable O(sudo_preserve_env) if O(sudo) is enabled. + - O(user)'s environment on O(host). + - Enable O(sudo_preserve_env) if O(sudo) is enabled. type: dict default: {} hooks_results: description: - - List of paths to the files in a jail. - - Content of the files is stored in the items of the list C(iocage_hooks). - - If a file is not available the item keeps the dash character C(-). - - The variable C(iocage_hooks) is not created if O(hooks_results) is empty. + - List of paths to the files in a jail. + - Content of the files is stored in the items of the list C(iocage_hooks). + - If a file is not available the item keeps the dash character C(-). + - The variable C(iocage_hooks) is not created if O(hooks_results) is empty. type: list elements: path version_added: 10.4.0 notes: - - You might want to test the command C(ssh user@host iocage list -l) on - the controller before using this inventory plugin with O(user) specified - and with O(host) other than V(localhost). - - If you run this inventory plugin on V(localhost) C(ssh) is not used. - In this case, test the command C(iocage list -l). - - This inventory plugin creates variables C(iocage_*) for each added host. - - The values of these variables are collected from the output of the - command C(iocage list -l). - - The names of these variables correspond to the output columns. - - The column C(NAME) is used to name the added host. + - You might want to test the command C(ssh user@host iocage list -l) on + the controller before using this inventory plugin with O(user) specified + and with O(host) other than V(localhost). + - If you run this inventory plugin on V(localhost) C(ssh) is not used. + In this case, test the command C(iocage list -l). + - This inventory plugin creates variables C(iocage_*) for each added host. + - The values of these variables are collected from the output of the + command C(iocage list -l). + - The names of these variables correspond to the output columns. + - The column C(NAME) is used to name the added host. ''' -EXAMPLES = ''' +EXAMPLES = r''' --- # file name must end with iocage.yaml or iocage.yml plugin: community.general.iocage From 90301ae021a9afc08e77586cb4b1eedc17072ace Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Sun, 9 Feb 2025 01:55:31 +0100 Subject: [PATCH 6/8] Fix DOCUMENTATION YAML 2-space indentation. --- plugins/inventory/iocage.py | 166 ++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index bd479ae3250..cad3427cd13 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -7,90 +7,90 @@ from __future__ import annotations DOCUMENTATION = r''' - name: iocage - short_description: iocage inventory source - version_added: 10.2.0 - author: - - Vladimir Botka (@vbotka) - requirements: - - iocage >= 1.8 +name: iocage +short_description: iocage inventory source +version_added: 10.2.0 +author: + - Vladimir Botka (@vbotka) +requirements: + - iocage >= 1.8 +description: + - Get inventory hosts from the iocage jail manager running on O(host). + - By default, O(host) is V(localhost). If O(host) is not V(localhost) it + is expected that the user running Ansible on the controller can + connect to the O(host) account O(user) with SSH non-interactively and + execute the command C(iocage list). + - Uses a configuration file as an inventory source, it must end + in C(.iocage.yml) or C(.iocage.yaml). +extends_documentation_fragment: + - ansible.builtin.constructed + - ansible.builtin.inventory_cache +options: + plugin: description: - - Get inventory hosts from the iocage jail manager running on O(host). - - By default, O(host) is V(localhost). If O(host) is not V(localhost) it - is expected that the user running Ansible on the controller can - connect to the O(host) account O(user) with SSH non-interactively and - execute the command C(iocage list). - - Uses a configuration file as an inventory source, it must end - in C(.iocage.yml) or C(.iocage.yaml). - extends_documentation_fragment: - - ansible.builtin.constructed - - ansible.builtin.inventory_cache - options: - plugin: - description: - - The name of this plugin, it should always be set to - V(community.general.iocage) for this plugin to recognize - it as its own. - required: true - choices: ['community.general.iocage'] - type: str - host: - description: The IP/hostname of the C(iocage) host. - type: str - default: localhost - user: - description: - - C(iocage) user. - It is expected that the O(user) is able to connect to the - 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: bool - default: false - env: - description: - - O(user)'s environment on O(host). - - Enable O(sudo_preserve_env) if O(sudo) is enabled. - type: dict - default: {} - hooks_results: - description: - - List of paths to the files in a jail. - - Content of the files is stored in the items of the list C(iocage_hooks). - - If a file is not available the item keeps the dash character C(-). - - The variable C(iocage_hooks) is not created if O(hooks_results) is empty. - type: list - elements: path - version_added: 10.4.0 - notes: - - You might want to test the command C(ssh user@host iocage list -l) on - the controller before using this inventory plugin with O(user) specified - and with O(host) other than V(localhost). - - If you run this inventory plugin on V(localhost) C(ssh) is not used. - In this case, test the command C(iocage list -l). - - This inventory plugin creates variables C(iocage_*) for each added host. - - The values of these variables are collected from the output of the - command C(iocage list -l). - - The names of these variables correspond to the output columns. - - The column C(NAME) is used to name the added host. + - The name of this plugin, it should always be set to + V(community.general.iocage) for this plugin to recognize + it as its own. + required: true + choices: ['community.general.iocage'] + type: str + host: + description: The IP/hostname of the C(iocage) host. + type: str + default: localhost + user: + description: + - C(iocage) user. + It is expected that the O(user) is able to connect to the + 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: bool + default: false + env: + description: + - O(user)'s environment on O(host). + - Enable O(sudo_preserve_env) if O(sudo) is enabled. + type: dict + default: {} + hooks_results: + description: + - List of paths to the files in a jail. + - Content of the files is stored in the items of the list C(iocage_hooks). + - If a file is not available the item keeps the dash character C(-). + - The variable C(iocage_hooks) is not created if O(hooks_results) is empty. + type: list + elements: path + version_added: 10.4.0 +notes: + - You might want to test the command C(ssh user@host iocage list -l) on + the controller before using this inventory plugin with O(user) specified + and with O(host) other than V(localhost). + - If you run this inventory plugin on V(localhost) C(ssh) is not used. + In this case, test the command C(iocage list -l). + - This inventory plugin creates variables C(iocage_*) for each added host. + - The values of these variables are collected from the output of the + command C(iocage list -l). + - The names of these variables correspond to the output columns. + - The column C(NAME) is used to name the added host. ''' EXAMPLES = r''' From c8f8ed154f607d1c79c1b550c9124456e58d0bda Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Tue, 11 Feb 2025 01:06:44 +0100 Subject: [PATCH 7/8] Update changelogs/fragments/9651-iocage-inventory-hooks.yml Co-authored-by: Felix Fontein --- changelogs/fragments/9651-iocage-inventory-hooks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/9651-iocage-inventory-hooks.yml b/changelogs/fragments/9651-iocage-inventory-hooks.yml index cf197547d73..268348ba3c2 100644 --- a/changelogs/fragments/9651-iocage-inventory-hooks.yml +++ b/changelogs/fragments/9651-iocage-inventory-hooks.yml @@ -1,2 +1,2 @@ minor_changes: - - iocage inventory plugin - the new parameter ``hooks`` of the plugin is a list of files inside a jail that provide configuration parameters for the inventory. The inventory plugin reads the files from the jails and put the contents into the items of created variable ``iocage_hooks`` (https://github.com/ansible-collections/community.general/issues/9650, https://github.com/ansible-collections/community.general/pull/9651). + - iocage inventory plugin - the new parameter ``hooks_results`` of the plugin is a list of files inside a jail that provide configuration parameters for the inventory. The inventory plugin reads the files from the jails and put the contents into the items of created variable ``iocage_hooks`` (https://github.com/ansible-collections/community.general/issues/9650, https://github.com/ansible-collections/community.general/pull/9651). From cd27805ed3c94794455ad5a2a723da6633428de0 Mon Sep 17 00:00:00 2001 From: Vladimir Botka Date: Tue, 11 Feb 2025 15:38:01 +0100 Subject: [PATCH 8/8] Add note about activated pool mountpoint. --- plugins/inventory/iocage.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index cad3427cd13..6edac6d0054 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -91,6 +91,11 @@ command C(iocage list -l). - The names of these variables correspond to the output columns. - The column C(NAME) is used to name the added host. + - The option O(hooks_results) expects the C(poolname) of a jail is mounted to + C(/poolname). For example, if you activate the pool C(iocage) this plugin + expects to find the O(hooks_results) items in the path + C(/iocage/iocage/jails//root). If you mount the C(poolname) to a + different path the easiest remedy is to create a symlink. ''' EXAMPLES = r'''