2013-12-13 125 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