SaltStack api使用

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容