1、系統環境python
cat /etc/redhat-release vim
CentOS Linux release 7.3.1611 (Core)api
2、安裝及配置app
一、salt-api全部操做均在master端curl
yum -y install salt-api pyOpenSSL tcp
二、配置salt-apiide
修改/etc/salt/master文件測試
sed -i '/#default_include/s/#default/default/g' /etc/salt/master url
若是沒有目錄須要建立rest
mkdir /etc/salt/master.d
三、建立用於salt-api的用戶
useradd -M -s /sbin/nologin saltapi
echo 'saltapi' | passwd saltapi --stdin
四、新增配置文件api.conf、eauth.conf
vim /etc/salt/master.d/api.conf
rest_cherrypy:
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/certs/localhost.key
vim /etc/salt/master.d/eauth.conf
external_auth:
pam:
saltapi:
- .*
- '@wheel'
- '@runner'
五、生成自簽名證書
salt-call tls.create_self_signed_cert
六、重啓salt-master
systemctl restart salt-master.service
七、啓動salt-api
systemctl restart salt-api
查看是否啓動8000端口
netstat -tnlp |grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 11042/python
3、Salt-api的使用方法
一、使用curl 獲取token
curl -k https://172.16.8.200:8000/login -H "Accept: application/x-yaml" -d username='saltapi' -d password='saltapi' -d eauth='pam'
return:
- eauth: pam
expire: 1503412552.298529
perms:
- .*
- '@wheel'
- '@runner'
start: 1503369352.298528
token: a30a9b669696402888d67ce857626799a89d8992
user: saltapi
獲取token後就可使用token通訊
注:重啓salt-api後token改變
二、測試minion端的聯通性
salt '*' test.ping
curl -k https://172.16.8.200:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: a30a9b669696402888d67ce857626799a89d8992" -d client='local' -d tgt='*' -d fun='test.ping'
執行結果:
curl -k https://172.16.8.200:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: a30a9b669696402888d67ce857626799a89d8992" -d client='local' -d tgt='*' -d fun='test.ping'
return:
- zabbix-grafana: true
三、執行遠程命令
salt '*' cmd.run df -h
curl -k https://172.16.8.200:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: a30a9b669696402888d67ce857626799a89d8992" -d client='local' -d tgt='*' -d fun='cmd.run' -d arg='df -h'
執行結果:
curl -k https://172.16.8.200:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: a30a9b669696402888d67ce857626799a89d8992" -d client='local' -d tgt='*' -d fun='cmd.run' -d arg='df -h'
return:
- zabbix-grafana: 'Filesystem Size Used Avail Use% Mounted on
/dev/sda5 98G 80G 19G 82% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 28K 3.9G 1% /dev/shm
tmpfs 3.9G 81M 3.8G 3% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda2 497M 129M 368M 26% /boot
tmpfs 783M 0 783M 0% /run/user/0'