From 3376442aa25387c1804ac109beb26b7f60f7c84b Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 1 Apr 2022 23:07:42 +0200 Subject: [PATCH] Proxmox Inventory: Add support for templating in inventory file (#4418) (#4435) * added templating to the url, user, and password * added changelog fragment * typo in description for url, and password * clarify in the changelog what can you change * update documentation and added an example * missing quote from examples * Apply suggestions from code review Changed to I for option names Co-authored-by: Felix Fontein * Update plugins/inventory/proxmox.py Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein (cherry picked from commit 13d18c9aa866b3c6f575ca84cb6094986312bc2c) Co-authored-by: Ilija Matoski --- ...x-support-templating-in-inventory-file.yml | 4 +++ plugins/inventory/proxmox.py | 32 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/4386-proxmox-support-templating-in-inventory-file.yml diff --git a/changelogs/fragments/4386-proxmox-support-templating-in-inventory-file.yml b/changelogs/fragments/4386-proxmox-support-templating-in-inventory-file.yml new file mode 100644 index 0000000000..e4c7def4a7 --- /dev/null +++ b/changelogs/fragments/4386-proxmox-support-templating-in-inventory-file.yml @@ -0,0 +1,4 @@ +--- +minor_changes: + - proxmox inventory plugin - add support for templating the ``url``, ``user``, and ``password`` options + (https://github.com/ansible-collections/community.general/pull/4418). diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index f2dba5309b..6a2186d2c8 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -31,6 +31,7 @@ DOCUMENTATION = ''' description: - URL to Proxmox cluster. - If the value is not specified in the inventory configuration, the value of environment variable C(PROXMOX_URL) will be used instead. + - Since community.general 4.7.0 you can also use templating to specify the value of the I(url). default: 'http://localhost:8006' type: str env: @@ -40,6 +41,7 @@ DOCUMENTATION = ''' description: - Proxmox authentication user. - If the value is not specified in the inventory configuration, the value of environment variable C(PROXMOX_USER) will be used instead. + - Since community.general 4.7.0 you can also use templating to specify the value of the I(user). required: yes type: str env: @@ -49,6 +51,7 @@ DOCUMENTATION = ''' description: - Proxmox authentication password. - If the value is not specified in the inventory configuration, the value of environment variable C(PROXMOX_PASSWORD) will be used instead. + - Since community.general 4.7.0 you can also use templating to specify the value of the I(password). required: yes type: str env: @@ -136,6 +139,14 @@ compose: my_inv_var_1: "'my_var1_value'" my_inv_var_2: > "my_var_2_value" + +# Specify the url, user and password using templating +# my.proxmox.yml +plugin: community.general.proxmox +url: "{{ lookup('ansible.builtin.ini', 'url', section='proxmox', file='file.ini') }}" +user: "{{ lookup('ansible.builtin.env','PM_USER') | default('ansible@pve') }}" +password: "{{ lookup('community.general.random_string', base64=True) }}" + ''' import itertools @@ -148,6 +159,7 @@ from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cachea from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.utils.display import Display +from ansible.template import Templar from ansible_collections.community.general.plugins.module_utils.version import LooseVersion @@ -500,10 +512,24 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): # read config from file, this sets 'options' self._read_config_data(path) + t = Templar(loader=loader) + # read options - self.proxmox_url = self.get_option('url').rstrip('/') - self.proxmox_user = self.get_option('user') - self.proxmox_password = self.get_option('password') + proxmox_url = self.get_option('url') + if t.is_template(proxmox_url): + proxmox_url = t.template(variable=proxmox_url, disable_lookups=False) + self.proxmox_url = proxmox_url.rstrip('/') + + proxmox_user = self.get_option('user') + if t.is_template(proxmox_user): + proxmox_user = t.template(variable=proxmox_user, disable_lookups=False) + self.proxmox_user = proxmox_user + + proxmox_password = self.get_option('password') + if t.is_template(proxmox_password): + proxmox_password = t.template(variable=proxmox_password, disable_lookups=False) + self.proxmox_password = proxmox_password + self.cache_key = self.get_cache_key(path) self.use_cache = cache and self.get_option('cache') self.host_filters = self.get_option('filters')