saltstack相关的一些总结

时间:2021-03-06 14:23:11

master配置如下:

file_roots:
  base:
    - /srv/salt

reactor:
  - 'salt/minion/*/start':
    - /var/salt/reactor/firstconnection.sls

暂时其它按照默认配置来写的

一:grains值的定义:

_grains/
├── role.py
└── test.py
我在/srv/salt/目录下mkdir了_grains目录,里面用python 写了两个脚本,内容如下:

[root@admin-node _grains]# cat test.py 
#! /uar/bin/python
def gettest():
    grains={}
    grains['software']='http'
    return grains
[root@admin-node _grains]# cat role.py 
#! /usr/bin/python
import socket,os
def getroutetable():
    grains={}
    grains['hostname']=socket.gethostname()
    return grains
    
为了同步grains,我们执行salt  "*"  saltutil.sync_grains

然后我们查看一下hostname、software两个grains:

[root@admin-node _grains]# salt '172.18.1.211' grains.item hostname
172.18.1.211:
    ----------
    hostname:
        node1
[root@admin-node _grains]# salt '172.18.1.211' grains.item software
172.18.1.211:
    ----------
    software:
        http
如上图所示,我们定义的hostname和software两个grains正确显示了


二:pillar的定义

pillar定义好后,可以通过命令saltutil.refresh_pillar

[root@admin-node srv]# tree pillar/
pillar/
├── schedule1.sls
├── schedule.sls
├── test.sls
└── top.sls

0 directories, 4 files
整个pillar目录的结构如上所示

(1)top.sls:

[root@admin-node pillar]# cat top.sls 
base:
  '*':
    - test
    - schedule1
  (2) test.sls:

[root@admin-node pillar]# cat test.sls 
{% if 'eth0' in grains['ip_interfaces'] %}
  {% if grains['ip_interfaces']['eth0'][0] == '172.18.1.211' %}
  server: web
  {% elif grains['ip_interfaces']['eth0'][0] == '172.18.1.212' %}
  server: ftp
  {% elif grains['ip_interfaces']['eth0'][0] == '172.18.1.213' %}
  server: mail
  {% else %}
  server: vedio
  {% endif %}

{%elif 'em1' in grains['ip_interfaces']%}
  {% if grains['ip_interfaces']['em1'][0] == '172.18.1.211' %}
  server: web
  {% elif grains['ip_interfaces']['em1'][0] == '172.18.1.212' %}
  server: ftp
  {% elif grains['ip_interfaces']['em1'][0] == '172.18.1.213' %}
  server: mail
  {% else %}
  server: vedio
  {% endif %}
{% endif %}
这里根据grains值定义了pillar值

(3) schedule1.sls

[root@admin-node pillar]# cat schedule1.sls 
schedule:
  regular_job1:
    function: state.sls
    seconds: 60
    args:
      - os
这里定义了一个定时任务  

(4) schedule.sls:

[root@admin-node pillar]# cat schedule.sls 
schedule:
  regular_job:
    function: cmd.run
    seconds: 60
    args:
      - 'date >> /tmp/date.log'
这里定义了一个定时任务


三: modules定义:

我们在/salt/salt目录下mkdir了_modules目录,这里用来存放自动移的可执行的salt modules(saltstack内置了很多module)

[root@admin-node salt]# tree _modules/
_modules/
├── agent1.py
├── agent2.py
└── agent.py

0 directories, 3 files
agent.py内容如下:

[root@admin-node _modules]# cat agent.py 
#! /usr/bin/python
import os
def getcwd():
    return 'Test'


def showpara(string):
    return  string
定义好自己的modules后,我们通过saltutil.sync_modules同步一下

现在我们执行自定义的module如下:

[root@admin-node _modules]# salt '172.18.1.211' agent.getcwd
172.18.1.211:
    Test

四:returner定义

定义了returner,那么minion的执行结果不仅会上报给master,也会传送一份给我们定义的returner

[root@admin-node salt]# tree   _returners/
_returners/
├── local_return.py
└── testreturner.py

0 directories, 2 files

testreturner.py内容如下:

[root@admin-node _returners]# cat testreturner.py 
def __virtual__():
    return 'testreturner'

def returner(ret):
    fd = open("/tmp/testfile", 'a+')
    fd.write(str(ret))

"""
if __name__ == '__main__':
   returner('haha')
"""

定义好returner后,我们通过saltutil.sync_returners同步一下

我们测试一下:

[root@admin-node _returners]# salt '172.18.1.212' test.ping --return=testreturner
172.18.1.212:
    True
[root@admin-node _returners]# ssh root@172.18.1.212
root@172.18.1.212's password: 
Last login: Sun Dec  6 09:18:53 2015 from admin-node
[root@node2 ~]# cat /tmp/testfile 

{'fun_args': [], 'jid': '20151206091934674837', 'return': True, 'retcode': 0, 'success': True, 'fun': 'test.ping', 'id': '172.18.1.212'}
minion执行的结果作为参数调用我们定义的testreturner.py脚本中

五:reactor定义

大家可以了解下saltstack的event。我们可以利用reactor在minion初次连接上我们的master时(也可以在minion开始认证是或者其它事件发生时),执行一系列工作。

[root@admin-node salt]# tree reactor/
reactor/
└── firstconnection.sls

0 directories, 1 file

firstconnection.sls文件内容如下:

[root@admin-node reactor]# cat firstconnection.sls 
grains_sync:
   local.saltutil.sync_grains:
     - 'tgt': {{ data['id'] }}

modules_sync:
   local.saltutil.sync_modules:
     - 'tgt': {{ data['id'] }}

pillar_sync:
   local.saltutil.refresh_pillar:
     - 'tgt': {{ data['id'] }}

os:
  local.state.sls:
     - 'tgt': {{ data['id'] }}
     - arg:
        - os
根据我们在master配置文件中的定义:

reactor:
  - 'salt/minion/*/start':
    - /var/salt/reactor/firstconnection.sls
在'salt/minion/*/start事件发生时,执行firstconnection.sls中定义的方法'。

这里在minion首次认证启动时,master会对触发start事件的minion之行sync_grains、sync_modules、refresh_pillar、state.sls等module,当然也可以先sync_modules,然后执行自定义的module


六:配置例子

[root@admin-node salt]# tree
.
├── _grains
│   ├── role.py
│   └── test.py
├── _modules
│   ├── agent1.py
│   ├── agent2.py
│   └── agent.py
├── os
│   ├── commonfile
│   ├── file.jinja
│   ├── init.sls
│   ├── node1
│   ├── node2
│   ├── node3
│   └── node4
├── _returners
│   ├── local_return.py
│   └── testreturner.py
└── top.sls


top.ls文件内容:

[root@admin-node salt]# cat top.sls 
base:
  '*':
    - os


os目录内容:

