SaltStack运维实战 ----代码整理

3.5 编写一个完整模快

# -*- coding: utf-8 -*-
 '''
The top nth processes which take up CPU and memory space usage are available
through this module, additionaly;
the module can get the system load information.
 '''
# Import python libs
import os
# Import salt libs
import salt.utils

def cpu(n):
    '''
    Return the top nth processes which take up the memory usage for this
minion
    CLI Example:
        salt '*' prank.cpu 
     '''
    cmd = "ps ax|sort -k3 -nr|head -n%s" %str(n)
    output = __salt__['cmd.run_stdout'](cmd,python_shell=True) 
    res=[]
    for line in output.splitlines():
	res.append(line)
    return output

def mem(n):
    '''
    Return the top nth processes which take up the CPU usage for this minion
    CLI Example:
        salt '*' prank.cpu 
     '''
    cmd = "ps axu|sort -k4 -nr|head -n%s" % str(n)
    output = __salt__['cmd.run_stdout'](cmd,python_shell=True)
    res = []
    for line in output.splitlines():
            res.append(line)
    return res



def load():
    '''
   Return the load averages for this minion
   CLI Example:
    .. code-block:: bash
       salt '*' prank.loadavg
     '''
    load_avg = os.getloadavg()
    return {'1-min': load_avg[0],
            '5-min': load_avg[1],
            '15-min': load_avg[2]}

问题:
发现原文中没有这个参数 python_shell=True
执行报错,加上后问题解决。

然后查找文档发现这么一句
:param bool python_shell:
If False, let python handle the positional arguments.
Set to True to use shell features, such as pipes or redirection.

文档链接
https://www.cnblogs.com/randomlee/p/Saltstack_module_cmd.html

发现cmd命令中如果有| 管道符 等符号 ,没 有python_shell=True参数,运行会报错。

你可能感兴趣的:(SaltStack)