目录
# 使用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