saltstack系列1之salt-api配置与使用
salt-api
salt-api是我们通过restful-api调用salt-master的接口,且调用的时候必须通过认证才能调用,认证的用户为系统用户,下面就说说如何配置salt-api。
不带SSL认证
1.创建用户
[root@salt yum.repos.d]# useradd saltapi
[root@salt yum.repos.d]# passwd saltapi # 密码为123456
2.修改salt-master配置文件
[root@salt yum.repos.d]# vim /etc/salt/master
default_include: master.d/*.conf
file_roots:
base:
- /salt/states
dev:
- /salt/dev
prod:
- /salt/prod
3.创建目录与配置文件:
[root@salt yum.repos.d]# cd /etc/salt/
[root@salt yum.repos.d]# mkdir master.d/
[root@salt yum.repos.d]# cd master.d/
[root@salt yum.repos.d]# cat api.conf # 第一个配置文件
rest_cherrypy:
host: 0.0.0.0
port: 8080
debug: true
disable_ssl: true
[root@salt yum.repos.d]# cat eauch.conf
external_auth:
pam:
saltapi:
- .*
- '@wheel'
- '@runner'
4.启动slat-api和salt-master
[root@salt yum.repos.d]# /etc/init.d/salt-api start
5.验证操作:
[root@salt ~]# curl -k http://127.0.0.1:8080/login -H "Accept: application/x-yaml" -d username='saltapi' -d password='123456' -d eauth='pam'
return:
- eauth: pam
expire: 1472481375.33111
perms:
- .*
start: 1472438175.331109
token: f9e354170c3311dc716db56d9217338a206ce006
user: saltapi
带SSL认证的
1.更改salt-master配置文件
[root@salt yum.repos.d]# vim /etc/salt/master
default_include: master.d/*.conf # 添加这一行
[root@salt master.d]# mkdir /etc/salt/master.d/ # 创建目录
2.创建key:
[root@salt private]# cd /etc/pki/tls/private
[root@salt certs]# make testcert
[root@salt certs]# cd ../private/
[root@salt private]# openssl rsa -in localhost.key -out localhost_nopass.key
3.创建目录与配置文件:
[root@salt master.d]# cat api.conf eauth.conf
rest_cherrypy:
port: 8010 监听 的端口
ssl_crt: /etc/pki/tls/certs/localhost.crt crt证书路径不能错误
ssl_key: /etc/pki/tls/private/localhost_nopass.key key路径也不能错误,不然端口死活起不来
external_auth:
pam:
saltapi: 调用系统认证的pam模块做用户认证
- .* 用户
4.启动服务
[root@salt master.d]# /etc/init.d/salt-api start
5.验证操作:
[root@salt master.d]# curl -k https://192.168.1.16:8010/login -H "Accept: application/x-yaml" -d username='saltapi' -d password='123456' -d eauth='pam'
return:
- eauth: pam
expire: 1472007733.0523901
perms:
- .*
start: 1471964533.0523901
token: 9b6a6e82c002b925952e5c930a699286e8096db1
user: saltapi
6.执行命令看看
[root@salt tmp]# curl -k https://172.16.22.129:8010/ -H "Accept: application/x-yaml" -H "X-Auth-Token: 5aa829c1df638221f0b62b25e68f7121a347022e" -d client="local" -d tgt='*' -d fun='test.ping'
return:
- web.minion1.com: true
curl -k http://172.16.22.129:8010/ -H "Accept: application/x-yaml" -H "X-Auth-Token: f9e354170c3311dc716db56d9217338a206ce006" -d client="local" -d tgt='*' -d fun='grains.items'