From f99060b3b9b292e2e82057b23a2520960269e160 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 4 Nov 2016 15:26:50 -0400 Subject: [PATCH] resolve inventory path on init This allows meta refresh_inventory to work with relative paths Added option to unfrackpath to not resolv symlinks fixes #16857 (cherry picked from commit 8217c1c39c8de848550e2a6c816377f11cc60e9f) --- lib/ansible/inventory/__init__.py | 5 ++--- lib/ansible/utils/path.py | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index c1f90b9dcde..50ca53f567c 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -37,8 +37,7 @@ from ansible.plugins import vars_loader from ansible.utils.unicode import to_unicode, to_bytes from ansible.utils.vars import combine_vars from ansible.parsing.utils.addresses import parse_address - -HOSTS_PATTERNS_CACHE = {} +from ansible.utils.path import unfrackpath try: from __main__ import display @@ -55,7 +54,7 @@ class Inventory(object): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded - self.host_list = host_list + self.host_list = unfrackpath(host_list, follow=False) self._loader = loader self._variable_manager = variable_manager self.localhost = None diff --git a/lib/ansible/utils/path.py b/lib/ansible/utils/path.py index a89dc0a95b3..20454db3458 100644 --- a/lib/ansible/utils/path.py +++ b/lib/ansible/utils/path.py @@ -23,14 +23,27 @@ from ansible.utils.unicode import to_bytes __all__ = ['unfrackpath'] -def unfrackpath(path): +def unfrackpath(path, follow=True): ''' - returns a path that is free of symlinks, environment - variables, relative path traversals and symbols (~) - example: - '$HOME/../../var/mail' becomes '/var/spool/mail' + Returns a path that is free of symlinks (if follow=True), environment variables, relative path traversals and symbols (~) + + :arg path: A byte or text string representing a path to be canonicalized + :raises UnicodeDecodeError: If the canonicalized version of the path + contains non-utf8 byte sequences. + :rtype: A text string (unicode on pyyhon2, str on python3). + :returns: An absolute path with symlinks, environment variables, and tilde + expanded. Note that this does not check whether a path exists. + + example:: + '$HOME/../../var/mail' becomes '/var/spool/mail' ''' - return os.path.normpath(os.path.realpath(os.path.expanduser(os.path.expandvars(path)))) + + if follow: + final_path = os.path.normpath(os.path.realpath(os.path.expanduser(os.path.expandvars(path)))) + else: + final_path = os.path.normpath(os.path.abspath(os.path.expanduser(os.path.expandvars(path)))) + + return final_path def makedirs_safe(path, mode=None): '''Safe way to create dirs in muliprocess/thread environments'''