salt-api使用

启用salt-api 服务

这里简单的说明下,SaltStack官方支持三种REST API,分别是rest_cherry; rest_tonado和rest_wsgi, 本文选择使用rest_cherry模块来实现SaltStack的HTTP API。

配置salt-API服务

yum -y install salt-api
useradd -M -s /sbin/nologin saltapi
echo "saltapi" | passwd saltapi --stdin
vim /etc/salt/master
external_auth:
  pam:
    saltapi:
      - .*
      - '@wheel'
      - '@runner'

rest_cherrypy:
  port: 8000
  disable_ssl: Ture
  
  systemctl restart salt-master 
  systemctl restart salt-api 

使用CURL测试salt-API接口

第一步登录获得token,后续的命令使用token直接执行。

curl  -sk http://192.168.56.11:8000/login -H 'Accept: application/x-yaml' -d username='saltapi' -d password='saltapi' -d eauth='pam'

return:
- eauth: pam
  expire: 1522883646.381435
  perms:
  - .*
  - '@wheel'
  - '@runner'
  start: 1522840446.381431
  token: 86e323effd30bed7b2cdbcf4e70744048bbc25ca
  user: saltapi

第二步执行如下命令:salt 'linux-node2' test.ping

curl -sk http://192.168.56.11:8000/ -H 'Accept: application/json' -H 'X-Auth-Token: 86e323effd30bed7b2cdbcf4e70744048bbc25ca' -d client='local' -d tgt='linux-node2' -d fun='test.ping'  | python -mjson.tool  
{
    "return": [
        {
            "linux-node2": true
        }
    ]
}

执行带参数的命令:salt 'linux-node2' cmd.run 'free -m'

curl -sk http://192.168.56.11:8000/ -H 'Accept: application/json' -H 'X-Auth-Token: 86e323effd30bed7b2cdbcf4e70744048bbc25ca' -d client='local' -d tgt='linux-node2' -d fun='cmd.run' -d arg='whoami' | python -mjson.tool
{
    "return": [
        {
            "linux-node2": "root"
        }
    ]
}

salt API /run 接口

运行绕过正常会话处理的命令除此之外,该URL与根URL(/)相同。

curl -sk http://192.168.56.11:8000/run \
    -H 'Accept: application/x-yaml' \
    -H 'Content-type: application/json' \
    -d '[{
        "client": "local",
        "tgt": "linux-node2",
        "fun": "test.ping",
        "username": "saltapi",
        "password": "saltapi",
        "eauth": "pam"
    }]' 
return:
- linux-node2: true

使用python 实现如下命令

salt 'linux-node2' cmd.run 'free -m'
import requests

url='http://192.168.56.11:8000/login'
username = 'saltapi'
password = 'saltapi'

data = {'username': username,'password': password,'eauth': 'pam'}
headers = {'Accept': 'application/json','Content-Type': 'application/x-www-form-urlencoded'}
login_req = requests.post(url=url, headers=headers, data=data)
js = login_req.json()
token = js['return'][0].get('token')
print(token)


cmd_headers = {'Accept': 'application/json','Content-type': 'application/x-www-form-urlencoded', 'X-Auth-Token': token}
cmd_data = {'client': 'local', 'tgt': 'linux-node2', 'fun': 'cmd.run', 'arg': 'free -m '}
urls = 'http://192.168.56.11:8000'
req = requests.post(urls,headers=cmd_headers,data=cmd_data)
print(req.status_code)

if req.status_code == 200:
	print(req.json())

执行结果如下:

ab5d9ea0f428367c79f8eaf3dc62d39b1c45b4c9
200
{'return': [{'linux-node2': '              total        used        free      shared  buff/cache   available\nMem:            979         607          76          49         295         172\nSwap:             0           0           0'}]}

使用python编写过程中遇到的问题:

  • curl 请求时,默认会增加Content-type': 'application/x-www-form-urlencoded 的头部,所以在request请求头中也需要加入。
Share