shell - Ansible playbook shell 输出

我在使用ansible playbook,想用ps,dstat等命令快速监视某些主机,


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

它可以很好地显示每个主机的标准输出,如下所示:


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脚本,这太麻烦了,所以我将它放在playbook中:


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


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 1920x1200n
.... 

我还尝试添加register: var和'调试'任务来显示{{ var.stdout }},但是结果一样。

是否有一种方法,可以通过运行一个playbook命令从stdout/sheet1获得的标准输出输出

时间:


- 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: root@notty",
" 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: root@notty",
" 0.1 root java",
" 0.0 root sort -r -k1"
 ]
}


- 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

你还可以使用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

在.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 root@$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,
$ 

...