SaltStack官方提供有REST API格式的api項(xiàng)目,使得salt與第三方系統(tǒng)集成變得更加簡(jiǎn)單。
以下操作前提是您已經(jīng)安裝好salt-master和salt-api服務(wù)
1、配置salt-master,開啟rest_cherrypy服務(wù)
這里偷懶,沒有使用ssl,所以直接禁用ssl了,線上環(huán)境建議使用ssl
rest_cherrypy:
port: 8181
host: 0.0.0.0
disable_ssl: True
2、配置pam驗(yàn)證,
external_auth:
pam:
saltuser:
- .*
- '@runner'
- '@wheel'
- '@jobs'
以上兩個(gè)修改都是在/etc/salt/master文件中
3、創(chuàng)建認(rèn)證用戶,并設(shè)置密碼
useradd -M -s /sbin/nologin saltuser
4、重啟salt-master和啟動(dòng)salt-api
systemctl restart salt-master
systemctl restart salt-api
5、查看salt監(jiān)聽端口
[root@qd01-stop-saltmaster001 ~]# ss -ltnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 30 *:8181 *:* users:(("salt-api",pid=13833,fd=11))
LISTEN 0 1000 *:4505 *:* users:(("salt-master",pid=12235,fd=18))
LISTEN 0 1000 *:4506 *:* users:(("salt-master",pid=12332,fd=32))
6、驗(yàn)證login登錄,獲取token字符串
[root@saltmaster001 ~]# curl -sS http://localhost:8181/login -H 'Accept: application/x-yaml' -d username=saltuser -d password=saltuser -d eauth=pam
return:
- eauth: pam
expire: 1610484091.7311294
perms:
- .*
- '@runner'
- '@wheel'
- '@jobs'
start: 1610440891.731129
token: 49d2bedbddf71dd6c4af3c2f5e09797b2cf0d9aa
user: saltuser
7、通過api執(zhí)行test.ping測(cè)試
[root@saltmaster001 ~]# curl -sSk http://localhost:8181 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 49d2bedbddf71dd6c4af3c2f5e09797b2cf0d9aa' -d client=local -d tgt='*monitor004*' -d fun=test.ping
return:
- monitor004: true
可以看到,返回值和直接在cmd執(zhí)行test.ping一樣
8、通過api執(zhí)行cmd.run
[root@saltmaster001 ~]# curl -sSk http://localhost:8181 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 49d2bedbddf71dd6c4af3c2f5e09797b2cf0d9aa' -d client=local -d tgt='qd01-stop-monitor004*' -d fun=cmd.run -d arg='uptime'
return:
- monitor004: ' 16:44:51 up 586 days, 12:47, 0 users, load
average: 0.00, 0.00, 0.00'
更多信息請(qǐng)參見https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#a-rest-api-for-salt