下面先学习salt.client的cmd函数
def cmd(
self,tgt,
fun,
arg=(),
timeout=None,
expr_form='glob',
ret='',
jid='',
kwarg=None,
**kwargs):
'''
Synchronously execute a command on targeted minions
#同步执行命令
The cmd method will execute and wait for the timeout period for all
minions to reply, then it will return all minion data at once. #一次性返回全部的minion执行结果
.. code-block:: python
>>> import salt.client
>>> local = salt.client.LocalClient()
>>> local.cmd('*', 'cmd.run', ['whoami'])
# 'tgt' 'fun' 'arg'
{'jerry': 'root'}
With extra keyword arguments for the command function to be run:
.. code-block:: python
local.cmd('*', 'test.arg', ['arg1', 'arg2'], kwarg={'foo': 'bar'})
# 'tgt' 'fun' 'arg'
#一句话解释“当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值”
Compound commands can be used for multiple executions in a single
publish. Function names and function arguments are provided in separate
lists but the index values must correlate and an empty list must be
used if no arguments are required.
.. code-block:: python
>>> local.cmd('*', [
'grains.items',
'sys.doc',
'cmd.run',
],
[
[],
[],
['uptime'],
])
#这里说的是,一个函数对应一个参数,如果没有参数,则用空list代替
:param tgt: Which minions to target for the execution. Default is shell
glob. Modified by the ``expr_form`` option.
:type tgt: string or list
#这里说的是 tgt默认使用shell glob模式,类型是string或者list
:param fun: The module and function to call on the specified minions of
the form ``module.function``. For example ``test.ping`` or
``grains.items``.
#这里说的是,参数fun来自于module.function
Compound commands
Multiple functions may be called in a single publish by
passing a list of commands. This can dramatically lower
overhead and speed up the application communicating with Salt.
#可以同时调用多个函数
This requires that the ``arg`` param is a list of lists. The
``fun`` list and the ``arg`` list must correlate by index
meaning a function that does not take arguments must still have
a corresponding empty list at the expected index.
:type fun: string or list of strings
#函数类型,字符串或者由字符串组成的列表
:param arg: A list of arguments to pass to the remote function. If the
function takes no arguments ``arg`` may be omitted except when
executing a compound command.
:type arg: list or list-of-lists
:param timeout: Seconds to wait after the last minion returns but
before all minions return.
#超时时间
:param expr_form: The type of ``tgt``. Allowed values:
* ``glob`` - Bash glob completion - Default
* ``pcre`` - Perl style regular expression
* ``list`` - Python list of hosts
* ``grain`` - Match based on a grain comparison
* ``grain_pcre`` - Grain comparison with a regex
* ``pillar`` - Pillar data comparison
* ``pillar_pcre`` - Pillar data comparison with a regex
* ``nodegroup`` - Match on nodegroup
* ``range`` - Use a Range server for matching
* ``compound`` - Pass a compound match string
:param ret: The returner to use. The value passed can be single
returner, or a comma delimited list of returners to call in order
on the minions
:param kwarg: A dictionary with keyword arguments for the function.
:param kwargs: Optional keyword arguments.
Authentication credentials may be passed when using
:conf_master:`external_auth`.
For example: ``local.cmd('*', 'test.ping', username='saltdev',
password='saltdev', eauth='pam')``.
Or: ``local.cmd('*', 'test.ping',
token='5871821ea51754fdcea8153c1c745433')``
:returns: A dictionary with the result of the execution, keyed by
minion ID. A compound command will return a sub-dictionary keyed by
function name.
'''
arg = salt.utils.args.condition_input(arg, kwarg)
pub_data = self.run_job(tgt,
fun,
arg,
expr_form,
ret,
timeout,
jid,
**kwargs)
#cmd接入参数后,使用run_job来执行
if not pub_data:
return pub_data
#如果pub_data为0,则返回
ret = {}
for fn_ret in self.get_cli_event_returns(
pub_data['jid'],
pub_data['minions'],
self._get_timeout(timeout),
tgt,
expr_form,
**kwargs):
#用get_cli_event_returns解析run_job的返回值
if fn_ret:
for mid, data in six.iteritems(fn_ret):
ret[mid] = data.get('ret', {})
return ret
综合来说,cmd函数就是用run_job来执行命令,并且获得jid。然后再用get_cli_event_returns通过jid查询推送结果