.. versionadded:: 2017.7.0

    Execute ``fun`` on all minions matched by ``tgt`` and ``tgt_type``.
    Parameter ``fun`` is the name of execution module function to call.

    This function should mainly be used as a helper for runner modules,
    in order to avoid redundant code.
    For example, when inside a runner one needs to execute a certain function
    on arbitrary groups of minions, only has to:

    .. code-block:: python

        ret1 = __salt__['salt.execute']('*', 'mod.fun')
        ret2 = __salt__['salt.execute']('my_nodegroup', 'mod2.fun2', tgt_type='nodegroup')

    It can also be used to schedule jobs directly on the master, for example:

    .. code-block:: yaml

        schedule:
            collect_bgp_stats:
                function: salt.execute
                args:
                    - edge-routers
                    - bgp.neighbors
                kwargs:
                    tgt_type: nodegroup
                days: 1
                returner: redis

def execute(tgt,
            fun,
            arg=(),
            timeout=None,
            tgt_type='glob',
            ret='',
            jid='',
            kwarg=None,
            **kwargs):
    '''
    .. versionadded:: 2017.7.0

    Execute ``fun`` on all minions matched by ``tgt`` and ``tgt_type``.
    Parameter ``fun`` is the name of execution module function to call.

    This function should mainly be used as a helper for runner modules,
    in order to avoid redundant code.
    For example, when inside a runner one needs to execute a certain function
    on arbitrary groups of minions, only has to:

    .. code-block:: python

        ret1 = __salt__['salt.execute']('*', 'mod.fun')
        ret2 = __salt__['salt.execute']('my_nodegroup', 'mod2.fun2', tgt_type='nodegroup')

    It can also be used to schedule jobs directly on the master, for example:

    .. code-block:: yaml

        schedule:
            collect_bgp_stats:
                function: salt.execute
                args:
                    - edge-routers
                    - bgp.neighbors
                kwargs:
                    tgt_type: nodegroup
                days: 1
                returner: redis
    '''
    client = salt.client.get_local_client(__opts__['conf_file'])
    try:
        ret = client.cmd(tgt,
                         fun,
                         arg=arg,
                         timeout=timeout or __opts__['timeout'],
                         tgt_type=tgt_type,  # no warn_until, as this is introduced only in 2017.7.0
                         ret=ret,
                         jid=jid,
                         kwarg=kwarg,
                         **kwargs)
    except SaltClientError as client_error:
        log.error('Error while executing %s on %s (%s)', fun, tgt, tgt_type)
        log.error(client_error)
        return {}
    return ret