57. Python saltstack 二次开发(2)

简介:

回顾上一节:

grains 和 pillar 都是定义他们的属性的

grains 定义在minion端(定义完必须重启minion,才能生效)

pillar  定义在master端(无需重启即可生效)

saltstack的api

Salt-api有两种方式:

第一种:是函数的形式,有人家定义好的函数,我们可以直接调用,直接写python代码调用函数或者类就可以了。

第二种:形式是salt-api有封装好的http协议的,我们需要启动一个服务端。



登录官网查看文档:

image.png


文档内容,如下:

image.png


安装salt-api:

1
yum  install  –y salt-api


1.加载master的配置文件

1
2
3
import   salt.config
master_opts  =  salt.config.client_config( '/etc/salt/master' )
print ( 'master_opts' )


master端,如果想查看配置文件的参数属性:

image.png


2. 加载minion的配置文件

1
2
3
import  salt.config
minion_opts  =  salt.config.minion_config( '/etc/salt/minion' )
print  ( 'minion_opts' )


minion端,想看配置文件内的参数属性:

image.png


3. 在master上执行各种模块:

1
2
3
>>>  import  salt.client
>>> local  =  salt.client.LocalClient( '/etc/salt/minion' )
>>> local.cmd( '*' "test.ping" )

返回:

1
{ '192.168.48.129' : True}


执行命令:

1
>> local.cmd( '*' "cmd.run" "w" )

【返回的是一个字典形式,很容易后期处理数据用】

1
{ '192.168.48.129' ' 12:17:38 up  5:58,  1 user,  load average: 0.00, 0.01, 0.05\nUSER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT\nroot     pts/0    192.168.48.1     11:14    2:50   0.89s  0.89s python' }


如果一次要执行多个模块:

一种方式:

1
>>> local.cmd( '*' , [ 'test.ping' 'cmd.run' ], [[], [ 'whoami' ]])

结果:

1
{ '192.168.48.129' : { 'test.ping' : True,  'cmd.run' 'root' }}

【test.ping 对应 [](空列表),cmd.run 对应 whoami 命令】

另一种方式:(不推荐这样用,还不如调两次,这样逻辑性不好)

1
>>> local.cmd( '*' , [ 'test.ping' 'cmd.run' ], [[], [ 'w; df -h' ]])


自定义的模块:

模块目录必须创建:

1
2
mkdir  -p  /srv/salt/_modules
cd   /srv/salt/_modules

创建 jd.py 文件:

vim jd.py

1
2
3
4
#!/usr/bin/python
#coding:utf-8
def  hello(name):
return  { "name" : name}

写完所有module要记得同步一下:

1
salt  '*'  saltutil.sync_all     #同步所有

或者

1
salt  '*'  saltutil.sync_modules     #只同步modules


image.png


执行命令获取结果:

1
# salt '*' jd.hello  ajing

结果输出,如图:

image.png


python交互界面中这样获取:

image.png


Master端执行salt-run

如果对于执行时间过长,没法直接返回的情况,我们就可以通过异步执行的形式进行返回 cmd_async 和 get_cache_returns()

先使用salt-run命令:

(1)命令行使用salt-run查看服务器状态

1
# salt-run manage.status

返回结果:

1
2
3
down:
up:
- 192.168.48.129


(2)使用 runner 模块查看客户端服务器状态:

1
2
3
4
5
6
import  salt.config
import  salt.runner
__opts__  =  salt.config.client_config( '/etc/salt/master' )
runermaster  =  salt.runner.RunnerClient(__opts__)
runnerMaster.cmd( 'jobs.list_jobs' , [])
runnerMaster.cmd( 'manage.status' )

返回结果:

1
2
3
4
down:
up:
- 192.168.48.129
{ 'down' : [],  'up' : [ '192.168.48.129' ]}


【以下例子代码只能只能在master上执行,而且是只能在master上才可以使用】

vim test.py

1
2
3
4
5
6
7
8
9
10
11
12
13
import  salt.config
import  salt.client
def  get_result(sleep_interval = 1 ):
     __opts__  =  salt.config.client_config( '/etc/salt/master' )
     localclient  =  salt.client.LocalClient(__opts__)
     jid  =  localclient.cmd_async( "*" , cmd.run,  'df -h' )
     #returns = localclient.get_cache_returns(jid)
     wait_time  =  0
     while  sleep_interval < __opts__[ 'timeout' ]:
         results  =  localclient.get_cache_returns(jid)
             if  returns:
                 return  results
             wait_time  + =  sleep_interval

这个 test.py 文件的位置是在 /srv/salt/_runner 里面,图中为定义 _runner 的目录位置 (文件:/etc/salt/master)

先创建 _runner 目录,修改文件 /etc/salt/master 的 runner路径参数:

image.png

先创建 _runner 目录,修改文件 /etc/salt/master 的 runner路径参数:

