# Buggy code
def cachedir_index_add(minion_id, profile, driver, provider, base=None):
    """
    Add an entry to the cachedir index. This generally only needs to happen when
    a new instance is created. This entry should contain:

    .. code-block:: yaml

        - minion_id
        - profile used to create the instance
        - provider and driver name

    The intent of this function is to speed up lookups for the cloud roster for
    salt-ssh. However, other code that makes use of profile information can also
    make use of this function.
    """
    base = init_cachedir(base)
    index_file = os.path.join(base, "index.p")
    lock_file(index_file)

    if os.path.exists(index_file):
        with salt.utils.fopen(index_file, "r") as fh_:
            index = msgpack.load(fh_)
    else:
        index = {}

    prov_comps = provider.split(":")

    index.update(
        {
            minion_id: {
                "id": minion_id,
                "profile": profile,
                "driver": driver,
                "provider": prov_comps[0],
            }
        }
    )

    with salt.utils.fopen(index_file, "w") as fh_:
        msgpack.dump(index, fh_)

    unlock_file(index_file)


# Traceback (UnicodeEncodeError)
$ salt-call -l trace cloud.full_query
.....
[DEBUG   ] Failed to execute 'ec2.list_nodes_min()' while querying for running nodes: 'ascii' codec can't encode characters in position 70860-70863: ordinal not in range(128)
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/cloud/__init__.py", line 2198, in run_parallel_map_providers_query
cloud.clouds[data['fun']]()
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/ec2.py", line 2720, in list_nodes_min
instances = query(params)
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/ec2.py", line 345, in query
result.text
UnicodeEncodeError: 'ascii' codec can't encode characters in position 70860-70863: ordinal not in range(128)
......

# Fixed code
def cachedir_index_add(minion_id, profile, driver, provider, base=None):
    """
    Add an entry to the cachedir index. This generally only needs to happen when
    a new instance is created. This entry should contain:

    .. code-block:: yaml

        - minion_id
        - profile used to create the instance
        - provider and driver name

    The intent of this function is to speed up lookups for the cloud roster for
    salt-ssh. However, other code that makes use of profile information can also
    make use of this function.
    """
    base = init_cachedir(base)
    index_file = os.path.join(base, "index.p")
    lock_file(index_file)

    if os.path.exists(index_file):
        with salt.utils.fopen(index_file, "r") as fh_:
            index = msgpack.load(fh_, encoding=MSGPACK_ENCODING)
    else:
        index = {}

    prov_comps = provider.split(":")

    index.update(
        {
            minion_id: {
                "id": minion_id,
                "profile": profile,
                "driver": driver,
                "provider": prov_comps[0],
            }
        }
    )

    with salt.utils.fopen(index_file, "w") as fh_:
        msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)

    unlock_file(index_file)