From 8e88d01872f698a5d1688858c8f5ae5a6aabbb60 Mon Sep 17 00:00:00 2001 From: Andrei Stepanov Date: Dec 04 2018 17:39:40 +0000 Subject: Search for ansible binary rather than hardcoded. Signed-off-by: Andrei Stepanov --- diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2 index 272a57c..c93e275 100755 --- a/inventory/standard-inventory-qcow2 +++ b/inventory/standard-inventory-qcow2 @@ -17,6 +17,7 @@ import sys import tempfile import time import distutils.util +import functools import yaml @@ -121,6 +122,15 @@ except NameError: unicode = str +def which(executable, default=None): + for path in os.environ['PATH'].split(os.pathsep): + path = path.strip('"') + fpath = os.path.join(path, executable) + if os.path.isfile(fpath) and os.access(fpath, os.X_OK): + return fpath + return default + + def main(argv): parser = argparse.ArgumentParser(description="Inventory for a QCow2 test image") parser.add_argument("--list", action="store_true", help="Verbose output") @@ -138,6 +148,13 @@ def main(argv): fhandler.setFormatter(logFormatter) logger.addHandler(fhandler) logger.info("Start provisioner.") + ansibles = ['ansible', 'ansible-3', None] + global ansible_bin + ansible_bin = functools.reduce(which, ansibles) + if not ansible_bin: + logger.error("Fail to find ansible.") + sys.exit(1) + logger.info("Path to ansible: %s", ansible_bin) if opts.host: data = inv_host(opts.host) else: @@ -412,7 +429,7 @@ def inv_host(image): # Wait for ssh to come up ping = [ - "/usr/bin/ansible", + ansible_bin, "--inventory", inventory, "localhost", @@ -453,7 +470,7 @@ def inv_host(image): interpreters = ["/usr/bin/python3", "/usr/bin/python2", "/usr/libexec/platform-python"] for interpreter in interpreters: check_file = [ - "/usr/bin/ansible", + ansible_bin, "--inventory", inventory, "localhost",