image.png

记得重启master服务

通过python命令测试并确认目录位置:

image.png


【补充】

客户端的获取结果的方式为:

1
2
3
4
import  salt.config
import  salt.client
caller  =  salt.client.Caller( '/etc/salt/minion' )
caller.cmd( "test.ping" )

得到结果:

1
True


所以,对比一下master端和minion端获取值的方式不同:

1
2
3
local  = salt.client.LocalClient( '/etc/salt/minion' ) #master端
对比:
caller = salt.client.Caller(‘ /etc/salt/minion ’) #minion端,无法使用LocalClient

类似shell命令的salt-call,可以在minion端执行salt的命令,测试连通性等操作。


Salt的内置环境变量:

在python的交互环境中,这些变量是不生效的,只有在自定义的模块,活着salt执行时才生效

(1)__opts__         #配置文件,类型等 最常用

(2)__salt__          #执行、调用 modules  最常用

举例:

1
2
__salt__[ 'cmd.run' ]( 'fdisk -l' )         ##__salt__[模块](参数)
__salt__[ 'network.ip_addrs' ]()        ##同上

【__salt__是个字典,它里面装了minion上所有的modules,__salt__的key是一个个的模块名称,value则是模块里面的一个个函数】

看个例子看看内建模块是怎么调用的:

vim /srv/salt/_modules/foo.py

1
2
def  foo(name):
     return  "I am %s"  %  name

cheng.py 也是定义的一个模块,目的是通过调用__salt__调用上面定义的那个foo模块中的foo函数

vim /srv/salt/_modules/cheng.py

1
2
def  cheng(name):
     return  __salt__[ 'foo.foo' ](name)


ok,同步一下模块:

1
salt  '*'  saltutil.sync_modules

同步完成

1
2
3
salt - minion:
     -  modules.cheng
     -  modules.foo



看一下结果:

1
salt  '*'  cheng.cheng Lilith

结果:

1
2
salt - minion:
     I am Lilith

通过这个例子,可以知道怎么调用__salt__里面的函数了。


(3)__pillar__ 之前说过,pillar很少用到

(4)__grains__ grains 比pillar还要少用到

举例1:

1
2
3
4
5
import  salt.config
import  salt.loader
__opts__  =  salt.config.minion_config( "/etc/salt/minion" )
__grains__  =  salt.loader.grains(__opts__)
__grains__[ 'id' ]

获得结果:

1
192.168.48.129


举例2:

1
2
3
4
5
6
7
8
import  salt.config
import  salt.loader
__opts__  =  salt.config.minion_config( '/etc/salt/minion' )
__grains__  =  salt.loader.grains(__opts__)
__opts__[ 'grains' =  __grains__
__utils__  =  salt.loader.utils(__opts__)
__salt__  =  salt.loader.minion_mods(__opts__, utils = __utils__)
__salt__[ 'test.ping' ]()


(5)__context__

1
2
if  not  'cp.fileclient'  in  __context__:
     __context__[ 'cp.fileclient' =  salt.fileclient.get_file_client(__opts__)



本文转自 听丶飞鸟说 51CTO博客,原文链接:http://blog.51cto.com/286577399/2068913

相关文章
|
12月前
|
JSON 监控 前端开发
python对接API二次开发高级实战案例解析:Zabbix API封装类实现获取认证密钥、所有主机组、所有主机、所有监控项和历史数据
python对接API二次开发高级实战案例解析:Zabbix API封装类实现获取认证密钥、所有主机组、所有主机、所有监控项和历史数据
381 0
|
2月前
|
API 开发工具 计算机视觉
华视 CVR-100UC 身份证读取 Python 二次开发(包含SDK下载地址)
华视 CVR-100UC 身份证读取 Python 二次开发(包含SDK下载地址)
|
12月前
|
JavaScript 前端开发 API
python对接API二次开发高级实战案例解析:百度地图Web服务API封装函数(行政区划区域检索、地理编码、国内天气查询、IP定位、坐标转换)
python对接API二次开发高级实战案例解析:百度地图Web服务API封装函数(行政区划区域检索、地理编码、国内天气查询、IP定位、坐标转换)
315 0
|
应用服务中间件 nginx Python
|
Web App开发 程序员 API
python开发初期及二次开发C api
1,python2 or python 区别, https://wiki.python.org/moin/Python2orPython3 python software foundation 2,python 应用(最近Ruiy在搞openstack知道在openstack上有一个web框架...
1009 0
|
消息中间件 监控 网络协议
SaltStack安装Apache/Mysql/PHP部署Wordpress
SaltStack是一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)构建。 SaltStack 采用 C/S模式,server端就是salt的master,client端就是minion,minion与master之间通过ZeroMQ消息队列通信。 master监听4505和4506端口,4505对应的是ZMQ的PUB system,用来发送消息,4506对应的是REP system是来接受
158 0