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 8217c1c39c)
pull/20067/head
Brian Coca 8 years ago
parent 00c48637b0
commit f99060b3b9

@ -37,8 +37,7 @@ from ansible.plugins import vars_loader
from ansible.utils.unicode import to_unicode, to_bytes from ansible.utils.unicode import to_unicode, to_bytes
from ansible.utils.vars import combine_vars from ansible.utils.vars import combine_vars
from ansible.parsing.utils.addresses import parse_address from ansible.parsing.utils.addresses import parse_address
from ansible.utils.path import unfrackpath
HOSTS_PATTERNS_CACHE = {}
try: try:
from __main__ import display from __main__ import display
@ -55,7 +54,7 @@ class Inventory(object):
# the host file file, or script path, or list of hosts # the host file file, or script path, or list of hosts
# if a list, inventory data will NOT be loaded # 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._loader = loader
self._variable_manager = variable_manager self._variable_manager = variable_manager
self.localhost = None self.localhost = None

@ -23,14 +23,27 @@ from ansible.utils.unicode import to_bytes
__all__ = ['unfrackpath'] __all__ = ['unfrackpath']
def unfrackpath(path): def unfrackpath(path, follow=True):
''' '''
returns a path that is free of symlinks, environment Returns a path that is free of symlinks (if follow=True), environment variables, relative path traversals and symbols (~)
variables, relative path traversals and symbols (~)
example: :arg path: A byte or text string representing a path to be canonicalized
'$HOME/../../var/mail' becomes '/var/spool/mail' :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): def makedirs_safe(path, mode=None):
'''Safe way to create dirs in muliprocess/thread environments''' '''Safe way to create dirs in muliprocess/thread environments'''

Loading…
Cancel
Save