利用saltstack结合mysql数据库测试返回值:
1,利用minion端的配置测试mysql的返回值:
[root@server2 tmp]# yum install MySQL-python
[root@server2 tmp]# vim /etc/salt/minion
[root@server2 tmp]# cat /etc/salt/minion | tail -n 13
mysql.host: '172.25.38.1' master端IP
mysql.user: 'salt' 用户
mysql.pass: 'westos' 密码
mysql.db: 'salt'
mysql.port: 3306 端口
###### Miscellaneous settings ######
############################################
# Default match type for filtering events tags: startswith, endswith, find, regex, fnmatch
#event_match_type: startswith
#
[root@server2 tmp]# /etc/init.d/salt-minion restart 重启服务
Stopping salt-minion:root:server2 daemon: OK
Starting salt-minion:root:server2 daemon: OK
在server1配置mysql进行授权:
[root@server1 tmp]# yum install -y mysql-server 安装数据库
[root@server1 tmp]# /etc/init.d/mysqld start 开启服务
[root@server1 tmp]# mysql 直接登陆回车
mysql> grant all on salt.* to salt@'172.25.38.%' identified by 'westos'; 授权用户密码给172.25.38网段
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@server1 tmp]# vim test.sql
[root@server1 tmp]# mysql < test.sql 将数据库导入test.sql
[root@server1 tmp]# cat test.sql
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `salt`;
--
-- Table structure for table `jids`
--
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- CREATE INDEX jid ON jids(jid) USING BTREE;
--
-- Table structure for table `salt_returns`
--
DROP TABLE IF EXISTS `salt_returns`;
CREATE TABLE `salt_returns` (
`fun` varchar(50) NOT NULL,
`jid` varchar(255) NOT NULL,
`return` mediumtext NOT NULL,
`id` varchar(255) NOT NULL,
`success` varchar(10) NOT NULL,
`full_ret` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_events`
--
DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
[root@server1 tmp]# salt 'server2' test.ping --return mysql 测试返回值
server2:
True
[root@server1 tmp]# mysql 登陆数据库
mysql> use salt 使用salt数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from salt_returns; 查看返回值
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| fun | jid | return | id | success | full_ret | alter_time |
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| test.ping | 20180818152931308839 | true | server2 | 1 | {"fun_args": [], "jid": "20180818152931308839", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "server2"} | 2018-08-18 15:29:31 |
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
1 row in set (0.00 sec)
2,利用master端的配置测试mysql的返回值:
[root@server1 tmp]# yum install MySQL-python -y
[root@server1 tmp]# vim /etc/salt/master 在master端配置mysql
[root@server1 tmp]# /etc/init.d/salt-master restart 重载服务,由于master服务关闭比较慢我们可以先stop然后start开启服务即可
Stopping salt-master daemon: [ OK ]
Starting salt-master daemon: [ OK ]
[root@server1 tmp]# mysql
mysql> grant all on salt.* to salt@localhost identified by 'westos'; 给本机授权
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; 刷新策略
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@server1 tmp]# salt server3 cmd.run 'df -h' 推送显示server3的磁盘空间
server3:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 1.1G 17G 6% /
tmpfs 246M 16K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
[root@server1 tmp]# mysql 登陆数据库
mysql> use salt 使用salt数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from salt_returns; 查看返回值
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| fun | jid | return | id | success | full_ret | alter_time |
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| test.ping | 20180818152931308839 | true | server2 | 1 | {"fun_args": [], "jid": "20180818152931308839", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "server2"} | 2018-08-18 15:29:31 |
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
利用python脚本调用显示所有主机磁盘信息:
[root@server1 tmp]# mkdir /srv/salt/_modules
[root@server1 tmp]# cd /srv/salt/
[root@server1 salt]# ls
_grains haproxy httpd keepalived _modules nginx pkgs top.sls users
[root@server1 salt]# cd _modules/
[root@server1 _modules]# ls
[root@server1 _modules]# vim my_disk.py
[root@server1 _modules]# cat my_disk.py
#!/usr/bin/env python
def df():
return __salt__['cmd.run']('df -h')
[root@server1 _modules]# salt '*' saltutil.sync_modules
server1:
- modules.my_disk
server4:
- modules.my_disk
server2:
- modules.my_disk
server3:
- modules.my_disk
[root@server1 _modules]# salt '*' my_disk.df
server3:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 1.1G 17G 6% /
tmpfs 246M 16K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
server4:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 1.1G 17G 7% /
tmpfs 246M 16K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
server2:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 991M 17G 6% /
tmpfs 246M 48K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
server1:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 1.1G 17G 7% /
tmpfs 246M 56K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
搭建top master用来管理各个master,实现了master端的横向扩展,减轻了master端的负载压力:
[root@server1 _modules]# salt-key -L
Accepted Keys:
server1
server2
server3
server4
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@server1 _modules]# salt-key -d server4 删除server4的公钥信息
The following keys are going to be deleted:
Accepted Keys:
server4
Proceed? [N/y] y
Key for minion server4 deleteed.
[root@server1 _modules]# salt-key -L 显示公钥信息
Accepted Keys:
server1
server2
server3
Denied Keys:
Unaccepted Keys:
Rejected Keys:
server4作为top-master:
[root@server4 extmods]# /etc/init.d/salt-minion stop 关闭minion服务
Stopping salt-minion:root:server4 daemon: OK
[root@server4 extmods]# chkconfig salt-minion off 开机自动关闭
[root@server4 extmods]# /etc/init.d/haproxy stop 停止haproxy服务
Stopping haproxy: [ OK ]
[root@server4 extmods]# /etc/init.d/keepalived stop 停止keepalived服务
Stopping keepalived: [ OK ]
[root@server4 extmods]# yum install salt-master -y
[root@server4 extmods]# cd /etc/salt/
[root@server4 salt]# ls
cloud cloud.maps.d master minion.d proxy
cloud.conf.d cloud.profiles.d master.d minion_id proxy.d
cloud.deploy.d cloud.providers.d minion pki roster
[root@server4 salt]# /etc/init.d/salt-master start 开启master服务
Starting salt-master daemon: [ OK ]
[root@server4 salt]# salt-key -L 显示公钥信息为空
Accepted Keys:
Denied Keys:
Unaccepted Keys:
Rejected Keys:
在server1配置syndic:
[root@server1 _modules]# yum install salt-syndic
[root@server1 _modules]# cd /etc/salt/
[root@server1 salt]# ls
cloud cloud.maps.d master minion.d proxy
cloud.conf.d cloud.profiles.d master.d minion_id proxy.d
cloud.deploy.d cloud.providers.d minion pki roster
[root@server1 salt]# vim master 写入top master的IP
[root@server1 salt]# /etc/init.d/salt-master stop 重启master服务因为修改配置文件必须重启服务生效
Stopping salt-master daemon: [ OK ]
[root@server1 salt]# /etc/init.d/salt-master start
Starting salt-master daemon: [ OK ]
[root@server1 salt]# /etc/init.d/salt-syndic start 牢记开启syndic服务不然无法交换公钥
Starting salt-syndic daemon: [ OK ]
在server4添加公钥信息:
[root@server4 salt]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
server1
Rejected Keys:
[root@server4 salt]# salt-key -A 添加公钥
The following keys are going to be accepted:
Unaccepted Keys:
server1
Proceed? [n/Y] y
Key for minion server1 accepted.
[root@server4 salt]# salt-key -L 添加成功
Accepted Keys:
server1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@server4 salt]# salt '*' test.ping 在top master可以看到master管理的节点,减轻了master端的压力
server1:
True
server3:
True
server2:
True
测试salt-ssh模块:
[root@server3 ~]# /etc/init.d/salt-minion stop 关闭server3的salt-minion的服务
Stopping salt-minion:root:server3 daemon: OK
[root@server3 ~]#
[root@server1 salt]# vim /etc/salt/roster
[root@server1 salt]# cat /etc/salt/roster | tail -n 4
server3:
host: 172.25.38.3
user: root
passwd: redhat
[root@server1 salt]# vim /etc/salt/master 注释掉加入的mysql数据库信息不然会读取文件报错
[root@server1 salt]# salt-ssh '*' test.ping
server3:
True
[root@server1 salt]# salt-ssh '*' test.ping -i
server3:
True
[root@server1 salt]# salt-ssh 'server3' test.ping
server3:
True
[root@server1 salt]# salt-ssh 'server3' my_disk.df 直接调用ssh服务可以查看server3的磁盘信息
server3:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 1.1G 17G 7% /
tmpfs 246M 16K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
salt工具之api:
[root@server3 ~]# /etc/init.d/salt-minion start 开启服务
Starting salt-minion:root:server3 daemon: OK
[root@server3 ~]#
[root@server1 certs]# yum install salt-api -y 在server1安装api模块
[root@server1 salt]# cd /etc/pki/
[root@server1 pki]# ls
CA ca-trust entitlement java nssdb product rpm-gpg rsyslog tls
[root@server1 pki]# cd tls/
[root@server1 tls]# ls
cert.pem certs misc openssl.cnf private
[root@server1 tls]# openssl genrsa 1024 > localhost.key 生成localhost.key
Generating RSA private key, 1024 bit long modulus
........................++++++
..........++++++
e is 65537 (0x10001)
[root@server1 tls]# ls
cert.pem certs localhost.key misc openssl.cnf private
[root@server1 tls]# mv localhost.key private/
[root@server1 tls]# cd private/
[root@server1 private]# ls
localhost.key
[root@server1 private]# cd ..
[root@server1 tls]# cd certs/
[root@server1 certs]# ls
ca-bundle.crt ca-bundle.trust.crt make-dummy-cert Makefile renew-dummy-cert
[root@server1 certs]# make testcert 生成证书
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
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) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server1
Email Address []:root@localhost
[root@server1 certs]# ls
ca-bundle.crt ca-bundle.trust.crt localhost.crt make-dummy-cert Makefile renew-dummy-cert
[root@server1 certs]# ll localhost.crt
-rw------- 1 root root 1029 Aug 18 17:17 localhost.crt
[root@server1 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# ls
ca-bundle.crt localhost.crt Makefile
ca-bundle.trust.crt make-dummy-cert renew-dummy-cert
[root@server1 certs]# cd /etc/salt/
[root@server1 salt]# ls
cloud cloud.maps.d master minion.d proxy
cloud.conf.d cloud.profiles.d master.d minion_id proxy.d
cloud.deploy.d cloud.providers.d minion pki roster
[root@server1 salt]# cd master.d/
[root@server1 master.d]# ls
[root@server1 master.d]# pwd
/etc/salt/master.d
[root@server1 master.d]# ls
[root@server1 master.d]# cd ..
[root@server1 salt]# cd master.d/
[root@server1 master.d]# vim api.conf
[root@server1 master.d]# cat api.conf
rest_cherrypy:
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/private/localhost.key
[root@server1 master.d]# ll /etc/pki/tls/private/localhost.key
-rw-r--r-- 1 root root 887 Aug 18 17:15 /etc/pki/tls/private/localhost.key
[root@server1 master.d]# ll /etc/pki/tls/certs/localhost.crt
-rw------- 1 root root 1029 Aug 18 17:17 /etc/pki/tls/certs/localhost.crt
[root@server1 master.d]# vim auth.conf
[root@server1 master.d]# cat auth.conf
external_auth:
pam:
saltapi:
- '.*'
- '@wheel'
- '@runner'
- '@jobs'
[root@server1 master.d]# useradd saltapi 建立用户
[root@server1 master.d]# passwd saltapi 设置密码
Changing password for user saltapi.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@server1 master.d]# ls
api.conf auth.conf
[root@server1 master.d]# /etc/init.d/salt-master stop 重启服务
Stopping salt-master daemon: [ OK ]
[root@server1 master.d]# /etc/init.d/salt-master start
Starting salt-master daemon: [ OK ]
[root@server1 master.d]# /etc/init.d/salt-master status 查看状态
salt-master (pid 9153) is running...
[root@server1 master.d]# /etc/init.d/salt-api status
salt-api is stopped
[root@server1 master.d]# /etc/init.d/salt-api start 开启api服务
Starting salt-api daemon: [ OK ]
[root@server1 master.d]# netstat -antlp | grep :8000 可以查看到8000端口
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 13394/python2.6
tcp 0 0 127.0.0.1:34543 127.0.0.1:8000 TIME_WAIT -
[root@server1 master.d]# curl -sSk https://localhost:8000/login \ 调用curl命令测试
> -H 'Accept: application/x-yaml' \
> -d username=saltapi \
> -d password=westos \
> -d eauth=pam
return:
- eauth: pam
expire: 1534628717.605648
perms:
- .*
- '@wheel'
- '@runner'
- '@jobs'
start: 1534585517.6056471
token: 12a1c98fab342223dcba3580024be39dcd22e6f2
user: saltapi
[root@server1 master.d]# curl -sSk https://localhost:8000/login -H 'Accept: application/x-yaml' -d username=saltapi -d password=westos -d eauth=pam
return:
- eauth: pam
expire: 1534629488.2124641
perms:
- .*
- '@wheel'
- '@runner'
- '@jobs'
start: 1534586288.2124619
token: 5d73f0f297815d16692030e437374f75e235d1dd
user: saltapi
[root@server1 master.d]# curl -sSk https://localhost:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 5d73f0f297815d16692030e437374f75e235d1dd' -d client=local -d tgt='*' -d fun=test.ping
return:
- server1: true
server2: true
server3: true
[root@server1 ~]# vim saltapi.py 编写脚本测试打印 print sapi.list_all_key()信息
[root@server1 ~]# python saltapi.py
([u'server1', u'server2', u'server3'], [])
[root@server1 ~]# cat saltapi.py
# -*- coding: utf-8 -*-
import urllib2,urllib
import time
try:
import json
except ImportError:
import simplejson as json
class SaltAPI(object):
__token_id = ''
def __init__(self,url,username,password):
self.__url = url.rstrip('/')
self.__user = username
self.__password = password
def token_id(self):
''' user login and get token id '''
params = {'eauth': 'pam', 'username': self.__user, 'password': self.__password}
encode = urllib.urlencode(params)
obj = urllib.unquote(encode)
content = self.postRequest(obj,prefix='/login')
try:
self.__token_id = content['return'][0]['token']
except KeyError:
raise KeyError
def postRequest(self,obj,prefix='/'):
url = self.__url + prefix
headers = {'X-Auth-Token' : self.__token_id}
req = urllib2.Request(url, obj, headers)
opener = urllib2.urlopen(req)
content = json.loads(opener.read())
return content
def list_all_key(self):
params = {'client': 'wheel', 'fun': 'key.list_all'}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
minions = content['return'][0]['data']['return']['minions']
minions_pre = content['return'][0]['data']['return']['minions_pre']
return minions,minions_pre
def delete_key(self,node_name):
params = {'client': 'wheel', 'fun': 'key.delete', 'match': node_name}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0]['data']['success']
return ret
def accept_key(self,node_name):
params = {'client': 'wheel', 'fun': 'key.accept', 'match': node_name}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0]['data']['success']
return ret
def remote_noarg_execution(self,tgt,fun):
''' Execute commands without parameters '''
params = {'client': 'local', 'tgt': tgt, 'fun': fun}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0][tgt]
return ret
def remote_execution(self,tgt,fun,arg):
''' Command execution with parameters '''
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0][tgt]
return ret
def target_remote_execution(self,tgt,fun,arg):
''' Use targeting for remote execution '''
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg, 'expr_form': 'nodegroup'}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
jid = content['return'][0]['jid']
return jid
def deploy(self,tgt,arg):
''' Module deployment '''
params = {'client': 'local', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
return content
def async_deploy(self,tgt,arg):
''' Asynchronously send a command to connected minions '''
params = {'client': 'local_async', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
jid = content['return'][0]['jid']
return jid
def target_deploy(self,tgt,arg):
''' Based on the node group forms deployment '''
params = {'client': 'local_async', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg, 'expr_form': 'nodegroup'}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
jid = content['return'][0]['jid']
return jid
def main():
sapi = SaltAPI(url='https://172.25.38.1:8000',username='saltapi',password='westos')
#sapi.token_id()
print sapi.list_all_key()
#sapi.delete_key('test-01')
#sapi.accept_key('test-01')
#sapi.deploy('server3','nginx')
#print sapi.remote_noarg_execution('test-01','grains.items')
if __name__ == '__main__':
main()
[root@server3 ~]# /etc/init.d/nginx status
nginx (pid 1801) is running...
[root@server3 ~]# /etc/init.d/nginx stop 将server3的nginx服务关闭
Stopping nginx: [ OK ]
[root@server1 ~]# vim saltapi.py 调用脚本打开serevr3的nginx服务
[root@server1 ~]# python saltapi.py
[root@server1 ~]# cat saltapi.py
# -*- coding: utf-8 -*-
import urllib2,urllib
import time
try:
import json
except ImportError:
import simplejson as json
class SaltAPI(object):
__token_id = ''
def __init__(self,url,username,password):
self.__url = url.rstrip('/')
self.__user = username
self.__password = password
def token_id(self):
''' user login and get token id '''
params = {'eauth': 'pam', 'username': self.__user, 'password': self.__password}
encode = urllib.urlencode(params)
obj = urllib.unquote(encode)
content = self.postRequest(obj,prefix='/login')
try:
self.__token_id = content['return'][0]['token']
except KeyError:
raise KeyError
def postRequest(self,obj,prefix='/'):
url = self.__url + prefix
headers = {'X-Auth-Token' : self.__token_id}
req = urllib2.Request(url, obj, headers)
opener = urllib2.urlopen(req)
content = json.loads(opener.read())
return content
def list_all_key(self):
params = {'client': 'wheel', 'fun': 'key.list_all'}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
minions = content['return'][0]['data']['return']['minions']
minions_pre = content['return'][0]['data']['return']['minions_pre']
return minions,minions_pre
def delete_key(self,node_name):
params = {'client': 'wheel', 'fun': 'key.delete', 'match': node_name}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0]['data']['success']
return ret
def accept_key(self,node_name):
params = {'client': 'wheel', 'fun': 'key.accept', 'match': node_name}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0]['data']['success']
return ret
def remote_noarg_execution(self,tgt,fun):
''' Execute commands without parameters '''
params = {'client': 'local', 'tgt': tgt, 'fun': fun}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0][tgt]
return ret
def remote_execution(self,tgt,fun,arg):
''' Command execution with parameters '''
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
ret = content['return'][0][tgt]
return ret
def target_remote_execution(self,tgt,fun,arg):
''' Use targeting for remote execution '''
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg, 'expr_form': 'nodegroup'}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
jid = content['return'][0]['jid']
return jid
def deploy(self,tgt,arg):
''' Module deployment '''
params = {'client': 'local', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
return content
def async_deploy(self,tgt,arg):
''' Asynchronously send a command to connected minions '''
params = {'client': 'local_async', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
jid = content['return'][0]['jid']
return jid
def target_deploy(self,tgt,arg):
''' Based on the node group forms deployment '''
params = {'client': 'local_async', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg, 'expr_form': 'nodegroup'}
obj = urllib.urlencode(params)
self.token_id()
content = self.postRequest(obj)
jid = content['return'][0]['jid']
return jid
def main():
sapi = SaltAPI(url='https://172.25.38.1:8000',username='saltapi',password='westos')
#sapi.token_id()
#print sapi.list_all_key()
#sapi.delete_key('test-01')
#sapi.accept_key('test-01')
sapi.deploy('server3','nginx.service')
#print sapi.remote_noarg_execution('test-01','grains.items')
if __name__ == '__main__':
main()
[root@server3 ~]# /etc/init.d/nginx status 在server3查看nginx服务已经开启
nginx (pid 3784) is running...
[root@server3 ~]#