saltstack演示
python调用salt-api执行命令
saltstack演示
https://docs.saltstack.com/en/latest/
安装saltstack 源
yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest.el7.noarch.rpm
sever 端 192.168.78.128 安装 master api minion ssh syndic cloud
yum install -y salt-minion salt-master salt-api
cd /etc/salt
yum install -y salt-ssh salt-syndic salt-cloud
client 端 192.168.78.129 安装 minion ssh syndic cloud
yum install -y salt-minion salt-ssh salt-syndic salt-cloud
修改minion 配置文件 冒号后必须加空格
192.168.78.128
添加
master: 192.168.78.128 (salt server IP)
id: linux01
192.168.78.129
添加
master: 192.168.78.128(salt server IP
id: linux02
192.168.78.128 启动master 和minion
systemctl start salt-master
systemctl start salt-minion
192.168.78.129 启动minion
systemctl start salt-minion
192.168.78.128
运行
salt-key -L 列出所有客户端ID
salt-key -A 允许所有客户端
salt 'linux*' cmd.run 'df -h'
api 安装配置
yum install -y salt-api pyOpenSSL
pip install cherrypy==3.2.3
安装pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
cd /etc/pki/tls/certs/
make testcert 创建密钥
设置秘钥密码,(3次) ,剩下回车 Freeme123
cd ../private/
openssl rsa -in localhost.key -out localhost_nopass.key (Freeme123)
chmod 755 /etc/pki/tls/certs/localhost.crt
chmod 755 /etc/pki/tls/private/localhost.key
chmod 755 /etc/pki/tls/private/localhost_nopass.key
useradd -M -s /sbin/nologin saltapi
passwd saltapi (Freeme123)
sed -i '/#default_include/s/#default/default/g' /etc/salt/master
mkdir -p /etc/salt/master.d
cd /etc/salt/master.d
vim api.conf
rest_cherrypy:
port: 8001
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/private/localhost_nopass.key
vim eauch.conf
external_auth:
pam:
saltapi: # 用户
- .* # 该配置文件给予saltapi用户所有模块使用权限,出于安全考虑一般只给予特定模块使用权限
- '@runner'
- '@wheel'
systemctl restart salt-master
systemctl start salt-api
python调用salt-api执行命令
salt_client.py
代码
#!/usr/bin/env python
\# _\*_ coding:utf-8 _\*_
import requests
import json
try:
import cookielib
except:
import http.cookiejar as cookielib
\# 使用urllib2请求https出错,做的设置
import ssl
context = ssl.\_create\_unverified_context()
import os,sys
BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(\_\_file\_\_)))
sys.path.append(BASEDIR)
\# os.environ\['SET'\]='conf.settings'
from CMDB import settings
\# 使用requests请求https出现警告,做的设置
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
\# salt\_api = settings.SALT\_API
class SaltApi:
def \_\_init\_\_(self, url):
self.url = url
self.username = "saltapi"
self.password = "Freeme123"
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
"Content-type": "application/json"
}
self.params = {'client': 'local', 'fun': '', 'tgt': ''}
self.login\_url = salt\_api + "login"
self.login_params = {'username': self.username, 'password': self.password, 'eauth': 'pam'}
self.token = self.get\_data(self.login\_url, self.login_params)\['token'\] ## 线上生产环境推荐.get('token') 查不到,不会报错
self.headers\['X-Auth-Token'\] = self.token
def get_data(self, url, params):
send_data = json.dumps(params)
request = requests.post(url, data=send_data, headers=self.headers, verify=False)
response = request.json()
result = dict(response) ## 强制转换成字典形式
return result\['return'\]\[0\]
def salt_command(self, tgt, method, arg=None):
if arg:
# params = {'client': 'local', 'fun': method, 'tgt': tgt, 'arg': arg,'expr_form':'list'}
params = {'client': 'local', 'fun': method, 'tgt': tgt, 'arg': arg,}
else:
params = {'client': 'local', 'fun': method, 'tgt': tgt}
result = self.get_data(self.url, params)
return result
class MainClass(object):
def \_\_init\_\_(self):
self.salt\_api = settings.SALT\_API
self.method = None
self.params = \[\]
def \_main(self,salt\_method,salt\_params,salt\_client='*',test=False):
salt = SaltApi(self.salt_api)
salt_test = 'test.ping'
if test:
result1 = salt.salt\_command(salt\_client, salt_test)
return result1
result2 = salt.salt\_command(salt\_client, salt\_method, salt\_params)
return result2