标签:规范 uri ann als == int eee red ide
‘‘‘
暂略
‘‘‘
字符编码问题
python2使用的unicode编码,python3使用str字符串。
执行模块能够返回不同格式的数据,salt可以为单个函数指定输出类型,譬如yaml,json等。
__outputter__是一个函数名和对应输出类型的字典,支持的输出类型参考链接:
https://docs.saltstack.com/en/2016.11/ref/output/all/index.html#all-salt-output
虚拟模块,可以重写一个已经存在的模块。
__VIRTUAL__会返回一个字符串,譬如True, False,如果返回TRUE,则使用当前模块名称加载模块,如果返回false,则不会加载模块。
在模块被加载之后__virtual__被调用,在这个时间点__salt__将不可用。
从__VIRTUAL__里面获取错误信息
示例:
1 ‘‘‘ 2 Cheese execution (or returner/beacon/etc.) module 3 ‘‘‘ 4 try: 5 import enzymes 6 HAS_ENZYMES = True 7 except ImportError: 8 HAS_ENZYMES = False 9 10 11 def __virtual__(): 12 ‘‘‘ 13 only load cheese if enzymes are available 14 ‘‘‘ 15 if HAS_ENZYMES: 16 return ‘cheese‘ 17 else: 18 return False, ‘The cheese execution module cannot be loaded: enzymes unavailable.‘ 19 20 ‘‘‘ 21 Cheese state module 22 ‘‘‘ 23 24 def __virtual__(): 25 ‘‘‘ 26 only load cheese if enzymes are available 27 ‘‘‘ 28 # predicate loading of the cheese state on the corresponding execution module 29 if ‘cheese.slice‘ in __salt__: 30 return ‘cheese‘ 31 else: 32 return False, ‘The cheese state module cannot be loaded: enzymes unavailable.‘
在minion端定义被重写后的模块,这样就覆盖了salt本身提供的模块:
1 providers: 2 service: systemd 3 pkg: aptpkg
虚拟模块的列表:
https://docs.saltstack.com/en/2016.11/ref/modules/all/salt.modules.pkg.html#virtual-pkg
__virtualname__是一个变量,可以重新定义模块名。
示例:
1 # Define the module‘s virtual name 2 __virtualname__ = ‘pkg‘ 3 4 5 def __virtual__(): 6 ‘‘‘ 7 Confine this module to Mac OS with Homebrew. 8 ‘‘‘ 9 10 if salt.utils.which(‘brew‘) and __grains__[‘os‘] == ‘MacOS‘: 11 return __virtualname__ 12 return False
salt ‘*‘ sys.doc #查看minion端的可用模块
在模块中添加使用帮助文档,这应该是作为一种规范的存在。
示例:
1 def spam(eggs): 2 ‘‘‘ 3 A function to make some spam with eggs! 4 5 CLI Example:: 6 7 salt ‘*‘ test.spam eggs 8 ‘‘‘ 9 return eggs
增加模块的说明文档,设置一个较好的规范格式来展示
1 :maintainer: Thomas Hatch <thatch@saltstack.com, Seth House <shouse@saltstack.com> 2 :maturity: new 3 :depends: python-mysqldb 4 :platform: all
调用日志模块,打印日志.
1 log = logging.getLogger(__name__) 2 3 log.info(‘Here is Some Information‘) 4 log.warning(‘You Should Not Do That‘) 5 log.error(‘It Is Busted‘)
设置模块函数的别名:
1 __func_alias__ = { 2 ‘set_‘: ‘set‘, 3 ‘list_‘: ‘list‘, 4 }
定义私有函数,以_下划线开头的函数
这样的函数会被加载到minion端:
1 def foo(bar): 2 return bar
而下面的就不会,python中定义私有方法就是以_开头的:
1 def _foobar(baz): # Preceded with an _ 2 return baz 3 4 cheese = {} # Not a callable Python object
在函数中使用装饰器.
示例:
1 import logging 2 3 from salt.utils.decorators import depends 4 5 log = logging.getLogger(__name__) 6 7 try: 8 import dependency_that_sometimes_exists 9 except ImportError as e: 10 log.trace(‘Failed to import dependency_that_sometimes_exists: {0}‘.format(e)) 11 12 @depends(‘dependency_that_sometimes_exists‘) 13 def foo(): 14 ‘‘‘ 15 Function with a dependency on the "dependency_that_sometimes_exists" module, 16 if the "dependency_that_sometimes_exists" is missing this function will not exist 17 ‘‘‘ 18 return True 19 20 def _fallback(): 21 ‘‘‘ 22 Fallback function for the depends decorator to replace a function with 23 ‘‘‘ 24 return ‘"dependency_that_sometimes_exists" needs to be installed for this function to exist‘ 25 26 @depends(‘dependency_that_sometimes_exists‘, fallback_function=_fallback) 27 def foo(): 28 ‘‘‘ 29 Function with a dependency on the "dependency_that_sometimes_exists" module. 30 If the "dependency_that_sometimes_exists" is missing this function will be 31 replaced with "_fallback" 32 ‘‘‘ 33 return True 34 35 36 from salt.utils.decorators import depends 37 38 HAS_DEP = False 39 try: 40 import dependency_that_sometimes_exists 41 HAS_DEP = True 42 except ImportError: 43 pass 44 45 @depends(HAS_DEP) 46 def foo(): 47 return True
标签:规范 uri ann als == int eee red ide
原文地址:http://www.cnblogs.com/solitarywares/p/7486105.html