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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值