diff --git a/changelogs/fragments/5437-proxmox.yml b/changelogs/fragments/5437-proxmox.yml new file mode 100644 index 0000000000..072c9d97b2 --- /dev/null +++ b/changelogs/fragments/5437-proxmox.yml @@ -0,0 +1,2 @@ +minor_changes: + - "proxmox inventory plugin - simplify option handling code (https://github.com/ansible-collections/community.general/pull/5437)." diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 67e0428157..b24bcacf25 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -222,7 +222,6 @@ from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.six import string_types 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 @@ -612,40 +611,23 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): # read config from file, this sets 'options' self._read_config_data(path) - t = Templar(loader=loader) + # read and template auth options + for o in ('url', 'user', 'password', 'token_id', 'token_secret'): + v = self.get_option(o) + if self.templar.is_template(v): + v = self.templar.template(v, disable_looups=False) + setattr(self, 'proxmox_%s' % o, v) - # read options - 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('/') + # some more cleanup and validation + self.proxmox_url = self.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 - - proxmox_token_id = self.get_option('token_id') - if t.is_template(proxmox_token_id): - proxmox_token_id = t.template(variable=proxmox_token_id, disable_lookups=False) - self.proxmox_token_id = proxmox_token_id - - proxmox_token_secret = self.get_option('token_secret') - if t.is_template(proxmox_token_secret): - proxmox_token_secret = t.template(variable=proxmox_token_secret, disable_lookups=False) - self.proxmox_token_secret = proxmox_token_secret - - if proxmox_password is None and (proxmox_token_id is None or proxmox_token_secret is None): + if self.proxmox_password is None and (self.proxmox_token_id is None or self.proxmox_token_secret is None): raise AnsibleError('You must specify either a password or both token_id and token_secret.') if self.get_option('qemu_extended_statuses') and not self.get_option('want_facts'): raise AnsibleError('You must set want_facts to True if you want to use qemu_extended_statuses.') + # read rest of options self.cache_key = self.get_cache_key(path) self.use_cache = cache and self.get_option('cache') self.host_filters = self.get_option('filters')