From b8a988e92230577362370fc4a1b4068fc56f7da2 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Fri, 29 Apr 2016 22:19:12 -0700 Subject: [PATCH] bytes when passing to os.path.* and opening the file, text at other times Fixes #15644 --- lib/ansible/inventory/__init__.py | 6 +++--- lib/ansible/parsing/dataloader.py | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 5875e9653c7..14c4e34f9f2 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -34,7 +34,7 @@ from ansible.inventory.dir import InventoryDirectory, get_file_parser from ansible.inventory.group import Group from ansible.inventory.host import Host from ansible.plugins import vars_loader -from ansible.utils.unicode import to_unicode +from ansible.utils.unicode import to_unicode, to_bytes from ansible.utils.vars import combine_vars from ansible.parsing.utils.addresses import parse_address @@ -742,11 +742,11 @@ class Inventory(object): if group and host is None: # load vars in dir/group_vars/name_of_group - base_path = os.path.abspath(os.path.join(to_unicode(basedir, errors='strict'), "group_vars/%s" % group.name)) + base_path = to_unicode(os.path.abspath(os.path.join(to_bytes(basedir), "group_vars/%s" % to_bytes(group.name))), errors='strict') results = combine_vars(results, self._variable_manager.add_group_vars_file(base_path, self._loader)) elif host and group is None: # same for hostvars in dir/host_vars/name_of_host - base_path = os.path.abspath(os.path.join(to_unicode(basedir, errors='strict'), "host_vars/%s" % host.name)) + base_path = to_unicode(os.path.abspath(os.path.join(to_bytes(basedir), "host_vars/%s" % to_bytes(host.name))), errors='strict') results = combine_vars(results, self._variable_manager.add_host_vars_file(base_path, self._loader)) # all done, results is a dictionary of variables for this particular host. diff --git a/lib/ansible/parsing/dataloader.py b/lib/ansible/parsing/dataloader.py index 42aa7fd83e6..3ab56a935cc 100644 --- a/lib/ansible/parsing/dataloader.py +++ b/lib/ansible/parsing/dataloader.py @@ -160,12 +160,13 @@ class DataLoader(): if not file_name or not isinstance(file_name, string_types): raise AnsibleParserError("Invalid filename: '%s'" % str(file_name)) - if not self.path_exists(file_name) or not self.is_file(file_name): + b_file_name = to_bytes(file_name) + if not self.path_exists(b_file_name) or not self.is_file(b_file_name): raise AnsibleFileNotFound("the file_name '%s' does not exist, or is not readable" % file_name) show_content = True try: - with open(file_name, 'rb') as f: + with open(b_file_name, 'rb') as f: data = f.read() if self._vault.is_encrypted(data): data = self._vault.decrypt(data)