python / 分布式管理-salt / 未分类 · 2013年9月29日

centos5.x 系列使用 salt 内置的nginx 模块

查看模块
find / -name nginx.*
cat /usr/lib/python2.6/site-packages/salt/modules/nginx.py
import salt.utils
# Cache the output of running which(‘nginx’) so this module
# doesn’t needlessly walk $PATH looking for the same binary
# for nginx over and over and over for each function herein
@salt.utils.memoize
def __detect_os():
return salt.utils.which(‘nginx’)
def __virtual__():
”’
Only load the module if nginx is installed
”’
if __detect_os():
return ‘nginx’
return False
def version():
”’
Return server version from nginx -v
CLI Example::
salt ‘*’ nginx.version 问丹爷下面的源码
”’
cmd = ‘{0} -v’.format(__detect_os())
out = __salt__[‘cmd.run’](cmd).splitlines()
ret = out[0].split(‘: ‘)
return ret[-1]
由上面打开这模块源码可以要用 salt.utils
但是导入这模块时报错 原来是python版本太老了
Python 2.4.3 (#1, Sep 3 2009, 15:37:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import salt.utils
Traceback (most recent call last):
File ““, line 1, in ?
ImportError: No module named salt.utils
#master 和 minion改为新版本 python就好了
vi /usr/bin/yum
mv /usr/bin/python /usr/bin/python24bak
ln -s /usr/bin/python26 /usr/bin/python

搞定
测试私有方法
>>> a = __detect_os()
>>> a
‘/usr/sbin/nginx’
解读
cmd = ‘{0} -v’.format(__detect_os()) #{0} 数组第一个
out = __salt__[‘cmd.run’](cmd).splitlines() #切成一行行
ret = out[0].split(‘: ‘) 上面的第一行
return ret[-1]