Add option to pass list of dirs to get_bin_path in module_common.py

The optional list is prepended to PATH.
Fix get_bin_path() to use os.path.join().
This commit is contained in:
Stephen Fromm 2012-08-22 23:28:14 -07:00
parent bdb39058ae
commit 4e7b67a45a

View file

@ -202,20 +202,24 @@ class AnsibleModule(object):
log_args = re.sub(r'login_password=.+ (.*)', r"login_password=NOT_LOGGING_PASSWORD \1", log_args) log_args = re.sub(r'login_password=.+ (.*)', r"login_password=NOT_LOGGING_PASSWORD \1", log_args)
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % log_args) syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % log_args)
def get_bin_path(self, arg): def get_bin_path(self, arg, opt_dirs=[]):
''' '''
find system executable in PATH. find system executable in PATH.
if found return full path; otherwise return None if found return full path; otherwise return None
''' '''
sbin_paths = ['/sbin', '/usr/sbin', '/usr/local/sbin'] sbin_paths = ['/sbin', '/usr/sbin', '/usr/local/sbin']
paths = os.environ.get('PATH').split(':') paths = []
for d in opt_dirs:
if d is not None and os.path.exists(d):
paths.append(d)
paths += os.environ.get('PATH').split(':')
bin_path = None bin_path = None
# mangle PATH to include /sbin dirs # mangle PATH to include /sbin dirs
for p in sbin_paths: for p in sbin_paths:
if p not in paths and os.path.exists(p): if p not in paths and os.path.exists(p):
paths.append(p) paths.append(p)
for d in paths: for d in paths:
path = '%s/%s' % (d, arg) path = os.path.join(d, arg)
if os.path.exists(path) and os.access(path, os.X_OK): if os.path.exists(path) and os.access(path, os.X_OK):
bin_path = path bin_path = path
break break