Skip to content

Commit

Permalink
proxmox inventory, remove redundant and simplify code (ansible-collec…
Browse files Browse the repository at this point in the history
…tions#5437)

* remove redundant and simplify code

we already have a templar from base class

loop reuses code instead of X copies of it

* whitey

* no need to import templar again

* Add changelog fragment.

Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
2 people authored and russoz committed Nov 5, 2022
1 parent 1833690 commit 5223823
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5437-proxmox.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "proxmox inventory plugin - simplify option handling code (https://github.com/ansible-collections/community.general/pull/5437)."
38 changes: 10 additions & 28 deletions plugins/inventory/proxmox.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@
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

Expand Down Expand Up @@ -612,40 +611,23 @@ def parse(self, inventory, loader, path, cache=True):
# 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')
Expand Down

0 comments on commit 5223823

Please sign in to comment.