python调用salstack

简介: Python API调用saltstack

Python API调用saltstack

SaltStack本身提供salt(usr/bin/salt)来交互管理,但是去服务器上敲命令肯定不是一个长远之计,这时候python就体现了nb的功能。


Python API就是给Python提供的API使用,需要在SaltStack master上运行


1.实例代码

[root@master  ~]$python

Python 2.7.5 (default, Jul 13 2018, 13:06:57)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import salt.client

>>> local = salt.client.LocalClient()  #<salt.client.LocalClient object at 0x7f886dbdd850>

>>> local.cmd('*','cmd.run',['hostname'])#向所有minion发送命令

{'slave': 'slave'}

因此python API就是提供了向saltstack发送命令的入口。


2.通过API获取saltstack的配置文件

(1)获取master配置文件

>>> import salt.config #导入salt配置模块

>>> m_opts=salt.config.client_config('/etc/salt/master') #读取salt配置文件,得到一个字典数据


(2)获取minion配置文件

Python 2.7.5 (default, Jul 13 2018, 13:06:57)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import salt.client

>>> salt.config.minion_config('/etc/salt/minion') #读取minion配置文件,得到字典数据,通过字典方法可以查看信息


#############################################################################


Python API介绍

/usr/bin/salt默认使用的接口是LocalClient,该接口只能在salt master上使用

[root@master  ~]$python

Python 2.7.5 (default, Jul 13 2018, 13:06:57)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import salt.client

>>> local = salt.client.LocalClient()  #<salt.client.LocalClient object at 0x7f886dbdd850>

>>> local.cmd('*','cmd.run',['hostname'])#向所有minion发送命令

{'slave': 'slave'}


逐条返回结果,local.cmd_iter()

>>> ret=local.cmd_iter('*','test.ping')

>>> ret

<generator object cmd_iter at 0x7f886d455c80>

>>> for i in ret:

...     print i

...

{'slave': {'retcode': 0, 'ret': True}}


菲波那切数列

>>> local.cmd('*','test.fib',[10])

{'slave': [[0, 1, 1, 2, 3, 5, 8], 2.1457672119140625e-06]}


检查minion服务器信息

>>> local.cmd('*','cmd.run',['hostname'])

{'slave': 'slave'}

>>> local.cmd('*','cmd.run',['ifconfig'])

>>> local.cmd('*','cmd.run',['crontab -l'])

>>> local.cmd('*','cmd.run',['df -h'])


启停minion的服务,如nginx

>>> local.cmd('*','service.stop',['nginx'])

{'slave': True}

>>> local.cmd('*','service.status',['nginx'])

{'slave': False}

>>> local.cmd('*','service.start',['nginx'])

{'slave': True}


相关文章
|
4月前
|
Java C++ Python
Python中do_something函数
【4月更文挑战第5天】要创建同名函数,可定义如下: ```python def do_something(argument1, argument2): pass ``` `pass`是空操作,实际使用应替换为具体代码。若要实现加法功能: ```python def do_something(argument1, argument2): result = argument1 + argument2 return result result = do_something(3, 4) print(result) # 输出:7 ```
64 3
Python中do_something函数
|
3月前
|
Python
【干货】Python中几个有趣的函数
【干货】Python中几个有趣的函数
15 2
|
2月前
|
Python
|
3月前
|
缓存 Python 容器
12.Python 函数
12.Python 函数
23 0
|
4月前
|
Python
python魔法方法介绍
Python的魔法方法,如`__init__`(构造)、`__new__`(对象创建)和`__del__`(析构),是双下划线包围的预定义方法,用于赋予类特殊行为:初始化实例、定制对象创建和资源释放。通过重载这些方法,可增强类的功能。
23 0
|
4月前
|
C++ Python
【C++/Python】C++调用python文件
【C++/Python】C++调用python文件
|
4月前
|
Python
python中使用fillna()函数
python中使用fillna()函数
612 1
|
4月前
|
Python
Python的函数
Python的函数
18 0
|
4月前
|
算法 数据处理 Python
python中为什么要使用函数
python中为什么要使用函数
217 2
|
4月前
|
Python
【python】函数详解
【python】函数详解
50 0