一 saltstack 數據系統 遠程執行
官方中文網站
官方英文網站
1、 簡介
三大功能
- 遠程執行
- 配置管理
- 雲管理
四種運行方式
- local
- master / minion c/s模式(常用)
- syndic - (相當於zabbix proxy)
- salt ssh
2、 安裝
1. 環境聲明
系統版本和內核:
CentOS Linux release 7.2.1511 (Core)
3.10.0-327.el7.x86_64
基礎環境:
salt-master 10.0.0.204
salt-minion 10.0.0.203
2. 安裝salt的repo庫文件
[root@salt-node4 ~]# yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el7.noarch.rpm
3. 安裝salt軟件
master端
安裝
[root@salt-node4 ~]# yum -y install salt-master salt-minion
啟動
[root@salt-node4 ~]# systemctl start salt-master
minion端
安裝
[root@salt-node4 ~]# yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el7.noarch.rpm
[root@salt-node4 ~]# yum -y install salt-minion
4. 修改salt-minion配置文件並啟動
vim /etc/salt/minion
...
16 master: 10.0.0.204
# 此id是salt-minion的唯一標識符,此處可不填寫,啟動salt後會采用系統的主機名作為id
101 #id:
...
啟動salt-minion
[root@test-node3 ~]# systemctl start salt-minion
[root@test-node3 ~]# cat /etc/salt/minion_id
test-node3.damaicha.org-203
3 遠程執行
1. 在master上 ,查看master的公鑰和minion發來的公鑰
[root@salt-node4 ~]# tree /etc/salt/pki /etc/salt/pki ├── master │ ├── master.pem │ ├── master.pub # master的公鑰 │ ├── minions │ ├── minions_autosign │ ├── minions_denied │ ├── minions_pre # 這個目錄下是minion客戶端發來的公鑰 │ │ ├── salt-node4.damaicha.org-204 │ │ └── test-node3.damaicha.org-203 │ └── minions_rejected └── minion ├── minion.pem └── minion.pub
2. master 接受minion發過來的公鑰。
查看下key
[root@salt-node4 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
salt-node4.damaicha.org-204
test-node3.damaicha.org-203
Rejected Keys:
salt-key -a 接受指定主機發來的公鑰
[root@salt-node4 ~]# salt-key -a salt*
The following keys are going to be accepted:
Unaccepted Keys:
salt-node4.damaicha.org-204
Proceed? [n/Y] y
Key for minion salt-node4.damaicha.org-204 accepted.
[root@salt-node4 ~]#
再次進行查看
[root@salt-node4 ~]# tree /etc/salt/pki/
/etc/salt/pki/
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions # master在同意之後,會將minion的證書移動到minions目錄下
│ │ ├── salt-node4.damaicha.org-204
│ │ └── test-node3.damaicha.org-203
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre
│ └── minions_rejected
└── minion # 同時minion也會收到來自master的公鑰
├── minion_master.pub
├── minion.pem
└── minion.pub
7 directories, 7 files
[root@salt-node4 ~]#
3.檢查遠程主機的狀態
[root@salt-node4 ~]# salt ‘*‘ test.ping
salt-node4.damaicha.org-204:
True
test-node3.damaicha.org-203:
True
4.遠程執行命令
root@salt-node4 ~]# salt "*" cmd.run "hostname"
test-node3.damaicha.org-203:
test-node3.damaicha.org-203
salt-node4.damaicha.org-204:
salt-node4.damaicha.org-204
4 配置管理
官方文檔
state格式:YAML 後綴:.sls
YAML:三班斧
YAML官方參考文檔
1、縮進
2個空格,不能使用tab
2、冒號 右邊有一個空格
3、短橫線 (後面都有一個空格)表示一個列表
1. 編寫一個sls文件
master開啟 file roots
[root@salt-node4 ~]# vim /etc/salt/master481 file_roots:
481 file_roots:
482 base:
483 - /srv/salt
重啟salt-master
[root@salt-node4 ~]# systemctl restart salt-master
新建salt目錄
[root@salt-node4 ~]# mkdir /srv/salt
[root@salt-node4 ~]# mkdir /srv/salt/web
編寫一個安裝apache的sls模塊文件
[root@salt-node4 web]# cat apache.sls
apache-install:
pkg.installed:
- names:
- httpd
- httpd-devel
apache-service:
service.running:
- name: httpd
- enable: True
執行這個自定義的模塊
[root@salt-node4 web]# salt "*" state.sls web.apache
2. 定義一個top file
定義一個top file 執行模塊,放在base目錄下。base目錄設置在file root目錄下。
他的作用是,可以定義哪些主機執行這個指定的模塊。
[root@salt-node4 salt]# pwd
/srv/salt
[root@salt-node4 salt]# cat top.sls
base:
‘test-node3.damaicha.org-203‘:
- web.apache
‘salt-node4.damaicha.org-204‘:
- web.apache
執行這個top file文件
1、 需要查看salt執行這個操縱需要操作那些,然後在進行操作
[root@salt-node4 salt]# salt ‘*‘ state.highstate test=True
2、 執行這狀態模塊
[root@salt-node4 salt]# salt ‘*‘ state.highstate
5 saltstack和zeroMQ
- 使用長連接的方式,當需要發布消息的時候直接發布即可。
- 消息訂閱-廣播,一人說,多個人聽。
4505端口和salt-minion建立一個長連接。
[root@salt-node4 salt]# lsof -i :4505
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
salt-mini 3206 root 26u IPv4 25721 0t0 TCP salt-node4.damaicha.org-204:33713->salt-node4.damaicha.org-204:4505 (ESTABLISHED)
salt-mast 3282 root 16u IPv4 23094 0t0 TCP *:4505 (LISTEN)
查看saltstack各個進程的名稱。
需要安裝python-setproctitle這個安裝包
Python-setprocesstilte
[root@salt-node4 salt]# ps aux|grep salt
root 3195 0.0 0.7 323484 3760 ? Ss Jan10 0:00 /usr/bin/python /usr/bin/salt-minion
root 3206 0.0 4.2 686432 20608 ? Sl Jan10 0:22 /usr/bin/python /usr/bin/salt-minion
root 3208 0.0 0.7 398568 3568 ? S Jan10 0:00 /usr/bin/python /usr/bin/salt-minion
root 3270 0.0 5.3 382168 25896 ? Ss Jan10 0:00 /usr/bin/python /usr/bin/salt-master
root 3281 0.0 2.0 309272 9860 ? S Jan10 0:00 /usr/bin/python /usr/bin/salt-master
root 3282 0.0 4.5 457940 21988 ? Sl Jan10 0:00 /usr/bin/python /usr/bin/salt-master
root 3285 0.0 4.8 392552 23304 ? S Jan10 0:00 /usr/bin/python /usr/bin/salt-master
root 3286 0.2 8.7 395512 42372 ? S Jan10 1:27 /usr/bin/python /usr/bin/salt-master
root 3375 3.3 6.1 1121104 30072 ? Sl Jan10 17:17 /usr/bin/python /usr/bin/salt-master
root 3376 0.0 5.3 382168 26076 ? S Jan10 0:00 /usr/bin/python /usr/bin/salt-master
root 3386 0.0 5.3 759024 25776 ? Sl Jan10 0:01 /usr/bin/python /usr/bin/salt-master
root 3406 0.0 6.6 619368 32284 ? Sl Jan10 0:01 /usr/bin/python /usr/bin/salt-master
root 3407 0.0 6.4 620716 31408 ? Sl Jan10 0:01 /usr/bin/python /usr/bin/salt-master
root 3410 0.0 6.5 619976 31904 ? Sl Jan10 0:01 /usr/bin/python /usr/bin/salt-master
root 3411 0.0 6.8 621428 33208 ? Sl Jan10 0:01 /usr/bin/python /usr/bin/salt-master
root 3412 0.0 6.6 619516 32136 ? Sl Jan10 0:01 /usr/bin/python /usr/bin/salt-master
root 32466 0.0 0.2 112648 976 pts/0 S+ 07:20 0:00 grep --color=auto salt
[root@salt-node4 salt]# systemctl restart salt-master
[root@salt-node4 salt]# ps aux|grep salt
root 3195 0.0 0.7 323484 3760 ? Ss Jan10 0:00 /usr/bin/python /usr/bin/salt-minion
root 3206 0.0 4.2 686432 20792 ? Sl Jan10 0:22 /usr/bin/python /usr/bin/salt-minion
root 3208 0.0 0.7 398568 3568 ? S Jan10 0:00 /usr/bin/python /usr/bin/salt-minion
root 32473 32.0 7.3 384236 35852 ? Ss 07:20 0:00 /usr/bin/python /usr/bin/salt-master ProcessManager # 主進程
root 32484 0.0 3.8 311340 18644 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master MultiprocessingLoggingQueue
root 32485 0.0 5.5 460008 27028 ? Sl 07:20 0:00 /usr/bin/python /usr/bin/salt-master ZeroMQPubServerChannel # zeromq 消息隊列
root 32488 0.0 5.4 378080 26644 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master EventPublisher
root 32489 10.5 6.8 386060 33104 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master Maintenance
root 32543 1.0 6.3 1122300 30672 ? Sl 07:20 0:00 /usr/bin/python /usr/bin/salt-master Reactor
root 32544 2.0 6.1 384236 30072 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master ReqServer_ProcessManager
root 32546 0.0 6.2 564484 30564 ? Sl 07:20 0:00 /usr/bin/python /usr/bin/salt-master MWorkerQueue
root 32562 47.0 6.7 386492 32656 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master MWorker-0
root 32563 50.0 6.7 386496 32660 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master MWorker-1
root 32564 47.0 6.7 386500 32660 ? R 07:20 0:00 /usr/bin/python /usr/bin/salt-master MWorker-2
root 32565 45.0 6.7 386496 32660 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master MWorker-3
root 32566 43.0 6.7 386504 32668 ? S 07:20 0:00 /usr/bin/python /usr/bin/salt-master MWorker-4
root 32899 0.0 0.2 112648 976 pts/0 S+ 07:20 0:00 grep --color=auto salt
[root@salt-node4 salt]#
6 數據系統grains
grains 靜態數據 是key values型。
當Minion 啟動時收集minion本地的相關信息 。 例如:操作系統版本,內核版本,cpu, 內存, 硬盤, 設備型號, 序列號....
作用:
- 目標選擇。
- 資產管理,信息查詢。
- 配置管理中使用。
- 定義位置:minion客戶端
1.grains基礎操作。
1、查看salt默認自帶的grains所有的Key。
[root@salt-node4 salt]# salt ‘test*‘ grains.ls
test-node3.damaicha.org-203:
- SSDs
- biosreleasedate
- biosversion
- cpu_flags
- cpu_model
- cpuarch
2、查看所有的key對應的values
[root@salt-node4 salt]# salt ‘test*‘ grains.items
...
.....
uid:
0
username:
root
uuid:
564df67d-68e6-39f0-4083-e6bcbcd3fb38
virtual:
VMWare
zmqversion:
4.1.4
3、查看指定的key對應的values
例如查看ip
[root@salt-node4 salt]# salt ‘*‘ grains.item fqdn_ip4
salt-node4.damaicha.org-204:
----------
fqdn_ip4:
- 10.0.0.204
test-node3.damaicha.org-203:
----------
fqdn_ip4:
- 10.0.0.203
4、根據grains來進行目標選擇。
[root@salt-node4 salt]# salt -G "fqdn_ip4:10.0.0.204" cmd.run "hostname"
salt-node4.damaicha.org-204:
salt-node4.damaicha.org-204
2.自定義grains
1、修改客戶端minion主配置文件。
[root@test-node3 ~]# vim /etc/salt/minion
118 grains:
119 roles:
120 - apache
2、重啟minion。
[root@test-node3 ~]# systemctl restart salt-minion
如果不想在mininon上重啟客戶端可以這樣操作,進行刷新grains操作。
salt ‘test*‘ saltutil.sync_grains
3、master端進行測試。
[root@salt-node4 salt]# salt -G "roles:apache" test.ping
test-node3.damaicha.org-203:
True
[root@salt-node4 salt]# salt ‘test-*‘ grains.item roles
test-node3.damaicha.org-203:
----------
roles:
- apache
3.自定義一個grains文件
1、自定義grains文件,一般放在/etc/salt/grains文件裏,salt啟動時會自動的去加載。
[root@test-node3 ~]# vim /etc/salt/grains
wangfei: heheda
2、刷新minion grains
[root@salt-node4 salt]# salt "test*" saltutil.sync_grains
test-node3.damaicha.org-203:
3、master端測試
[root@salt-node4 salt]# salt ‘test-*‘ grains.item wangfei
test-node3.damaicha.org-203:
----------
wangfei:
heheda
4.top file 結合grains使用
[root@salt-node4 salt]# vim top.sls
base:
‘test-node3.damaicha.org-203‘:
- web.apache
‘salt-node4.damaicha.org-204‘:
- web.apache
‘wangfei:heheda‘:
- match: grain
- web.apache
-
[root@salt-node4 salt]# salt ‘*‘ state.highstate
5.自定義一個grains
1、新建_grains文件夾(必須放在/srv/salt/目錄下。)
[root@salt-node4 salt]# cd /srv/salt/
[root@salt-node4 salt]# mkdir _grains
2、定義自定義的grains文件。
[root@salt-node4 salt]# cd _grains/
[root@salt-node4 _grains]# vim my_grains.py
#!/bin/env python
#-*- coding:utf-8 -*-
def my_grains():
#初始化一個grains字典
grains = {}
#設置字典中的值 key-values
grains[‘iaas‘] = ‘openstack‘
grains[‘edu‘] = ‘oldboyedu‘
# 返回這個字典
return grains
3、my_grains.py 推送到minion上去,此時已經同時刷新grains了。
[root@salt-node4 _grains]# salt ‘*‘ saltutil.sync_grains
salt-node4.damaicha.org-204:
- grains.my_grains
test-node3.damaicha.org-203:
- grains.my_grains
4、minion客戶端查看推送過來的grains文件。
文件在/var/cache/salt/目錄extmods目錄下。
[root@test-node3 ~]# tree /var/cache/salt/
/var/cache/salt/
└── minion
├── accumulator
├── extmods
│ └── grains
│ ├── my_grains.py
│ └── my_grains.pyc
├── files
│ └── base
│ ├── _grains
│ │ └── my_grains.py # here
│ ├── top.sls
│ └── web
│ └── apache.sls
├── highstate.cache.p
├── module_refresh
├── pkg_refresh
├── proc
└── sls.p
9 directories, 9 files
[root@test-node3 ~]#
5、master查看我定義的grains
[root@salt-node4 _grains]# salt ‘tes*‘ grains.item iaas
test-node3.damaicha.org-203:
----------
iaas:
openstack
[root@salt-node4 _grains]# salt ‘tes*‘ grains.item edu
test-node3.damaicha.org-203:
----------
edu:
oldboyedu
6.定義的grains優先級
grains優先級:
1. 系統自帶的。
2. grains文件裏寫的。
3. minion配置文件裏寫的。
4. 自己寫的grains。
7 數據系統pillar
pillar動態,給特定的minion指定特定的數據。只用指定的minion才能看到自己的數據
在什麽地方使用呢?
- 定義一些敏感的數據時使用。
- 所有的變量都可以用pillar來進行定義。例如100臺機器中有1臺不一樣,就可以用pillar來進行管理。
- 目標選擇。
- 定義位置在master端。
1、查看pillar的值。
默認是沒有值的,因為pillar在配置文件裏面默認是關閉的。
打開配置文件裏面的Pillar
[root@salt-node4 ~]# vim /etc/salt/master
664 pillar_opts: True # 是pillar_opts 這一行哦。
重啟master 查看默認的pillar
[root@salt-node4 ~]# systemctl restart salt-master
[root@salt-node4 ~]# salt ‘test-*‘ pillar.items
test-node3.damaicha.org-203:
----------
master:
----------
__role:
master
auth_mode:
1
.....................
看完之後,就關閉它。一會我們要自定義一個pillar
2、開啟pillar_roots。
[root@salt-node4 salt]# vim /etc/salt/master
641 pillar_roots:
642 base:
643 - /srv/pillar
重啟master
[root@salt-node4 ~]# systemctl restart salt-master
3、新建pillar文件夾。
[root@salt-node4 ~]# mkdir /srv/pillar
[root@salt-node4 ~]# tree /srv/
/srv/
├── pillar
└── salt
├── _grains
│ └── my_grains.py
├── top.sls
└── web
└── apache.sls
4 directories, 3 files
4、新建pillar的sls文件。
pillar必須結合top file才能使用。 pillar是在服務器端進行定義的,不用top file要不咋進行定義呢?
[root@salt-node4 ~]# cd /srv/pillar/
[root@salt-node4 pillar]# mkdir web
[root@salt-node4 pillar]# vim web/apache.sls # 這是一個jinjia模板
{% if grains[‘os‘] == ‘CentOS‘ %}
apache: httpd
{% elif grains[‘os‘] == ‘Debian‘ %}
apache: apache2
{% endif %}
5、新建pillar 的 topfile 文件。
[root@salt-node4 pillar]# vim top.sls
base:
‘test-node3.damaicha.org-203‘:
- web.apache
6、master刷新pillar。
[root@salt-node4 pillar]# salt ‘*‘ saltutil.refresh_pillar
]test-node3.damaicha.org-203:
True
salt-node4.damaicha.org-204:
True
7、master 測試。
[root@salt-node4 pillar]# salt ‘*‘ pillar.items # 查看所有的pillar
salt-node4.damaicha.org-204:
----------
test-node3.damaicha.org-203:
----------
apache:
httpd
[root@salt-node4 pillar]# salt ‘*‘ pillar.item apache # 查看指定的pillar
test-node3.damaicha.org-203:
----------
apache:
httpd
salt-node4.damaicha.org-204:
----------
apache:
匹配pillar
[root@salt-node4 pillar]# salt -I "apache:httpd" cmd.run "hostname"
test-node3.damaicha.org-203:
test-node3.damaicha.org-203
8 pillar和grains的區別
類型:
- pillar 動態的
- grains 靜態的
數據收集方式:
- grains minion啟動時收集
- pillar master自定義
應用場景:
- grains 目標查詢、目標選擇、配置管理
- pillar 目標選擇、配置管理、敏感數據
定義位置:
- grains minion客戶端
- pillar master服務端
9 遠程執行 - 指定目標
salt ‘*‘ cmd.run "hostname"
分解
命令:salt
目標:*
模塊:cmd.run
返回:hostname執行後就是返回結果。
1、 目標。
有2種:
1. 和minion id 有關的。
2. 和minion id 無關的。
minion id 有關的
通配符
例子:
salt ‘test-node3.damaicha.org-203‘ test.ping
salt ‘test-*‘ test.ping
salt ‘test-node[0-9].damaicha.org-203‘ test.ping
正則表達式 -E
例子:
salt -E ‘(test|salt)-node[0-9].damaicha.org-20[0-9]‘ test.ping
列表 -L
例子:
salt -L ‘test-node3.damaicha.org-203,salt-node4.damaicha.org-204‘ test.ping
minion id無關的
pillar -I
例子:
salt -I "apache:httpd" cmd.run "hostname"
grains -G
例子:
salt -G "wangfei:heheda" cmd.run "hostname"
node -N
1、定義好Node
vim /etc/salt/master
952 nodegroups:
953 web: ‘[email protected],salt-node4.damaicha.org-204‘
2、重啟master
systemctl restart salt-master
3、測試
[root@salt-node4 pillar]# salt -N "web" test.ping
salt-node4.damaicha.org-204:
True
test-node3.damaicha.org-203:
True
混合匹配 -C
官方參考資料 - 混合匹配
註意:混合匹配也同樣適用於topfile文件
例子:
salt -C ‘G@os:Debian and webser* or E@db.*‘ test.ping
Letter Match Type Example Alt Delimiter?
G Grains glob G@os:Ubuntu Yes
E PCRE Minion ID E@web\d+\.(dev|qa|prod)\.loc No
P Grains PCRE P@os:(RedHat|Fedora|CentOS) Yes
L List of minions [email protected],minion3.domain.com or bl*.domain.com No
I Pillar glob I@pdata:foobar Yes
J Pillar PCRE J@pdata:^(foo|bar)$ Yes
S Subnet/IP address [email protected]/24 or [email protected] No
R Range cluster
批處理 -b
百分比重啟,先重啟50臺,再重啟50臺機器。
10臺10臺的
salt ‘*‘ -b 10 test.ping
按照百分比來
salt -G ‘os:RedHat‘ --batch-size 25% apache.signal restart
所有匹配目標的方式都可以用到top file裏來指定目標
10 遠程執行 - 執行模塊
" rel="nofollow">saltstack所有的模塊官方連接
saltstack所有的自帶模塊在/usr/lib/python2.7/site-packages/salt/modules裏。
現在執行一個自帶的模塊。
獲取所有的tcp連接
[root@salt-node4 pillar]# salt ‘test*‘ network.active_tcp
test-node3.damaicha.org-203:
----------
0:
----------
local_addr:
::
local_port:
80
remote_addr:
::
remote_port:
0
1:
----------
local_addr:
::
local_port:
52113
remote_addr:
::
remote_port:
0
2:
----------
local_addr:
10.0.0.203
local_port:
56828
remote_addr:
10.0.0.204
remote_port:
4505
獲取主機名
[root@salt-node4 pillar]# salt ‘test*‘ network.get_hostname
test-node3.damaicha.org-203:
test-node3.damaicha.org-203
查看ssh服務是否在運行
saltstack所有服務相關的服務
[root@salt-node4 pillar]# salt ‘test*‘ service.available sshd
test-node3.damaicha.org-203:
True
查看所有正在運行的服務
[root@salt-node4 pillar]# salt ‘test*‘ service.get_all
復制文件cp
將master裏/etc/hosts 復制到目標主機為/tmp/hehe
[root@salt-node4 pillar]# salt-cp "*" /etc/hosts /tmp/wf
salt-node4.damaicha.org-204:
----------
/tmp/wf:
True
test-node3.damaicha.org-203:
----------
/tmp/wf:
True
查看top file文件在minion需要做哪些事情。
[root@salt-node4 pillar]# salt ‘*‘ state.show_top
test-node3.damaicha.org-203:
----------
base:
- web.apache
- web.apache
salt-node4.damaicha.org-204:
----------
base:
- web.apache
手動的安裝一個軟件
[root@salt-node4 pillar]# salt "*" state.single pkg.installed name=lsof
11 saltstack遠程執行-返回程序
官方相關參考文檔
返回數據到mysql
官方相關參考文檔 - return mysql
1、minion安裝mysql的pyhon客戶端。
[root@salt-node4 ~]#salt -G "wangfei:heheda" state.single pkg.installed name=MySQL-python
2、mysql數據庫新建對應的庫和表。
新建數據庫
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `salt`;
創建對應的表
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX jid ON jids(jid) USING BTREE;
--
-- Table structure for table `salt_returns`
--
DROP TABLE IF EXISTS `salt_returns`;
CREATE TABLE `salt_returns` (
`fun` varchar(50) NOT NULL,
`jid` varchar(255) NOT NULL,
`return` mediumtext NOT NULL,
`id` varchar(255) NOT NULL,
`success` varchar(10) NOT NULL,
`full_ret` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3、新建對應的管理用戶。
grant all on salt.* to salt@‘%‘ identified by ‘salt@pw‘;
4、minion客戶端配置文件,添加如下,並且重啟。
vim /etc/salt/minion
mysql.host: ‘10.0.0.204‘
mysql.user: ‘salt‘
mysql.pass: ‘salt@pw‘
mysql.db: ‘salt‘
mysql.port: 3306
systemctl restart salt-minion
5、master端執行命令
[root@salt-node4 ~]# salt ‘*‘ cmd.run "hostname" --return mysql
6、此時登錄數據庫進行查看。
MariaDB [salt]> select * from salt_returns\G
*************************** 8. row ***************************
fun: cmd.run
jid: 20170221043152141415
return: "Filesystem Size Used Avail Use% Mounted on\n/ dev/mapper/centos-root 18G 2.0G 16G 12% /\ndevtmpfs 227M 0 227M 0% /dev\ntmpfs 237M 12K 237M 1% /dev/shm\ntmpfs 237M 4.6M 233M 2% /run\ntmpfs
............................
12 saltstack遠程執行-編寫執行模塊
1、存放位置。
[root@salt-node4 ~]# mkdir /srv/salt/_modules # 模塊位置就是在srv/salt目錄下,不能更改。
2、新建執行模塊。(文件名就是模塊名。)
[root@salt-node4 ~]# vim /srv/salt/_modules/my_disk.py
[root@salt-node4 /srv/salt/_modules]# vim my_disk.py
def list():
cmd = ‘df -h‘
ret = __salt__[‘cmd.run‘](cmd)
return ret
3、刷新,加載這個自定義的模塊。
[root@salt-node4 ~]# salt ‘*‘ saltutil.sync_modules
salt-node4.damaicha.org-204:
- modules.my_disk
test-node3.damaicha.org-203:
- modules.my_disk
刷新後,會將這個文件放在/var/cache/salt/minion/extmods/modules/my_disk.py
[root@test-node3 srv]# tree /var/cache/salt
/var/cache/salt/
└── minion
├── accumulator
├── extmods
│ ├── grains
│ │ ├── my_grains.py
│ │ └── my_grains.pyc
│ └── modules
│ └── my_disk.py
├── files
│ └── base
│ ├── _grains
│ │ └── my_grains.py
│ ├── _modules
│ │ └── my_disk.py
│ ├── top.sls
│ └── web
│ └── apache.sls
├── highstate.cache.p
├── module_refresh
├── proc
└── sls.p
4、服務端執行。
[root@salt-node4 ~]# salt ‘*‘ my_disk.list
test-node3.damaicha.org-203:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 2.0G 16G 12% /
devtmpfs 227M 0 227M 0% /dev
tmpfs 237M 12K 237M 1% /dev/shm
tmpfs 237M 4.6M 233M 2% /run
tmpfs 237M 0 237M 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 26% /boot
tmpfs 48M 0 48M 0% /run/user/0
salt-node4.damaicha.org-204:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 2.0G 16G 12% /
devtmpfs 227M 0 227M 0% /dev
tmpfs 237M 28K 237M 1% /dev/shm
tmpfs 237M 8.6M 229M 4% /run
tmpfs 237M 0 237M 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 26% /boot
tmpfs 48M 0 48M 0% /run/user/0
一 saltstack 數據系統 遠程執行