68

ansible-playbook を使用して ps や dstat などのコマンドを使用して、いくつかのホストをすばやく監視したいと考えています。コマンド自体は、たとえば次のansibleように、私が望むことを完全に実行します。

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] 

ただし、これには、すべてのタスクに対して一連のシェルスクリプトを保持する必要があり、これはあまり「可能」ではないため、これをプレイブックに入れます。

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

で実行します-vvが、出力には辞書の内容が基本的に表示され、改行はそのように出力されないため、次のような読み取り不能な混乱が発生します。

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 を使用して出力をフォーマットしますか?出力をホスト上のファイルにリダイレクトし、そのファイルを取得して画面にエコーしますか?)、シェル/ ansible の知識が限られているため、試すだけの日。

4

7 に答える 7

102

この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: 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"
    ]
}
于 2014-07-18T21:43:50.943 に答える
18

これは始まりかもしれません:

- 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ここでカバーされています: (「変数の登録」の章)

于 2013-12-13T11:00:27.903 に答える
11

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
于 2014-03-08T03:06:39.497 に答える
2

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

STDOUT を使用するタスクには、次のセクションがあります。

STDOUT:

What ever was in STDOUT
于 2018-03-09T15:30:33.263 に答える
-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 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,
$ 

言うまでもなく、そのままでは機能しません。適切に更新する必要があります。

于 2015-03-27T18:05:19.693 に答える