1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

inventory: find required binary for plugin to work (#53052)

Use existing "get_bin_path" API to find the binary path
required for inventory plugins to work.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-03-16 10:51:42 +05:30 committed by GitHub
parent 1b4a973d28
commit b0306f51d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 10 deletions

View file

@ -59,6 +59,7 @@ from ansible import constants as C
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleParserError
from ansible.module_utils._text import to_native, to_text from ansible.module_utils._text import to_native, to_text
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
from ansible.module_utils.common.process import get_bin_path
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
@ -68,14 +69,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
find_port = re.compile(r'^(\d+)/(\w+)\s+(\w+)\s+(\w+)') find_port = re.compile(r'^(\d+)/(\w+)\s+(\w+)\s+(\w+)')
def __init__(self): def __init__(self):
self._nmap = None self._nmap = None
for path in os.environ.get('PATH').split(':'):
candidate = os.path.join(path, 'nmap')
if os.path.exists(candidate):
self._nmap = candidate
break
super(InventoryModule, self).__init__() super(InventoryModule, self).__init__()
def verify_file(self, path): def verify_file(self, path):
@ -91,6 +85,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def parse(self, inventory, loader, path, cache=False): def parse(self, inventory, loader, path, cache=False):
try:
self._nmap = get_bin_path('nmap', True)
except ValueError as e:
raise AnsibleParserError(e)
if self._nmap is None: if self._nmap is None:
raise AnsibleParserError('nmap inventory plugin requires the nmap cli tool to work') raise AnsibleParserError('nmap inventory plugin requires the nmap cli tool to work')
@ -160,7 +159,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
# TODO: parse more data, OS? # TODO: parse more data, OS?
# if any lefotvers # if any leftovers
if host and ports: if host and ports:
self.inventory.set_variable(host, 'ports', ports) self.inventory.set_variable(host, 'ports', ports)

View file

@ -54,6 +54,7 @@ from ansible.errors import AnsibleParserError
from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils.common._collections_compat import MutableMapping from ansible.module_utils.common._collections_compat import MutableMapping
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
from ansible.module_utils.common.process import get_bin_path
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
@ -62,10 +63,16 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
NAME = 'virtualbox' NAME = 'virtualbox'
VBOX = b"VBoxManage" VBOX = b"VBoxManage"
def __init__(self):
self._vbox_path = None
super(InventoryModule, self).__init__()
def _query_vbox_data(self, host, property_path): def _query_vbox_data(self, host, property_path):
ret = None ret = None
try: try:
cmd = [self.VBOX, b'guestproperty', b'get', to_bytes(host, errors='surrogate_or_strict'), to_bytes(property_path, errors='surrogate_or_strict')] cmd = [self._vbox_path, b'guestproperty', b'get',
to_bytes(host, errors='surrogate_or_strict'),
to_bytes(property_path, errors='surrogate_or_strict')]
x = Popen(cmd, stdout=PIPE) x = Popen(cmd, stdout=PIPE)
ipinfo = to_text(x.stdout.read(), errors='surrogate_or_strict') ipinfo = to_text(x.stdout.read(), errors='surrogate_or_strict')
if 'Value' in ipinfo: if 'Value' in ipinfo:
@ -216,6 +223,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def parse(self, inventory, loader, path, cache=True): def parse(self, inventory, loader, path, cache=True):
try:
self._vbox_path = get_bin_path(self.VBOX, True)
except ValueError as e:
raise AnsibleParserError(e)
super(InventoryModule, self).parse(inventory, loader, path) super(InventoryModule, self).parse(inventory, loader, path)
cache_key = self.get_cache_key(path) cache_key = self.get_cache_key(path)
@ -241,7 +253,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
running = self.get_option('running_only') running = self.get_option('running_only')
# start getting data # start getting data
cmd = [self.VBOX, b'list', b'-l'] cmd = [self._vbox_path, b'list', b'-l']
if running: if running:
cmd.append(b'runningvms') cmd.append(b'runningvms')
else: else: