2013-12-13 123 views
33

我想快速监视一些使用ps,dstat等命令的主机,使用ansible-playbook。该ansible命令本身完美的我想要做什么,比如我会用:Ansible playbook shell输出

ansible -m shell -a "ps -eo pcpu,user,args | sort -r -k1 | head -n5" 

,它很好地打印为每个主机的所有此类STD输出:

localhost | success | rc=0 >> 
0.0 root  /sbin/init 
0.0 root  [kthreadd] 
0.0 root  [ksoftirqd/0] 
0.0 root  [migration/0] 

otherhost | success | rc=0 >> 
0.0 root  /sbin/init 
0.0 root  [kthreadd] 
0.0 root  [ksoftirqd/0] 
0.0 root  [migration/0] 

然而,这需要我保持一堆周围shell脚本每个任务的这是不是很“ansible”所以我把这个在剧本:

--- 
- 
    hosts: all 
    gather_facts: no 
    tasks: 
    - shell: ps -eo pcpu,user,args | sort -r -k1 | head -n5 

-vv运行它,但输出baiscally示出了字典的内容和换行不打印这样所以这导致一个不可读的混乱这样的:

changed: [localhost] => {"changed": true, "cmd": "ps -eo pcpu,user,args | sort -r -k1 
head -n5 ", "delta": "0:00:00.015337", "end": "2013-12-13 10:57:25.680708", "rc": 0, 
"start": "2013-12-13 10:57:25.665371", "stderr": "", "stdout": "47.3 xxx Xvnc4 :24 
-desktop xxx:24 (xxx) -auth /home/xxx/.Xauthority -geometry 1920x1200\n 
.... 

我还尝试添加register: var和一个“调试”任务,以显示{{ var.stdout }}但结果是当然是一样的。

当通过剧本运行时,是否有办法从命令的stdout/stderr中获得格式良好的输出?我可以想到一些可能的方法(使用sed?格式输出重定向输出到主机上的文件,然后将该文件回传给屏幕?),但是由于我对shell的了解有限,一天试试吧。

+1

给也许想要输出的样本(这样做manualy) – NeronLeVelu

+0

@NeronLeVelu goodd点,增加 – stijn

回答

48

debug模块真的可以使用一些爱情,但此刻的你能做的最好的是使用此:

- hosts: all 
    gather_facts: no 
    tasks: 
    - shell: ps -eo pcpu,user,args | sort -r -k1 | head -n5 
     register: ps 

    - debug: var=ps.stdout_lines 

它给像这样的输出:

ok: [host1] => { 
    "ps.stdout_lines": [ 
     "%CPU USER  COMMAND", 
     " 1.0 root  /usr/bin/python", 
     " 0.6 root  sshd: [email protected] ", 
     " 0.2 root  java", 
     " 0.0 root  sort -r -k1" 
    ] 
} 
ok: [host2] => { 
    "ps.stdout_lines": [ 
     "%CPU USER  COMMAND", 
     " 4.0 root  /usr/bin/python", 
     " 0.6 root  sshd: [email protected] ", 
     " 0.1 root  java", 
     " 0.0 root  sort -r -k1" 
    ] 
} 
+2

你是怎么把它打印出来的呢?当我这样做时,输出全部在一行上。 –

+1

已接受!这基本上是geerlingguy的答案,但使用stdout_lines而不是stdout会给出合理的格式输出 – stijn

+2

[register](http://docs.ansible.com/playbooks_conditionals.html#id7)操作的相关文档。 –

13

这是一个开始可能是:

- hosts: all 
    gather_facts: no 
    tasks: 
    - shell: ps -eo pcpu,user,args | sort -r -k1 | head -n5 
     register: ps 

    - local_action: command echo item 
     with_items: ps.stdout_lines 

注:关于ps.stdout_lines文档是在这里涵盖:('Register Variables' chapter)

+1

这是一个开始,但是线是以完全随机的顺序输出的? – stijn

+0

Ansible将在没有任何排序的情况下一行一行地返回标准输出(我假定它只是运行不同的任务,并在收到它们时打印行)。为了将每个主机的线路保持在一起,在末尾加上| tr“\ n”“〜”这样的东西,将所有内容放在一行中,并在接收端再次分开 –

5

扩大在什么leucos他回答说,你也可以用Ansible谦卑debug模块打印的信息:

- hosts: all 
    gather_facts: no 
    tasks: 
    - shell: ps -eo pcpu,user,args | sort -r -k1 | head -n5 
     register: ps 

    # Print the shell task's stdout. 
    - debug: msg={{ ps.stdout }} 

    # Print all contents of the shell task's output. 
    - debug: var=ps 
+1

这只是打印出“msg”:“ “'为第一个调试任务,”msg“为:第二个为”Hello world!“' – stijn

+0

对于我来说,我得到'”msg“:”12.7“'作为第一个调试项(它只是打印第一行stdout),并为第二个调试条目打印整个stdout。我对本地主机(127.0.0.1)运行了手册。 – geerlingguy

+0

@stijn“Hello world!”是msg的默认值。 http://docs.ansible.com/ansible/debug_module.html – backslash112

-1

也许不相关,如果你正在寻找这样做只使用ansible。但它更容易让我有一个函数在我.bash_profile,然后运行_check_machine host1 host2

function _check_machine() { 
    echo 'hostname,num_physical_procs,cores_per_procs,memory,Gen,RH Release,bios_hp_power_profile,bios_intel_qpi_link_power_management,bios_hp_power_regulator,bios_idle_power_state,bios_memory_speed,' 
    hostlist=$1 
    for h in `echo $hostlist | sed 's/ /\n/g'`; 
    do 
     echo $h | grep -qE '[a-zA-Z]' 
     [ $? -ne 0 ] && h=plabb$h 
     echo -n $h, 
     ssh [email protected]$h 'grep "^physical id" /proc/cpuinfo | sort -u | wc -l; grep "^cpu cores" /proc/cpuinfo |sort -u | awk "{print \$4}"; awk "{print \$2/1024/1024; exit 0}" /proc/meminfo; /usr/sbin/dmidecode | grep "Product Name"; cat /etc/redhat-release; /etc/facter/bios_facts.sh;' | sed 's/Red at Enterprise Linux Server release //g; s/.*=//g; s/\tProduct Name: ProLiant BL460c //g; s/-//g' | sed 's/Red Hat Enterprise Linux Server release //g; s/.*=//g; s/\tProduct Name: ProLiant BL460c //g; s/-//g' | tr "\n" "," 
     echo '' 
    done 
} 

例如

$ _machine_info '10 20 1036' 
hostname,num_physical_procs,cores_per_procs,memory,Gen,RH Release,bios_hp_power_profile,bios_intel_qpi_link_power_management,bios_hp_power_regulator,bios_idle_power_state,bios_memory_speed, 
plabb10,2,4,47.1629,G6,5.11 (Tikanga),Maximum_Performance,Disabled,HP_Static_High_Performance_Mode,No_CStates,1333MHz_Maximum, 
plabb20,2,4,47.1229,G6,6.6 (Santiago),Maximum_Performance,Disabled,HP_Static_High_Performance_Mode,No_CStates,1333MHz_Maximum, 
plabb1036,2,12,189.12,Gen8,6.6 (Santiago),Custom,Disabled,HP_Static_High_Performance_Mode,No_CStates,1333MHz_Maximum, 
$ 

不用说功能不会为你工作,因为它是。你需要适当地更新它。

+0

输出并不糟糕,但其中一个要点就是我不必打扰hostlist/ssh/for循环等等。所以我不打算为功能降低该功能的便利性:P – stijn

+0

You我不回答这个问题。 “...使用可靠的剧本” –

2

如果您需要特定退出状态,Ansible通过回调插件提供了一种方法。

Example。如果您需要100%准确的退出状态,这是一个非常好的选择。

如果没有,您可以随时使用Debug Module,这是标准这种情况下使用。

干杯

1

我发现使用minimalstdout_callback与ansible-剧本给了相似的输出使用特设ansible。

在你ansible.cfg(请注意,我在OS X这样修改callback_plugins路径,以满足您的安装)

stdout_callback  = minimal 
callback_plugins = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/plugins/callback 

这样一个ansible-playbook任务像你

--- 
- 
    hosts: example 
    gather_facts: no 
    tasks: 
    - shell: ps -eo pcpu,user,args | sort -r -k1 | head -n5 

给出像这样的输出,就像一个特设命令会

example | SUCCESS | rc=0 >> 
%CPU USER  COMMAND 
0.2 root  sshd: [email protected]/3 
0.1 root  /usr/sbin/CROND -n 
0.0 root  [xfs-reclaim/vda] 
0.0 root  [xfs_mru_cache] 

我使用ansib然后LE-剧本2.2.1.0

0

ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook /tmp/foo.yml -vvv

任务与STDOUT将有部分:

STDOUT: 

What ever was in STDOUT