salt-api安装以及使用
1.salt-api安装
pip install CherryPy
yum -y install salt-api
yum install gcc make python-devel libffi-devel
pip install PyOpenSSL
要想让salt 兼容python3
那就需要安装最新的salt
2.salt-api配置
A.预先生成签名证书
mkdir -p cd /etc/pki/tls/certs/
#如果make testcert 出现如下
[root@DevOps certs]# make testcert
make: 对“testcert”无需做任何事。
那删除 /etc/pki/tls/private/ 下的localhost.key 文件即可
[root@centos7 ~]# cd /etc/pki/tls/certs/ # 生成自签名证书,用于ssl [root@centos7 certs]# make testcert umask 77 ; \ /usr/bin/openssl genrsa -aes128 2048 > /etc/pki/tls/private/localhost.key Generating RSA private key, 2048 bit long modulus ...................................................................+++ ..+++ e is 65537 (0x10001) Enter pass phrase: # 输入加密密语,4到8191个字符 Verifying - Enter pass phrase: # 确认加密密语 umask 77 ; \ /usr/bin/openssl req -utf8 -new -key /etc/pki/tls/private/localhost.key -x509 -days 365 -out /etc/pki/tls/certs/localhost.crt -set_serial 0 Enter pass phrase for /etc/pki/tls/private/localhost.key: # 再次输入密语 You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN # 选填 State or Province Name (full name) []:Shanghai Locality Name (eg, city) [Default City]:Shanghai Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []: Email Address []:
B.解密key文件,生成无密码的key文件, 过程中需要输入key密码,该密码为之前生成证书时设置的密码
[root@centos7 certs]# cd ../private/ [root@centos7 private]# openssl rsa -in localhost.key -out localhost_nopass.key Enter pass phrase for localhost.key: writing RSA key [root@centos7 private]# ls localhost.key localhost_nopass.key
C.添加用户:
[root@centos7 private]# useradd -M -s /sbin/nologin saltapi
[root@centos7 private]# echo "saltapi" | passwd saltapi --stdin
D.添加配置文件
# 添加配置文件 [root@centos7 ~]# mkdir -p /etc/salt/master.d/ [root@centos7 ~]# vim /etc/salt/master.d/eauth.conf # 处于安全因素,一般只给特定模块的使用权限,这里给saltapi用户所有模块的使用权限 external_auth: pam: saltapi: - .* - '@wheel' - '@runner' [root@centos7 ~]# vim /etc/salt/master.d/api.conf rest_cherrypy: port: 7777 # salt-api 监听端口 ssl_crt: /etc/pki/tls/certs/localhost.crt # ssl认证的证书 ssl_key: /etc/pki/tls/private/localhost_nopass.key # 如果不使用ssl,用下面配置 rest_cherrypy: port: 8888 host: 172.16.30.133 disable_ssl: True [root@centos7 ~]# systemctl restart salt-master.service [root@centos7 ~]# systemctl restart salt-api.service
3.Salt-api使用
获取token信息
curl -k https://172.16.30.133:7777/login -H "Accept: application/x-yaml" -d username='saltapi' -d password='saltapi' -d eauth='pam'
curl -k https://172.16.30.133:7777/ -H "Accept: application/json" -H "X-Auth-Token: c1a0ec2c6ddc9073ff98244e4ab9b7bd1794d722" -d client='local' -d tgt='*126' -d fun='pillar.items'
#其中 token 后边的串为认证成功后获取的token串,之后可以不用再次输入密码,直接使用本Token即可
通过Restful API实现日常操作
1)运行远程模块。
2)查询指定job。
3)运行runner
模板方法:
curl -k https://10.0.2.118:8000/ \
-H "Accept: application/json" \ # 返回信息格式 , 默认 json,
-H "X-Auth-Token: eb74373815d94624d3dd05016432a1b385fa43e9" \ #
-d client='local' \ # 调用的底层 salt 模块
-d tgt='*' \ #target, 如果不使用默认匹配模式 (glob), 添加
-d fun='test.ping' \ # 执行函数
| python -mjson.tool #Json 格式化输出
1.远程执行模块
curl -k https://192.168.29.72:8000/ -H "Accept: application/json" -H "X-Auth-Token: 0e827785dccd22163d025a71c0045771a06beff8" -d client='local' -d tgt='*' -d fun='test.ping'| python -mjson.tool
2.查询制定job
通过jid来获取任务详细信息
curl -k https://192.168.29.72:8000/jobs -H "X-Auth-Token: 0e827785dccd22163d025a71c0045771a06beff8" -d client='local' -d tgt='*' -d fun='test.ping'| python -mjson.tool
return": [
{
"20150503173832332733": {
"Arguments": [], # 任务参数
"Function": "test.ping", # 任务调用行数
"StartTime": "2015, May 03 17:38:32.332733",
"Target": "*", # 任务对象
"Target-type": "glob", # 匹配类型
"User": "root" # 执行用户
}
获取jid后,即可获得该任务详细信息
curl -k https://192.168.29.72:8000/jobs/20170503160855059500 -H "X-Auth-Token: 0e827785dccd22163d025a71c0045771a06beff8" | python -mjson.tool
3.运行runner
curl -k https://192.168.29.72:8000/jobs/ -H "X-Auth-Token: 0e827785dccd22163d025a71c0045771a06beff8" -d client="runner" -d fun="manage.status" | python -mjson.tool
参数解释: client : 模块,python处理salt-api的主要模块,‘client interfaces <netapi-clients>’ local : 使用‘LocalClient <salt.client.LocalClient>’ 发送命令给受控主机,等价于saltstack命令行中的‘salt‘命令 local_async : 和local不同之处在于,这个模块是用于异步操作的,即在master端执行命令后返回的是一个jobid,任务放在后台运行,通过产看jobid的结果来获取命令的执行结果。 runner : 使用‘RunnerClient<salt.runner.RunnerClient>‘ 调用salt-master上的runner模块,等价于saltstack命令行中的‘salt-run‘命令 runner_async : 异步执行runner模块 wheel : 使用‘WheelClient<salt.wheel.WheelClient>‘, 调用salt-master上的wheel模块,wheel模块没有在命令行端等价的模块,但它通常管理主机资源,比如文件状态,pillar文件,salt配置文件,以及关键模块<salt.wheel.key>功能类似于命令行中的salt-key。 wheel_async : 异步执行wheel模块 备注:一般情况下local模块,需要tgt和arg(数组),kwarg(字典),因为这些值将被发送到minions并用于执行所请求的函数。而runner和wheel都是直接应用于master,不需要这些参数。 tgt : minions fun : 函数 arg : 参数 expr_form : tgt的匹配规则 ‘glob‘ - Bash glob completion - Default ‘pcre‘ - Perl style regular expression ‘list‘ - Python list of hosts ‘grain‘ - Match based on a grain comparison ‘grain_pcre‘ - Grain comparison with a regex ‘pillar‘ - Pillar data comparison ‘nodegroup‘ - Match on nodegroup ‘range‘ - Use a Range server for matching ‘compound‘ - Pass a compound match string

浙公网安备 33010602011771号