mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
proxmox inventory, remove redundant and simplify code (#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>
This commit is contained in:
parent
20b84fc709
commit
f1d5f71b8d
2 changed files with 12 additions and 28 deletions
2
changelogs/fragments/5437-proxmox.yml
Normal file
2
changelogs/fragments/5437-proxmox.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- "proxmox inventory plugin - simplify option handling code (https://github.com/ansible-collections/community.general/pull/5437)."
|
|
@ -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 import string_types
|
||||||
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
||||||
from ansible.utils.display import Display
|
from ansible.utils.display import Display
|
||||||
from ansible.template import Templar
|
|
||||||
|
|
||||||
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
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'
|
# read config from file, this sets 'options'
|
||||||
self._read_config_data(path)
|
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
|
# some more cleanup and validation
|
||||||
proxmox_url = self.get_option('url')
|
self.proxmox_url = self.proxmox_url.rstrip('/')
|
||||||
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 self.proxmox_password is None and (self.proxmox_token_id is None or self.proxmox_token_secret is None):
|
||||||
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):
|
|
||||||
raise AnsibleError('You must specify either a password or both token_id and token_secret.')
|
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'):
|
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.')
|
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.cache_key = self.get_cache_key(path)
|
||||||
self.use_cache = cache and self.get_option('cache')
|
self.use_cache = cache and self.get_option('cache')
|
||||||
self.host_filters = self.get_option('filters')
|
self.host_filters = self.get_option('filters')
|
||||||
|
|
Loading…
Reference in a new issue