# Buggy code
def get_file_parser(hostsfile, groups, loader):
    # check to see if the specified file starts with a
    # shebang (#!/), so if an error is raised by the parser
    # class we can show a more apropos error

    shebang_present = False
    processed = False
    myerr = []
    parser = None

    try:
        with open(hostsfile, "rb") as inv_file:
            initial_chars = inv_file.read(2)
            if initial_chars.startswith(b"#!"):
                shebang_present = True
    except:
        pass

    # FIXME: make this 'plugin loop'
    # script
    if loader.is_executable(hostsfile):
        try:
            parser = InventoryScript(loader=loader, groups=groups, filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append(
                'Attempted to execute "%s" as inventory script: %s'
                % (hostsfile, to_native(e))
            )
    elif shebang_present:
        myerr.append(
            "The inventory file '%s' looks like it should be an executable inventory script, but is not marked executable. Perhaps you want to correct this with `chmod +x %s`?"
            % (hostsfile, hostsfile)
        )

    # YAML/JSON
    if (
        not processed
        and not shebang_present
        and os.path.splitext(hostsfile)[-1] in C.YAML_FILENAME_EXTENSIONS
    ):
        try:
            parser = InventoryYAMLParser(
                loader=loader, groups=groups, filename=hostsfile
            )
            processed = True
        except Exception as e:
            myerr.append(
                'Attempted to read "%s" as YAML: %s' % (hostsfile, to_native(e))
            )

    # ini
    if not processed and not shebang_present:
        try:
            parser = InventoryINIParser(
                loader=loader, groups=groups, filename=hostsfile
            )
            processed = True
        except Exception as e:
            myerr.append(
                'Attempted to read "%s" as ini file: %s ' % (hostsfile, to_native(e))
            )

    if not processed and myerr:
        raise AnsibleError("\n".join(myerr))

    return parser


# Traceback (UnicodeDecodeError)
$ ansible -i test -m setup all -vvvv
Using /home/dromanov/Документы/ansible/ansible.cfg as config file
ERROR! Unexpected Exception: 'ascii' codec can't decode byte 0xd0 in position 15: ordinal not in range(128)
the full traceback was:

Traceback (most recent call last):
File "/usr/bin/ansible", line 92, in <module>
exit_code = cli.run()
File "/usr/lib/python2.7/dist-packages/ansible/cli/adhoc.py", line 129, in run
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory)
File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 75, in __init__
self._group_vars_files = self._find_group_vars_files(self._basedir)
File "/usr/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 764, in _find_group_vars_files
found_vars = set(os.listdir(unicode(path)))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 15: ordinal not in range(128)

# Fixed code
def get_file_parser(hostsfile, groups, loader):
    # check to see if the specified file starts with a
    # shebang (#!/), so if an error is raised by the parser
    # class we can show a more apropos error

    shebang_present = False
    processed = False
    myerr = []
    parser = None

    try:
        with open(hostsfile, "rb") as inv_file:
            initial_chars = inv_file.read(2)
            if initial_chars.startswith(b"#!"):
                shebang_present = True
    except:
        pass

    # FIXME: make this 'plugin loop'
    # script
    if loader.is_executable(hostsfile):
        try:
            parser = InventoryScript(loader=loader, groups=groups, filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append(
                'Attempted to execute "%s" as inventory script: %s'
                % (hostsfile, to_native(e))
            )
    elif shebang_present:
        myerr.append(
            "The inventory file '%s' looks like it should be an executable inventory script, but is not marked executable. Perhaps you want to correct this with `chmod +x %s`?"
            % (hostsfile, hostsfile)
        )

    # YAML/JSON
    if (
        not processed
        and not shebang_present
        and os.path.splitext(hostsfile)[-1] in C.YAML_FILENAME_EXTENSIONS
    ):
        try:
            parser = InventoryYAMLParser(
                loader=loader, groups=groups, filename=hostsfile
            )
            processed = True
        except Exception as e:
            myerr.append(
                'Attempted to read "%s" as YAML: %s'
                % (to_native(hostsfile), to_native(e))
            )

    # ini
    if not processed and not shebang_present:
        try:
            parser = InventoryINIParser(
                loader=loader, groups=groups, filename=hostsfile
            )
            processed = True
        except Exception as e:
            myerr.append(
                'Attempted to read "%s" as ini file: %s '
                % (to_native(hostsfile), to_native(e))
            )

    if not processed and myerr:
        raise AnsibleError("\n".join(myerr))

    return parser