# Buggy code
def get(tgt, fun, expr_form="glob", roster="flat"):
    """
    Get data from the mine based on the target, function and expr_form

    This will actually run the function on all targeted minions (like
    publish.publish), as salt-ssh clients can't update the mine themselves.

    We will look for mine_functions in the roster, pillar, and master config,
    in that order, looking for a match for the defined function

    Targets can be matched based on any standard matching system that can be
    matched on the defined roster (in salt-ssh) via these keywords::

    CLI Example:

    .. code-block:: bash

        salt-ssh '*' mine.get '*' network.interfaces
        salt-ssh '*' mine.get 'myminion' network.interfaces roster=flat
        salt-ssh '*' mine.get '192.168.5.0' network.ipaddrs roster=scan
    """
    # Set up opts for the SSH object
    opts = copy.deepcopy(__opts__)
    if roster:
        opts["roster"] = roster
    opts["argv"] = [fun]
    opts["selected_target_option"] = expr_form
    opts["tgt"] = tgt
    opts["arg"] = []

    # Create the SSH object to handle the actual call
    ssh = salt.client.ssh.SSH(opts)

    # Run salt-ssh to get the minion returns
    rets = {}
    for ret in ssh.run_iter(mine=True):
        rets.update(ret)

    cret = {}
    for host in rets:
        if "return" in rets[host]:
            cret[host] = rets[host]["return"]
        else:
            cret[host] = rets[host]
    return cret


# Traceback (KeyError)
$ salt-ssh   server    state.sls teste
[ERROR   ] Rendering exception occurred: Jinja error: 'python2_bin'
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 366, in render_jinja_tmpl
output = template.render(**decoded_context)
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 2, in top-level template code
File "/usr/lib/python2.7/site-packages/salt/client/ssh/wrapper/mine.py", line 48, in get
ssh = salt.client.ssh.SSH(opts)
File "/usr/lib/python2.7/site-packages/salt/client/ssh/__init__.py", line 288, in __init__
python2_bin=self.opts['python2_bin'],
KeyError: 'python2_bin'

; line 2

---
# Get all machines instances on saltmaster
{% for host, hostinfo in salt['mine.get']('*','network.interfaces').items() -%}    <======================
/tmp/{{ hosts }}/:
file.directory
{% endfor %}

Traceback (most recent call last):
[...]

---
[CRITICAL] Rendering SLS 'base:teste' failed: Jinja error: 'python2_bin'
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 366, in render_jinja_tmpl
output = template.render(**decoded_context)
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 2, in top-level template code
File "/usr/lib/python2.7/site-packages/salt/client/ssh/wrapper/mine.py", line 48, in get
ssh = salt.client.ssh.SSH(opts)
File "/usr/lib/python2.7/site-packages/salt/client/ssh/__init__.py", line 288, in __init__
python2_bin=self.opts['python2_bin'],
KeyError: 'python2_bin'

; line 2

---
# Get all machines instances on saltmaster
{% for host, hostinfo in salt['mine.get']('*','network.interfaces').items() -%}    <======================
/tmp/{{ hosts }}/:
file.directory
{% endfor %}

Traceback (most recent call last):
[...]

---
server:
- Rendering SLS 'base:teste' failed: Jinja error: 'python2_bin'
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 366, in render_jinja_tmpl
output = template.render(**decoded_context)
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 2, in top-level template code
File "/usr/lib/python2.7/site-packages/salt/client/ssh/wrapper/mine.py", line 48, in get
ssh = salt.client.ssh.SSH(opts)
File "/usr/lib/python2.7/site-packages/salt/client/ssh/__init__.py", line 288, in __init__
python2_bin=self.opts['python2_bin'],
KeyError: 'python2_bin'

; line 2

---
# Get all machines instances on saltmaster
{% for host, hostinfo in salt['mine.get']('*','network.interfaces').items() -%}    <======================
/tmp/{{ hosts }}/:
file.directory
{% endfor %}

Traceback (most recent call last):
[...]
---

# Fixed code
def get(tgt, fun, expr_form="glob", roster="flat"):
    """
    Get data from the mine based on the target, function and expr_form

    This will actually run the function on all targeted minions (like
    publish.publish), as salt-ssh clients can't update the mine themselves.

    We will look for mine_functions in the roster, pillar, and master config,
    in that order, looking for a match for the defined function

    Targets can be matched based on any standard matching system that can be
    matched on the defined roster (in salt-ssh) via these keywords::

    CLI Example:

    .. code-block:: bash

        salt-ssh '*' mine.get '*' network.interfaces
        salt-ssh '*' mine.get 'myminion' network.interfaces roster=flat
        salt-ssh '*' mine.get '192.168.5.0' network.ipaddrs roster=scan
    """
    # Set up opts for the SSH object
    opts = copy.deepcopy(__context__["master_opts"])
    minopts = copy.deepcopy(__opts__)
    opts.update(minopts)
    if roster:
        opts["roster"] = roster
    opts["argv"] = [fun]
    opts["selected_target_option"] = expr_form
    opts["tgt"] = tgt
    opts["arg"] = []

    # Create the SSH object to handle the actual call
    ssh = salt.client.ssh.SSH(opts)

    # Run salt-ssh to get the minion returns
    rets = {}
    for ret in ssh.run_iter(mine=True):
        rets.update(ret)

    cret = {}
    for host in rets:
        if "return" in rets[host]:
            cret[host] = rets[host]["return"]
        else:
            cret[host] = rets[host]
    return cret