[root@admin-node salt]# tree os
os
├── commonfile
├── file.jinja
├── init.sls
├── node1
├── node2
├── node3
└── node4

0 directories, 7 files
 node1-4是基于主机的特殊配置文件,而commonfile是公共配置文件

init.sls文件内容

[root@admin-node os]# cat init.sls 
configfilecopy:
   file:
     - managed
     - name: /root/{{ grains['hostname'] }}
     - source: salt://os/{{ grains['hostname'] }}
commonfile:
   file:
     - managed
     - name: /root/commonfile
     - source: salt://os/commonfile

这里配置文件我们分为公共配置文件commonfile和主机特殊配置文件configfilecopy,configfilecopy这个ID中对于某个特定的minion的配置文件则根据我们自定义的module获取的特定minion的hostname这个grain值来对应的{{ grains['hostname'] }},公共配置文件则定义在commonfile中,与minion的grains和pillar值没有关系。


如果我们想在配置文件中也使用jinja,则我们如下:

配置文件(使用jinja)

[root@admin-node os]# cat file.jinja 
hostname: {{ grains['hostname'] }}
{% if grains['hostname'] == 'node1' %}
line1
{% elif grains['hostname'] == 'node2' %}
line2
{% elif grains['hostname'] == 'node3' %}
line3
{% elif grains['hostname'] == 'node4' %}
line4
{% endif %}


top.sls文件中对应书写:

jinjafile:
   file:
     - managed
     - name: /root/testfile
     - source: salt://os/file.jinja
     - template: jinja




我们之前定义了reactor,minion首次连接到master时时会同步这些配置文件的,并且我们也定义了schedule,定时的minion会到master上同步自己的配置文件



七:event

我们执行下salt-run state.event pretty=True,我们可以看到打印出一系列事件。

例如,一台minion的认证开始:

