From b0306f51d7818117ed5412a489f7c02191a92c96 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Sat, 16 Mar 2019 10:51:42 +0530 Subject: [PATCH] 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 --- lib/ansible/plugins/inventory/nmap.py | 15 +++++++-------- lib/ansible/plugins/inventory/virtualbox.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/ansible/plugins/inventory/nmap.py b/lib/ansible/plugins/inventory/nmap.py index 5b1b5375cb2..5c84f27560d 100644 --- a/lib/ansible/plugins/inventory/nmap.py +++ b/lib/ansible/plugins/inventory/nmap.py @@ -59,6 +59,7 @@ from ansible import constants as C from ansible.errors import AnsibleParserError from ansible.module_utils._text import to_native, to_text from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable +from ansible.module_utils.common.process import get_bin_path 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+)') def __init__(self): - 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__() def verify_file(self, path): @@ -91,6 +85,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): 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: 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? - # if any lefotvers + # if any leftovers if host and ports: self.inventory.set_variable(host, 'ports', ports) diff --git a/lib/ansible/plugins/inventory/virtualbox.py b/lib/ansible/plugins/inventory/virtualbox.py index c759f49513d..6bb16fa18ed 100644 --- a/lib/ansible/plugins/inventory/virtualbox.py +++ b/lib/ansible/plugins/inventory/virtualbox.py @@ -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.common._collections_compat import MutableMapping from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable +from ansible.module_utils.common.process import get_bin_path class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): @@ -62,10 +63,16 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): NAME = 'virtualbox' VBOX = b"VBoxManage" + def __init__(self): + self._vbox_path = None + super(InventoryModule, self).__init__() + def _query_vbox_data(self, host, property_path): ret = None 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) ipinfo = to_text(x.stdout.read(), errors='surrogate_or_strict') if 'Value' in ipinfo: @@ -216,6 +223,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): 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) cache_key = self.get_cache_key(path) @@ -241,7 +253,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): running = self.get_option('running_only') # start getting data - cmd = [self.VBOX, b'list', b'-l'] + cmd = [self._vbox_path, b'list', b'-l'] if running: cmd.append(b'runningvms') else: