目錄
# 使用create_self_signed_cert()執行功能生成自簽名證書
一、簡介
saltsatck本身就提供了一套算完整的api,使用 CherryPy 來實現 restful 的 api,供外部的程序調用。
官方文檔:
https://www.unixhot.com/docs/saltstack/ref/netapi/all/salt.netapi.rest_cherrypy.html#a-rest-api-for-salt
二、安裝部署
# 安裝salt-api和CherryPy
[root@linux-node1 salt]# yum install -y salt-api
# 生成自簽名SSL證書
[root@linux-node1 salt]# yum list | grep -i pyopenssl
[root@linux-node1 salt]# yum install -y pyOpenSSL.x86_64
# 使用create_self_signed_cert()執行功能生成自簽名證書
[root@linux-node1 salt]# salt-call --local tls.create_self_signed_cert
三、master配置
[root@linux-node1 salt]# vi /etc/salt/master
default_include: master.d/*.conf
[root@linux-node1 salt]# mkdir /etc/salt/master.d && cd /etc/salt/master.d
[root@linux-node1 master.d]# vi api.conf
rest_cherrypy:
host: 192.168.56.11
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/certs/localhost.key
四、準備工作
#創建訪問認證用戶
[root@linux-node1 master.d]# useradd -M -s /sbin/nologin saltapi
[root@linux-node1 master.d]# echo "saltapi" | passwd saltapi --stdin
更改用戶 saltapi 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
# 訪問授權
[root@linux-node1 master.d]# vim api-auth.conf
external_auth:
pam:
saltapi: # user
- .*
- '@wheel' # to allow access to all wheel modules
- '@runner' # to allow access to all runner modules
- '@jobs' # to allow access to the jobs runner and/or wheel module
# 啓動salt-api
[root@linux-node1 master.d]# systemctl restart salt-master
[root@linux-node1 master.d]# systemctl restart salt-api
[root@linux-node1 master.d]# netstat -ntpl | grep 8000
tcp 0 0 192.168.56.11:8000 0.0.0.0:* LISTEN 8430/python
五、訪問驗證
# 訪問認證,獲取token
[root@linux-node1 master.d]# curl -sSk https://192.168.56.11:8000/login \
> -H 'Accept: application/x-yaml' \
> -d username=saltapi \
> -d password=saltapi \
> -d eauth=pam
return:
- eauth: pam
expire: 1585699754.516841
perms:
- .*
- '@wheel'
- '@runner'
- '@jobs'
start: 1585656554.51684
token: 32bb0f64f6ffac329a4663b1368eca23046e701b
user: saltapi
# 直接使用模塊方法
[root@linux-node1 master.d]# curl -sSk https://192.168.56.11:8000:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 32bb0f64f6ffac329a4663b1368eca23046e701b' -d client=local -d tgt='*^C -d fun=test.ping
[root@linux-node1 master.d]# curl -sSk https://192.168.56.11:8000 \
> -H 'Accept: application/x-yaml' \
> -H 'X-Auth-Token: 32bb0f64f6ffac329a4663b1368eca23046e701b'\
> -d client=local \
> -d tgt='*' \
> -d fun=test.ping
return:
- linux-node1.example.com: true
linux-node2.example.com: true
# 使用cmd.run
[root@linux-node1 master.d]# curl -sSk https://192.168.56.11:8000 \
> -H 'Accept: application/x-yaml' \
> -H 'X-Auth-Token: 32bb0f64f6ffac329a4663b1368eca23046e701b'\
> -d client=local \
> -d tgt='*' \
> -d fun=cmd.run -d arg='hostname'
return:
- linux-node1.example.com: linux-node1.example.com
linux-node2.example.com: linux-node2.example.com