{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-02T18:05:02.058304', 'act': 'pend', 'id': '172.18.1.213', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy5H2aLZ1QpaILqxwCoso\nruW6PgBkg9J7ovM89I1BZTC8QDfBNbjokdayMUwjznbU/BO8kjyFmDvaD338Hrdx\nhI1VZ9mocFfzYFZ5Igi7hEg+25XJVyEqtwMCuG3oy3QU8bKc6xrMZhXREkBQWTaY\nMYBpGX2NuIWeHRBpe6r6v6xMKG8lYJpjM5cR35vKEtN/Sf5o4uTcOLIJG9Stnb2B\nSB8N0raPpOXhbKQ2AZsUe98VD04Iq1ugL+ZQRqjfRcJEle2XPCyWYE8Fxcfo6MAv\nqgvGzPqd/U4QEl2RM3COOTh3piJKX/A3hrUoc566zn4hVEcvOze68jdSc5BuHLNS\nGQIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-02T18:05:12.071996', 'act': 'pend', 'id': '172.18.1.213', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy5H2aLZ1QpaILqxwCoso\nruW6PgBkg9J7ovM89I1BZTC8QDfBNbjokdayMUwjznbU/BO8kjyFmDvaD338Hrdx\nhI1VZ9mocFfzYFZ5Igi7hEg+25XJVyEqtwMCuG3oy3QU8bKc6xrMZhXREkBQWTaY\nMYBpGX2NuIWeHRBpe6r6v6xMKG8lYJpjM5cR35vKEtN/Sf5o4uTcOLIJG9Stnb2B\nSB8N0raPpOXhbKQ2AZsUe98VD04Iq1ugL+ZQRqjfRcJEle2XPCyWYE8Fxcfo6MAv\nqgvGzPqd/U4QEl2RM3COOTh3piJKX/A3hrUoc566zn4hVEcvOze68jdSc5BuHLNS\nGQIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-02T18:05:22.087882', 'act': 'pend', 'id': '172.18.1.213', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy5H2aLZ1QpaILqxwCoso\nruW6PgBkg9J7ovM89I1BZTC8QDfBNbjokdayMUwjznbU/BO8kjyFmDvaD338Hrdx\nhI1VZ9mocFfzYFZ5Igi7hEg+25XJVyEqtwMCuG3oy3QU8bKc6xrMZhXREkBQWTaY\nMYBpGX2NuIWeHRBpe6r6v6xMKG8lYJpjM5cR35vKEtN/Sf5o4uTcOLIJG9Stnb2B\nSB8N0raPpOXhbKQ2AZsUe98VD04Iq1ugL+ZQRqjfRcJEle2XPCyWYE8Fxcfo6MAv\nqgvGzPqd/U4QEl2RM3COOTh3piJKX/A3hrUoc566zn4hVEcvOze68jdSc5BuHLNS\nGQIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'salt/event/new_client', 'data': {'_stamp': '2015-12-02T18:05:25.106430'}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-02T18:05:32.103191', 'act': 'pend', 'id': '172.18.1.213', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy5H2aLZ1QpaILqxwCoso\nruW6PgBkg9J7ovM89I1BZTC8QDfBNbjokdayMUwjznbU/BO8kjyFmDvaD338Hrdx\nhI1VZ9mocFfzYFZ5Igi7hEg+25XJVyEqtwMCuG3oy3QU8bKc6xrMZhXREkBQWTaY\nMYBpGX2NuIWeHRBpe6r6v6xMKG8lYJpjM5cR35vKEtN/Sf5o4uTcOLIJG9Stnb2B\nSB8N0raPpOXhbKQ2AZsUe98VD04Iq1ugL+ZQRqjfRcJEle2XPCyWYE8Fxcfo6MAv\nqgvGzPqd/U4QEl2RM3COOTh3piJKX/A3hrUoc566zn4hVEcvOze68jdSc5BuHLNS\nGQIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-02T18:05:42.116831', 'act': 'pend', 'id': '172.18.1.213', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy5H2aLZ1QpaILqxwCoso\nruW6PgBkg9J7ovM89I1BZTC8QDfBNbjokdayMUwjznbU/BO8kjyFmDvaD338Hrdx\nhI1VZ9mocFfzYFZ5Igi7hEg+25XJVyEqtwMCuG3oy3QU8bKc6xrMZhXREkBQWTaY\nMYBpGX2NuIWeHRBpe6r6v6xMKG8lYJpjM5cR35vKEtN/Sf5o4uTcOLIJG9Stnb2B\nSB8N0raPpOXhbKQ2AZsUe98VD04Iq1ugL+ZQRqjfRcJEle2XPCyWYE8Fxcfo6MAv\nqgvGzPqd/U4QEl2RM3COOTh3piJKX/A3hrUoc566zn4hVEcvOze68jdSc5BuHLNS\nGQIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'salt/event/new_client', 'data': {'_stamp': '2015-12-02T18:05:50.911324'}}


打印出认证通过事件:

{'tag': 'salt/event/new_client', 'data': {'_stamp': '2015-12-02T21:31:17.367587'}}
------
{'tag': 'salt/key', 'data': {'_stamp': '2015-12-02T21:31:19.314526', 'act': 'accept', 'id': '172.18.1.214', 'result': True}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-02T21:31:24.996918', 'act': 'accept', 'id': '172.18.1.214', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqLYIXiAfspFIy1ifu2KL\neHxonFwEB8fqAGfhADgntCNpz6jY3gNTuP+B1PibjjDukkskJ9aXBIHBsoyWMUxt\n1//aVPV/2SMwqmrZKswc9OiLZqrtwi4fO64l6KOirnXEZSSdTOYJF3bZtUBs+prF\n6/ewe8SWHBT0dbbfkx0uBTUI+GDxR1FzkJBW09hUQ4MwHLzVmL2O0yjmyR5HdzzS\nPwJdkYJhxOnNSiWdc+XknBAuO+sD5g73QVQYhDrZLQ32LmgpdM8vwudkLmLsIVXi\nLoEiDM65Bay+dhqdS6Hn2vGzIpIoyB0UkI22HWWWTPEaLe4A8J8d9uIq8GFbYszq\nqwIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-02T21:31:25.026257', 'act': 'accept', 'id': '172.18.1.214', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqLYIXiAfspFIy1ifu2KL\neHxonFwEB8fqAGfhADgntCNpz6jY3gNTuP+B1PibjjDukkskJ9aXBIHBsoyWMUxt\n1//aVPV/2SMwqmrZKswc9OiLZqrtwi4fO64l6KOirnXEZSSdTOYJF3bZtUBs+prF\n6/ewe8SWHBT0dbbfkx0uBTUI+GDxR1FzkJBW09hUQ4MwHLzVmL2O0yjmyR5HdzzS\nPwJdkYJhxOnNSiWdc+XknBAuO+sD5g73QVQYhDrZLQ32LmgpdM8vwudkLmLsIVXi\nLoEiDM65Bay+dhqdS6Hn2vGzIpIoyB0UkI22HWWWTPEaLe4A8J8d9uIq8GFbYszq\nqwIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'minion_start', 'data': {'_stamp': '2015-12-02T21:31:25.207740', 'pretag': None, 'cmd': '_minion_event', 'tag': 'minion_start', 'data': 'Minion 172.18.1.214 started at Wed Dec  2 21:31:25 2015', 'id': '172.18.1.214'}}
------
{'tag': 'salt/minion/172.18.1.214/start', 'data': {'_stamp': '2015-12-02T21:31:25.209573', 'pretag': None, 'cmd': '_minion_event', 'tag': 'salt/minion/172.18.1.214/start', 'data': 'Minion 172.18.1.214 started at Wed Dec  2 21:31:25 2015', 'id': '172.18.1.214'}}
------
{'tag': 'salt/job/20151202213127254721/ret/172.18.1.214', 'data': {'tgt_type': 'glob', 'jid': '20151202213127254721', 'return': True, 'tgt': '172.18.1.214', 'schedule': '__mine_interval', 'cmd': '_return', 'pid': 23395, '_stamp': '2015-12-02T21:31:27.255343', 'arg': [], 'fun': 'mine.update', 'id': '172.18.1.214'}}

我们在master上执行一条命令:

{'tag': 'salt/event/new_client', 'data': {'_stamp': '2015-12-02T21:04:37.797695'}}
------
{'tag': '20151202210437808909', 'data': {'_stamp': '2015-12-02T21:04:37.809138', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151202210437808909/new', 'data': {'tgt_type': 'glob', 'jid': '20151202210437808909', 'tgt': '*', '_stamp': '2015-12-02T21:04:37.809305', 'user': 'root', 'arg': ['routetable'], 'fun': 'grains.item', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151202210437808909/ret/172.18.1.211', 'data': {'fun_args': ['routetable'], 'jid': '20151202210437808909', 'return': {'routetable': 'Not Show'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-02T21:04:37.824184', 'fun': 'grains.item', 'id': '172.18.1.211'}}
------
{'tag': 'salt/job/20151202210437808909/ret/172.18.1.214', 'data': {'fun_args': ['routetable'], 'jid': '20151202210437808909', 'return': {'routetable': 'Not Show'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-02T21:04:37.824184', 'fun': 'grains.item', 'id': '172.18.1.214'}}
------
{'tag': 'salt/job/20151202210437808909/ret/172.18.1.212', 'data': {'fun_args': ['routetable'], 'jid': '20151202210437808909', 'return': {'routetable': 'Not Show'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-02T21:04:37.827048', 'fun': 'grains.item', 'id': '172.18.1.212'}}
------
{'tag': 'salt/job/20151202210437808909/ret/172.18.1.213', 'data': {'fun_args': ['routetable'], 'jid': '20151202210437808909', 'return': {'routetable': 'Not Show'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-02T21:04:37.827085', 'fun': 'grains.item', 'id': '172.18.1.213'}}
------
从上面我们可以看到,多看几遍,我们可以看出我们执行的命令是:

salt  '*'  grains.item routetable


如果是minion定时任务的话,event流程和我们在master执行任务有所不同,如下:

{'tag': 'salt/job/20151206104253259315/ret/172.18.1.213', 'data': {'tgt_type': 'glob', 'jid': '20151206104253259315', 'return': {'file_|-configfilecopy_|-/root/node3_|-managed': {'comment': 'File /root/node3 is in the correct state', 'name': '/root/node3', 'start_time': '10:42:53.242323', 'result': True, 'duration': 8.0939999999999994, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '10:42:53.250520', 'result': True, 'duration': 1.9990000000000001, '__run_num__': 1, 'changes': {}}}, 'tgt': '172.18.1.213', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 9739, '_stamp': '2015-12-06T10:42:53.259906', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.213'}}
------
{'tag': 'salt/job/20151206104253384539/ret/172.18.1.211', 'data': {'tgt_type': 'glob', 'jid': '20151206104253384539', 'return': {'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '10:42:53.378781', 'result': True, 'duration': 2.0619999999999998, '__run_num__': 1, 'changes': {}}, 'file_|-configfilecopy_|-/root/node1_|-managed': {'comment': 'File /root/node1 is in the correct state', 'name': '/root/node1', 'start_time': '10:42:53.365468', 'result': True, 'duration': 13.202999999999999, '__run_num__': 0, 'changes': {}}}, 'tgt': '172.18.1.211', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 28766, '_stamp': '2015-12-06T10:42:53.385136', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.211'}}
------
{'tag': 'salt/job/20151206104253460737/ret/172.18.1.212', 'data': {'tgt_type': 'glob', 'jid': '20151206104253460737', 'return': {'file_|-configfilecopy_|-/root/node2_|-managed': {'comment': 'File /root/node2 is in the correct state', 'name': '/root/node2', 'start_time': '10:42:53.446600', 'result': True, 'duration': 8.3279999999999994, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '10:42:53.455030', 'result': True, 'duration': 1.8620000000000001, '__run_num__': 1, 'changes': {}}}, 'tgt': '172.18.1.212', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 10797, '_stamp': '2015-12-06T10:42:53.461228', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.212'}}
------
{'tag': 'salt/job/20151206104254194806/ret/172.18.1.214', 'data': {'tgt_type': 'glob', 'jid': '20151206104254194806', 'return': {'file_|-configfilecopy_|-/root/node4_|-managed': {'comment': 'File /root/node4 is in the correct state', 'name': '/root/node4', 'start_time': '10:42:54.165390', 'result': True, 'duration': 8.423, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '10:42:54.173934', 'result': True, 'duration': 2.1440000000000001, '__run_num__': 1, 'changes': {}}}, 'tgt': '172.18.1.214', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 22884, '_stamp': '2015-12-06T10:42:54.195340', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.214'}}

作为补充我们打印出salt '*' state.ls os和定时任务的state.ls os事件,具有一些差异性:

salt '*' state.ls  os

{'tag': 'salt/event/new_client', 'data': {'_stamp': '2015-12-07T03:45:22.373095'}}
------
{'tag': '20151207034522384332', 'data': {'_stamp': '2015-12-07T03:45:22.384562', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151207034522384332/new', 'data': {'tgt_type': 'glob', 'jid': '20151207034522384332', 'tgt': '*', '_stamp': '2015-12-07T03:45:22.384745', 'user': 'root', 'arg': ['os'], 'fun': 'state.sls', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151207034522384332/ret/172.18.1.211', 'data': {'fun_args': ['os'], 'jid': '20151207034522384332', 'return': {'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:22.697042', 'result': True, 'duration': 2.3399999999999999, '__run_num__': 1, 'changes': {}}, 'file_|-configfilecopy_|-/root/node1_|-managed': {'comment': 'File /root/node1 is in the correct state', 'name': '/root/node1', 'start_time': '03:45:22.684113', 'result': True, 'duration': 12.819000000000001, '__run_num__': 0, 'changes': {}}}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-07T03:45:22.634943', 'fun': 'state.sls', 'id': '172.18.1.211', 'out': 'highstate'}}
------
{'tag': 'salt/job/20151207034522384332/ret/172.18.1.213', 'data': {'fun_args': ['os'], 'jid': '20151207034522384332', 'return': {'file_|-configfilecopy_|-/root/node3_|-managed': {'comment': 'File /root/node3 is in the correct state', 'name': '/root/node3', 'start_time': '03:45:22.685693', 'result': True, 'duration': 8.9819999999999993, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:22.694825', 'result': True, 'duration': 2.2320000000000002, '__run_num__': 1, 'changes': {}}}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-07T03:45:22.636391', 'fun': 'state.sls', 'id': '172.18.1.213', 'out': 'highstate'}}
------
{'tag': 'salt/job/20151207034522384332/ret/172.18.1.214', 'data': {'fun_args': ['os'], 'jid': '20151207034522384332', 'return': {'file_|-configfilecopy_|-/root/node4_|-managed': {'comment': 'File /root/node4 is in the correct state', 'name': '/root/node4', 'start_time': '03:45:22.676061', 'result': True, 'duration': 9.2729999999999997, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:22.685486', 'result': True, 'duration': 2.0510000000000002, '__run_num__': 1, 'changes': {}}}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-07T03:45:22.638505', 'fun': 'state.sls', 'id': '172.18.1.214', 'out': 'highstate'}}
------
{'tag': 'salt/job/20151207034522384332/ret/172.18.1.212', 'data': {'fun_args': ['os'], 'jid': '20151207034522384332', 'return': {'file_|-configfilecopy_|-/root/node2_|-managed': {'comment': 'File /root/node2 is in the correct state', 'name': '/root/node2', 'start_time': '03:45:22.696244', 'result': True, 'duration': 8.3650000000000002, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:22.704759', 'result': True, 'duration': 2.6339999999999999, '__run_num__': 1, 'changes': {}}}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-07T03:45:22.643268', 'fun': 'state.sls', 'id': '172.18.1.212', 'out': 'highstate'}}
------
<span style="font-family: Arial, Helvetica, sans-serif;">定时任务 state.ls os</span>
<span style="font-family: Arial, Helvetica, sans-serif;">{'tag': 'salt/job/20151207034553687201/ret/172.18.1.212', 'data': {'tgt_type': 'glob', 'jid': '20151207034553687201', 'return': {'file_|-configfilecopy_|-/root/node2_|-managed': {'comment': 'File /root/node2 is in the correct state', 'name': '/root/node2', 'start_time': '03:45:53.740729', 'result': True, 'duration': 7.8040000000000003, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:53.748636', 'result': True, 'duration': 3.282, '__run_num__': 1, 'changes': {}}}, 'tgt': '172.18.1.212', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 1222, '_stamp': '2015-12-07T03:45:53.687642', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.212'}}</span>
------
{'tag': 'salt/job/20151207034554691883/ret/172.18.1.213', 'data': {'tgt_type': 'glob', 'jid': '20151207034554691883', 'return': {'file_|-configfilecopy_|-/root/node3_|-managed': {'comment': 'File /root/node3 is in the correct state', 'name': '/root/node3', 'start_time': '03:45:54.743785', 'result': True, 'duration': 7.8719999999999999, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:54.751764', 'result': True, 'duration': 1.8919999999999999, '__run_num__': 1, 'changes': {}}}, 'tgt': '172.18.1.213', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 2257, '_stamp': '2015-12-07T03:45:54.692412', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.213'}}
------
{'tag': 'salt/job/20151207034554694545/ret/172.18.1.214', 'data': {'tgt_type': 'glob', 'jid': '20151207034554694545', 'return': {'file_|-configfilecopy_|-/root/node4_|-managed': {'comment': 'File /root/node4 is in the correct state', 'name': '/root/node4', 'start_time': '03:45:54.734839', 'result': True, 'duration': 7.7690000000000001, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:54.742711', 'result': True, 'duration': 1.742, '__run_num__': 1, 'changes': {}}}, 'tgt': '172.18.1.214', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 11129, '_stamp': '2015-12-07T03:45:54.695016', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.214'}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-07T03:45:55.356802', 'act': 'pend', 'id': '172.17.42.1', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsaqek3b3yzqkRnkXfI8i\nCGUQ4cEUVw+ZG6pp/jbubIw+WtiYRovMQo7aRDjjmxK2yiTNn0/fr+vAHOaAUkMm\nNoywHEst7GJqSeQUAcoeNGWI3+9SeYM7oco5QXx02/tvrbaea0JToxG9ubVJtX1Y\n3PEFCgqqf4o2kFcHsunRGx8gDQPglDswio/KLGgFGTxCz6GDuOW7tAxJl86oyeh9\noeLMlJn4Qo3qihaZvjR1+ykO0nKBb26jT7bMDl7UD/FFZJzGRPrK/hvln3q/IZ6T\n/03G953p9ngOT+ozocw+eRIrYynHbEr/ej6cqwTUFwZJtMu6FbRRlM1aZX8c5THe\ntwIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}
------
{'tag': 'salt/job/20151207034555696317/ret/172.18.1.211', 'data': {'tgt_type': 'glob', 'jid': '20151207034555696317', 'return': {'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '03:45:55.759219', 'result': True, 'duration': 1.7809999999999999, '__run_num__': 1, 'changes': {}}, 'file_|-configfilecopy_|-/root/node1_|-managed': {'comment': 'File /root/node1 is in the correct state', 'name': '/root/node1', 'start_time': '03:45:55.745653', 'result': True, 'duration': 13.457000000000001, '__run_num__': 0, 'changes': {}}}, 'tgt': '172.18.1.211', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 26781, '_stamp': '2015-12-07T03:45:55.696957', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.211'}}
------
{'tag': 'salt/auth', 'data': {'_stamp': '2015-12-07T03:46:05.371313', 'act': 'pend', 'id': '172.17.42.1', 'pub': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsaqek3b3yzqkRnkXfI8i\nCGUQ4cEUVw+ZG6pp/jbubIw+WtiYRovMQo7aRDjjmxK2yiTNn0/fr+vAHOaAUkMm\nNoywHEst7GJqSeQUAcoeNGWI3+9SeYM7oco5QXx02/tvrbaea0JToxG9ubVJtX1Y\n3PEFCgqqf4o2kFcHsunRGx8gDQPglDswio/KLGgFGTxCz6GDuOW7tAxJl86oyeh9\noeLMlJn4Qo3qihaZvjR1+ykO0nKBb26jT7bMDl7UD/FFZJzGRPrK/hvln3q/IZ6T\n/03G953p9ngOT+ozocw+eRIrYynHbEr/ej6cqwTUFwZJtMu6FbRRlM1aZX8c5THe\ntwIDAQAB\n-----END PUBLIC KEY-----\n', 'result': True}}



master执行salt-key -d minionId的事件有:

salt/event/new_client	{
    "_stamp": "2015-12-02T21:13:11.767667"
}
salt/key	{
    "_stamp": "2015-12-02T21:13:14.183500", 
    "act": "delete", 
    "id": "172.18.1.214", 
    "result": true
}
key	{
    "_stamp": "2015-12-02T21:13:21.230747", 
    "rotate_aes_key": true
}


除了saltstack命令salt-run state.event可以打印所有的event,我们也可以自行写程序去连接saltstack的unix domain socket,下面给出两个半成品的程序:

import salt.utils.event
event = salt.utils.event.MasterEvent('/var/run/salt/master')
for eachevent in event.iter_events(full=True):
    print eachevent
    print "------"

import salt.utils.event
import re
import signal, time
import sys
#{'tag': 'salt/job/20151202210437808909/new', 'data': {'tgt_type': 'glob', 'jid': '20151202210437808909', 'tgt': '*', '_stamp': '2015-12-02T21:04:37.809305', 'user': 'root', 'arg': ['routetable'], 'fun': 'grains.item', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
#{'tag': 'salt/job/20151202210437808909/ret/172.18.1.213', 'data': {'fun_args': ['routetable'], 'jid': '20151202210437808909', 'return': {'routetable': 'Not Show'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-02T21:04:37.827085', 'fun': 'grains.item', 'id': '172.18.1.213'}}
import os

def handler(num1, num2):
    signal.signal(signal.SIG_IGN, handler)
    print 'We are in signal handler'
    print 'Job Not Ret: '+str(record[jid])
    print ' Job Failed: '+str(failedrecord[jid])
    for item in failedrecord[jid]:
        os.system('salt '+ str(item) + ' state.sls os')
    for item in record[jid]:
        os.system('salt '+ str(item) + ' state.sls os')
    os._exit(0)
signal.signal(signal.SIGCLD, handler)

#fd = open('/var/log/record', 'a+')
#os.dup2(fd.fileno(), sys.stdout.fileno())
#os.dup2(fd.fileno(), sys.stderr.fileno())

try:
   pid = os.fork()
   if pid == 0:
      time.sleep(2)
      try:
         os.execl('/usr/bin/salt', 'salt', '*', 'state.sls', 'os')
      except:
         print 'exec error!'
         os._exit(1)
except OSError:
   print 'first fork error!'
   os._exit(1)
event = salt.utils.event.MasterEvent('/var/run/salt/master')
flag=False
reg=re.compile('salt/job/([0-9]+)/new')
reg1=reg
#a process to exec. command, but will sleep some time
#another process listen the event
#if we use this method, we can filter the event through func. name
record={}
failedrecord={}
jid = 0


#try:
for eachevent in event.iter_events(tag='salt/job',full=True):
    #time.sleep(20)
    #print eachevent
    #print "------"
    eachevent=dict(eachevent)
    #print eachevent
    result = reg.findall(eachevent['tag'])
    if not flag and result:
       flag = True
       #record = {}
       #failedrecord = {}
       jid = result[0]
       print "   job_id: " + jid
       print "  Command: " + dict(eachevent['data'])['fun'] + ' ' + str(dict(eachevent['data'])['arg'])
       print "    RunAs: " + dict(eachevent['data'])['user'] 
       print "exec_time: " + dict(eachevent['data'])['_stamp'] 
       print "host_list: " + str(dict(eachevent['data'])['minions'])
       record[jid]=eachevent['data']['minions']
       failedrecord[jid]=[]
       reg1 = re.compile('salt/job/'+jid+'/ret/([0-9.]+)')
       #print jid
    else:
       result = reg1.findall(eachevent['tag'])
       #print result.group(0)
       if result:
          record[jid].remove(result[0])
          if not dict(eachevent['data'])['success']:
             #print result[0]
             #record[jid].remove(result[0])
             #print 'Job Not Ret: '+str(record[jid])
             #print ' Job Failed: '+str(failedrecord[jid]) 
             #if not record[jid]:
             #   break
             failedrecord[jid].append(result[0])
             #print ' Job Failed: '+str(failedrecord[jid])
          #else:
             #failedrecord[jid].append(result[0])
             #print 'Job Not Ret: '+str(record[jid])
#except:
#   print 'we in except'
"""
   print 'Job Not Ret: '+str(record[jid])
   print ' Job Failed: '+str(failedrecord[jid])
   for item in failedrecord[jid]:
       os.system('salt '+ str(item) + ' state.sls os')
   for item in record[jid]:
       os.system('salt '+ str(item) + ' state.sls os')
   os._exit(0)
"""
#print 'Job Not Ret: '+str(record[jid])
#print ' Job Failed: '+str(failedrecord[jid]) 
"""
if fnmatch.fnmatch(eachevent['tag'], 'salt/job/*/new'):
print eachevent['data']['minions']
elif fnmatch.fnmatch(eachevent['tag'], 'salt/job/*/ret/*'):
print eachevent['data']['jid']
print eachevent['data']['return']
"""

这个程序主要是监听我们执行命令的返回结果,保存所有执行失败的任务和未返回的任务,并且重新执行



八:saltstack  jobs管理

salt-run jobs.lookup_jid

我们可以通过上面这个命令去查找某个任务


对于我们在salt上执行的命令,下面我们举两个例子:

(1)我们执行 salt '*' test.ping , event事件如下图所示:

{'tag': '20151208054645984283', 'data': {'_stamp': '2015-12-08T05:46:45.984507', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151208054645984283/new', 'data': {'tgt_type': 'glob', 'jid': '20151208054645984283', 'tgt': '*', '_stamp': '2015-12-08T05:46:45.984681', 'user': 'root', 'arg': [], 'fun': 'test.ping', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151208054645984283/ret/172.18.1.211', 'data': {'fun_args': [], 'jid': '20151208054645984283', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-08T05:46:46.039656', 'fun': 'test.ping', 'id': '172.18.1.211'}}
------
{'tag': 'salt/job/20151208054645984283/ret/172.18.1.213', 'data': {'fun_args': [], 'jid': '20151208054645984283', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-08T05:46:46.039669', 'fun': 'test.ping', 'id': '172.18.1.213'}}
------
{'tag': 'salt/job/20151208054645984283/ret/172.18.1.212', 'data': {'fun_args': [], 'jid': '20151208054645984283', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-08T05:46:46.048971', 'fun': 'test.ping', 'id': '172.18.1.212'}}
------
我们可以看到job id为:

20151208054645984283

我们执行:

salt-run jobs.lookup_jid 20151208054645984283

结果如下:

[root@admin-node salt]# salt-run jobs.lookup_jid 20151208054645984283
172.18.1.211:
    True
172.18.1.212:
    True
172.18.1.213:
    True

(2)我们执行salt "*" state.sls os, event事件如下:

{'tag': '20151208055434834396', 'data': {'_stamp': '2015-12-08T05:54:34.834608', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151208055434834396/new', 'data': {'tgt_type': 'glob', 'jid': '20151208055434834396', 'tgt': '*', '_stamp': '2015-12-08T05:54:34.834764', 'user': 'root', 'arg': ['os'], 'fun': 'state.sls', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}
------
{'tag': 'salt/job/20151208055434834396/ret/172.18.1.213', 'data': {'fun_args': ['os'], 'jid': '20151208055434834396', 'return': {'file_|-configfilecopy_|-/root/node3_|-managed': {'comment': 'File /root/node3 is in the correct state', 'name': '/root/node3', 'start_time': '05:54:35.162360', 'result': True, 'duration': 11.856999999999999, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '05:54:35.174326', 'result': True, 'duration': 1.9179999999999999, '__run_num__': 1, 'changes': {}}}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-08T05:54:35.893811', 'fun': 'state.sls', 'id': '172.18.1.213', 'out': 'highstate'}}
------
{'tag': 'salt/job/20151208055434834396/ret/172.18.1.212', 'data': {'fun_args': ['os'], 'jid': '20151208055434834396', 'return': {'file_|-configfilecopy_|-/root/node2_|-managed': {'comment': 'File /root/node2 is in the correct state', 'name': '/root/node2', 'start_time': '05:54:35.999054', 'result': True, 'duration': 7.9699999999999998, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '05:54:36.007128', 'result': True, 'duration': 2.02, '__run_num__': 1, 'changes': {}}}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-08T05:54:35.944885', 'fun': 'state.sls', 'id': '172.18.1.212', 'out': 'highstate'}}
------
{'tag': 'salt/job/20151208055434834396/ret/172.18.1.211', 'data': {'fun_args': ['os'], 'jid': '20151208055434834396', 'return': {'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '05:54:36.155804', 'result': True, 'duration': 2.052, '__run_num__': 1, 'changes': {}}, 'file_|-configfilecopy_|-/root/node1_|-managed': {'comment': 'File /root/node1 is in the correct state', 'name': '/root/node1', 'start_time': '05:54:36.143253', 'result': True, 'duration': 12.446999999999999, '__run_num__': 0, 'changes': {}}}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-08T05:54:36.093437', 'fun': 'state.sls', 'id': '172.18.1.211', 'out': 'highstate'}}
我们可以看出job id为:

20151208055434834396

执行命令结果为:

[root@admin-node salt]# salt-run jobs.lookup_jid 20151208055434834396
data:
    ----------
    172.18.1.211:
        ----------
        file_|-commonfile_|-/root/commonfile_|-managed:
            ----------
            __run_num__:
                1
            changes:
                ----------
            comment:
                File /root/commonfile is in the correct state
            duration:
                2.052
            name:
                /root/commonfile
            result:
                True
            start_time:
                05:54:36.155804
        file_|-configfilecopy_|-/root/node1_|-managed:
            ----------
            __run_num__:
                0
            changes:
                ----------
            comment:
                File /root/node1 is in the correct state
            duration:
                12.447
            name:
                /root/node1
            result:
                True
            start_time:
                05:54:36.143253
    172.18.1.212:
        ----------
        file_|-commonfile_|-/root/commonfile_|-managed:
            ----------
            __run_num__:
                1
            changes:
                ----------
            comment:
                File /root/commonfile is in the correct state
            duration:
                2.02
            name:
                /root/commonfile
            result:
                True
            start_time:
                05:54:36.007128
        file_|-configfilecopy_|-/root/node2_|-managed:
            ----------
            __run_num__:
                0
            changes:
                ----------
            comment:
                File /root/node2 is in the correct state
            duration:
                7.97
            name:
                /root/node2
            result:
                True
            start_time:
                05:54:35.999054
    172.18.1.213:
        ----------
        file_|-commonfile_|-/root/commonfile_|-managed:
            ----------
            __run_num__:
                1
            changes:
                ----------
            comment:
                File /root/commonfile is in the correct state
            duration:
                1.918
            name:
                /root/commonfile
            result:
                True
            start_time:
                05:54:35.174326
        file_|-configfilecopy_|-/root/node3_|-managed:
            ----------
            __run_num__:
                0
            changes:
                ----------
            comment:
                File /root/node3 is in the correct state
            duration:
                11.857
            name:
                /root/node3
            result:
                True
            start_time:
                05:54:35.162360

(3)对于minion上的schedule任务:

event事件如下:

{'tag': 'salt/job/20151208060157760050/ret/172.18.1.213', 'data': {'tgt_type': 'glob', 'jid': '20151208060157760050', 'return': {'file_|-configfilecopy_|-/root/node3_|-managed': {'comment': 'File /root/node3 is in the correct state', 'name': '/root/node3', 'start_time': '06:01:57.810217', 'result': True, 'duration': 8.6470000000000002, '__run_num__': 0, 'changes': {}}, 'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '06:01:57.819016', 'result': True, 'duration': 2.6190000000000002, '__run_num__': 1, 'changes': {}}}, 'tgt': '172.18.1.213', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 2964, '_stamp': '2015-12-08T06:01:57.760588', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.213'}}
------
{'tag': 'salt/job/20151208060200764872/ret/172.18.1.211', 'data': {'tgt_type': 'glob', 'jid': '20151208060200764872', 'return': {'file_|-commonfile_|-/root/commonfile_|-managed': {'comment': 'File /root/commonfile is in the correct state', 'name': '/root/commonfile', 'start_time': '06:02:00.827131', 'result': True, 'duration': 2.1499999999999999, '__run_num__': 1, 'changes': {}}, 'file_|-configfilecopy_|-/root/node1_|-managed': {'comment': 'File /root/node1 is in the correct state', 'name': '/root/node1', 'start_time': '06:02:00.813057', 'result': True, 'duration': 13.936999999999999, '__run_num__': 0, 'changes': {}}}, 'tgt': '172.18.1.211', 'schedule': 'regular_job1', 'cmd': '_return', 'pid': 28763, '_stamp': '2015-12-08T06:02:00.765747', 'arg': [], 'fun': 'state.sls', 'id': '172.18.1.211'}}

选取其中一个job id:

20151208060200764872

执行命令,结果如图:

[root@admin-node salt]# salt-run jobs.lookup_jid 20151208060200764872
172.18.1.211:
    ----------
    file_|-commonfile_|-/root/commonfile_|-managed:
        ----------
        __run_num__:
            1
        changes:
            ----------
        comment:
            File /root/commonfile is in the correct state
        duration:
            2.15
        name:
            /root/commonfile
        result:
            True
        start_time:
            06:02:00.827131
    file_|-configfilecopy_|-/root/node1_|-managed:
        ----------
        __run_num__:
            0
        changes:
            ----------
        comment:
            File /root/node1 is in the correct state
        duration:
            13.937
        name:
            /root/node1
        result:
            True
        start_time:
            06:02:00.813057

这里补充一点:look job id打印出来的信息其实就是对应job id下minion的返回event事件的return对应的值

salt '*' test.ping --verbose

加上verbose可以打印出job id

 

九:runners

saltstack runner包括functions,每个function都是一个runner,我们可以通过salt-run runnerModule.function方式去执行我们自定义的runner函数

下面举个例子:

[root@admin-node ~]# salt-run manage.up

- 172.18.1.211

- 172.18.1.212

- 172.18.1.213

- 172.18.1.214

[root@admin-node ~]# 



号外:
下面我们打印出执行salt-run manage.down & salt-run manage.up这两个runner执行的event
我们可以看到manage.down和manage.up会引起一个event,event格式为:salt/run/jobid/new,down & up会执行一个salt '*' test.ping,这样会同时导致我们前述的多个event,根据test.ping返回的结果,我们的manage.up & manage.down就会返回,event格式为:salt/run/jobid/ret,return后面跟着我们所有存活或者非存活的minion列表

salt/event/new_client {"_stamp": "2015-12-10T08:41:29.009211"}

salt/run/20151210084129008080/new {"fun": "runner.manage.down", "jid": "20151210084129008080", "user": "root", "_stamp": "2015-12-10T08:41:29.010338"}

salt/event/new_client {"_stamp": "2015-12-10T08:41:29.097974"}

20151210084129108800 {"_stamp": "2015-12-10T08:41:29.109032", "minions": ["172.18.1.212", "172.18.1.214", "172.18.1.213", "172.18.1.211"]}

salt/job/20151210084129108800/new {"tgt_type": "glob", "jid": "20151210084129108800", "tgt": "*", "_stamp": "2015-12-10T08:41:29.109217", "user": "root", "arg": [], "fun": "test.ping", "minions": ["172.18.1.212", "172.18.1.214", "172.18.1.213", "172.18.1.211"]}

salt/job/20151210084129108800/ret/172.18.1.213 {"fun_args": [], "jid": "20151210084129108800", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:29.163739", "fun": "test.ping", "id": "172.18.1.213"}

salt/job/20151210084129108800/ret/172.18.1.214 {"fun_args": [], "jid": "20151210084129108800", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:29.166692", "fun": "test.ping", "id": "172.18.1.214"}

salt/job/20151210084129108800/ret/172.18.1.211 {"fun_args": [], "jid": "20151210084129108800", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:29.169561", "fun": "test.ping", "id": "172.18.1.211"}

salt/job/20151210084129108800/ret/172.18.1.212 {"fun_args": [], "jid": "20151210084129108800", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:29.171280", "fun": "test.ping", "id": "172.18.1.212"}

salt/event/new_client {"_stamp": "2015-12-10T08:41:29.184679"}

salt/run/20151210084129008080/ret {"jid": "20151210084129008080", "return": [], "success": true, "_stamp": "2015-12-10T08:41:29.237725", "user": "root", "fun": "runner.manage.down"}

salt/auth {"_stamp": "2015-12-10T08:41:32.561693", "act": "pend", "id": "172.17.42.1", "pub": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsaqek3b3yzqkRnkXfI8i\nCGUQ4cEUVw+ZG6pp/jbubIw+WtiYRovMQo7aRDjjmxK2yiTNn0/fr+vAHOaAUkMm\nNoywHEst7GJqSeQUAcoeNGWI3+9SeYM7oco5QXx02/tvrbaea0JToxG9ubVJtX1Y\n3PEFCgqqf4o2kFcHsunRGx8gDQPglDswio/KLGgFGTxCz6GDuOW7tAxJl86oyeh9\noeLMlJn4Qo3qihaZvjR1+ykO0nKBb26jT7bMDl7UD/FFZJzGRPrK/hvln3q/IZ6T\n/03G953p9ngOT+ozocw+eRIrYynHbEr/ej6cqwTUFwZJtMu6FbRRlM1aZX8c5THe\ntwIDAQAB\n-----END PUBLIC KEY-----\n", "result": true}

salt/job/20151210084134399466/ret/172.18.1.214 {"tgt_type": "glob", "jid": "20151210084134399466", "return": {"file_|-configfilecopy_|-/root/node4_|-managed": {"comment": "File /root/node4 is in the correct state", "name": "/root/node4", "start_time": "08:41:34.438295", "result": true, "duration": 8.6039999999999992, "__run_num__": 0, "changes": {}}, "file_|-commonfile_|-/root/commonfile_|-managed": {"comment": "File /root/commonfile is in the correct state", "name": "/root/commonfile", "start_time": "08:41:34.447036", "result": true, "duration": 2.1459999999999999, "__run_num__": 1, "changes": {}}}, "tgt": "172.18.1.214", "schedule": "regular_job1", "cmd": "_return", "pid": 15238, "_stamp": "2015-12-10T08:41:34.400008", "arg": [], "fun": "state.sls", "id": "172.18.1.214"}

salt/event/new_client {"_stamp": "2015-12-10T08:41:34.956901"}

salt/run/20151210084134955817/new {"fun": "runner.manage.up", "jid": "20151210084134955817", "user": "root", "_stamp": "2015-12-10T08:41:34.957633"}

salt/event/new_client {"_stamp": "2015-12-10T08:41:35.044808"}

20151210084135056602 {"_stamp": "2015-12-10T08:41:35.056850", "minions": ["172.18.1.212", "172.18.1.214", "172.18.1.213", "172.18.1.211"]}

salt/job/20151210084135056602/new {"tgt_type": "glob", "jid": "20151210084135056602", "tgt": "*", "_stamp": "2015-12-10T08:41:35.056996", "user": "root", "arg": [], "fun": "test.ping", "minions": ["172.18.1.212", "172.18.1.214", "172.18.1.213", "172.18.1.211"]}

salt/job/20151210084135056602/ret/172.18.1.214 {"fun_args": [], "jid": "20151210084135056602", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:35.112318", "fun": "test.ping", "id": "172.18.1.214"}

salt/job/20151210084135056602/ret/172.18.1.213 {"fun_args": [], "jid": "20151210084135056602", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:35.112170", "fun": "test.ping", "id": "172.18.1.213"}

salt/job/20151210084135056602/ret/172.18.1.212 {"fun_args": [], "jid": "20151210084135056602", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:35.114612", "fun": "test.ping", "id": "172.18.1.212"}

salt/job/20151210084135056602/ret/172.18.1.211 {"fun_args": [], "jid": "20151210084135056602", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-12-10T08:41:35.121683", "fun": "test.ping", "id": "172.18.1.211"}

salt/event/new_client {"_stamp": "2015-12-10T08:41:35.133430"}

salt/run/20151210084134955817/ret {"jid": "20151210084134955817", "return": ["172.18.1.211", "172.18.1.212", "172.18.1.213", "172.18.1.214"], "success": true, "_stamp": "2015-12-10T08:41:35.186572", "user": "root", "fun": "runner.manage.up"}

salt/auth {"_stamp": "2015-12-10T08:41:42.575620", "act": "pend", "id": "172.17.42.1", "pub": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsaqek3b3yzqkRnkXfI8i\nCGUQ4cEUVw+ZG6pp/jbubIw+WtiYRovMQo7aRDjjmxK2yiTNn0/fr+vAHOaAUkMm\nNoywHEst7GJqSeQUAcoeNGWI3+9SeYM7oco5QXx02/tvrbaea0JToxG9ubVJtX1Y\n3PEFCgqqf4o2kFcHsunRGx8gDQPglDswio/KLGgFGTxCz6GDuOW7tAxJl86oyeh9\noeLMlJn4Qo3qihaZvjR1+ykO0nKBb26jT7bMDl7UD/FFZJzGRPrK/hvln3q/IZ6T\n/03G953p9ngOT+ozocw+eRIrYynHbEr/ej6cqwTUFwZJtMu6FbRRlM1aZX8c5THe\ntwIDAQAB\n-----END PUBLIC KEY-----\n", "result": true}



下面进入正题:

(1)我们在master配置文件中添加着一行数据:

# Add any additional locations to look for master runners:

#runner_dirs: []

runner_dirs: [/srv/salt/_runners]


用来指定我们存放runner的路径,当然可以指定多个路径

(2)我们在/srv/salt/_runners创建我们自己的runner:
这里我们是模仿manage.up写的

[root@admin-node _runners]# cat mymanage.py

#! /usr/bin/python

import salt.client


def up():

    """

    This is the same as manage.up

    """

    uplist=[]

    client = salt.client.LocalClient(__opts__['conf_file'])

    minions = client.cmd('*', 'test.ping', timeout=1)

    for minion in sorted(minions):

        if minion:

           uplist.append(minion)

    return uplist

[root@admin-node _runners]# 


(3)然后我们重启下salt-master,执行命令salt-run my manage.up,结果如下:

[root@admin-node _runners]# salt-run mymanage.up

- 172.18.1.211

- 172.18.1.212

- 172.18.1.213

- 172.18.1.214

[root@admin-node _runners]# 


打印出对应event:

{'tag': 'salt/event/new_client', 'data': {'_stamp': '2015-12-10T14:21:18.560501'}}

------

{'tag': 'salt/run/20151210142118559053/new', 'data': {'fun': 'runner.mymanage.up', 'jid': '20151210142118559053', 'user': 'root', '_stamp': '2015-12-10T14:21:18.561737'}}

------

{'tag': 'salt/event/new_client', 'data': {'_stamp': '2015-12-10T14:21:18.657889'}}

------

{'tag': '20151210142118670150', 'data': {'_stamp': '2015-12-10T14:21:18.670356', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}

------

{'tag': 'salt/job/20151210142118670150/new', 'data': {'tgt_type': 'glob', 'jid': '20151210142118670150', 'tgt': '*', '_stamp': '2015-12-10T14:21:18.670502', 'user': 'root', 'arg': [], 'fun': 'test.ping', 'minions': ['172.18.1.212', '172.18.1.214', '172.18.1.213', '172.18.1.211']}}

------

{'tag': 'salt/job/20151210142118670150/ret/172.18.1.214', 'data': {'fun_args': [], 'jid': '20151210142118670150', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-10T14:21:18.724722', 'fun': 'test.ping', 'id': '172.18.1.214'}}

------

{'tag': 'salt/job/20151210142118670150/ret/172.18.1.212', 'data': {'fun_args': [], 'jid': '20151210142118670150', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-10T14:21:18.729889', 'fun': 'test.ping', 'id': '172.18.1.212'}}

------

{'tag': 'salt/job/20151210142118670150/ret/172.18.1.213', 'data': {'fun_args': [], 'jid': '20151210142118670150', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-10T14:21:18.731082', 'fun': 'test.ping', 'id': '172.18.1.213'}}

------

{'tag': 'salt/job/20151210142118670150/ret/172.18.1.211', 'data': {'fun_args': [], 'jid': '20151210142118670150', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2015-12-10T14:21:18.734312', 'fun': 'test.ping', 'id': '172.18.1.211'}}

------

{'tag': 'salt/run/20151210142118559053/ret', 'data': {'jid': '20151210142118559053', 'return': ['172.18.1.211', '172.18.1.212', '172.18.1.213', '172.18.1.214'], 'success': True, '_stamp': '2015-12-10T14:21:18.772064', 'user': 'root', 'fun': 'runner.mymanage.up'}}

------


我们可以看到salt-run mymanage.up的执行流程与salt-run manage.up的流程是一样的