天天看點

saltstack相關的一些總結

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 寫了兩個腳本,内容如下:

[[email protected] _grains]# cat test.py 
#! /uar/bin/python
def gettest():
    grains={}
    grains['software']='http'
    return grains
[[email protected] _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:

[[email protected] _grains]# salt '172.18.1.211' grains.item hostname
172.18.1.211:
    ----------
    hostname:
        node1
[[email protected] _grains]# salt '172.18.1.211' grains.item software
172.18.1.211:
    ----------
    software:
        http
           

如上圖所示,我們定義的hostname和software兩個grains正确顯示了

二:pillar的定義

pillar定義好後,可以通過指令saltutil.refresh_pillar

[[email protected] srv]# tree pillar/
pillar/
├── schedule1.sls
├── schedule.sls
├── test.sls
└── top.sls

0 directories, 4 files
           

整個pillar目錄的結構如上所示

(1)top.sls:

[[email protected] pillar]# cat top.sls 
base:
  '*':
    - test
    - schedule1
           

  (2) test.sls:

[[email protected] 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

[[email protected] pillar]# cat schedule1.sls 
schedule:
  regular_job1:
    function: state.sls
    seconds: 60
    args:
      - os
           

這裡定義了一個定時任務  

(4) schedule.sls:

[[email protected] 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)

[[email protected] salt]# tree _modules/
_modules/
├── agent1.py
├── agent2.py
└── agent.py

0 directories, 3 files
           

agent.py内容如下:

[[email protected] _modules]# cat agent.py 
#! /usr/bin/python
import os
def getcwd():
    return 'Test'


def showpara(string):
    return  string
           

定義好自己的modules後,我們通過saltutil.sync_modules同步一下

現在我們執行自定義的module如下:

[[email protected] _modules]# salt '172.18.1.211' agent.getcwd
172.18.1.211:
    Test
           

四:returner定義

定義了returner,那麼minion的執行結果不僅會上報給master,也會傳送一份給我們定義的returner

[[email protected] salt]# tree   _returners/
_returners/
├── local_return.py
└── testreturner.py

0 directories, 2 files
           

testreturner.py内容如下:

[[email protected] _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同步一下

我們測試一下:

[[email protected] _returners]# salt '172.18.1.212' test.ping --return=testreturner
172.18.1.212:
    True
[[email protected] _returners]# ssh [email protected]
[email protected]'s password: 
Last login: Sun Dec  6 09:18:53 2015 from admin-node
[[email protected] ~]# 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開始認證是或者其它事件發生時),執行一系列工作。

[[email protected] salt]# tree reactor/
reactor/
└── firstconnection.sls

0 directories, 1 file
           

firstconnection.sls檔案内容如下:

[[email protected] 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

六:配置例子

[[email protected] 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檔案内容:

[[email protected] salt]# cat top.sls 
base:
  '*':
    - os
           

os目錄内容:

[[email protected] salt]# tree os
os
├── commonfile
├── file.jinja
├── init.sls
├── node1
├── node2
├── node3
└── node4

0 directories, 7 files
           

 node1-4是基于主機的特殊配置檔案,而commonfile是公共配置檔案

init.sls檔案内容

[[email protected] 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)

[[email protected] 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
           

結果如下:

[[email protected] 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
           

執行指令結果為:

[[email protected] 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
           

執行指令,結果如圖:

[[email protected] 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函數

下面舉個例子:

[[email protected] ~]# salt-run manage.up

- 172.18.1.211

- 172.18.1.212

- 172.18.1.213

- 172.18.1.214

[[email protected] ~]# 

号外: 下面我們列印出執行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寫的

[[email protected] _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

[[email protected] _runners]# 

(3)然後我們重新開機下salt-master,執行指令salt-run my manage.up,結果如下:

[[email protected] _runners]# salt-run mymanage.up

- 172.18.1.211

- 172.18.1.212

- 172.18.1.213

- 172.18.1.214

[[email protected] _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的流程是一樣的