首页 文章

Ansible playbook shell输出

提问于
浏览
40

我想使用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]

然而,这需要我为每个不太“安全”的任务保留一堆shell脚本,所以我把它放在一个剧本中:

---
-
  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 和a 'debug'任务来显示 {{ var.stdout }} 但结果当然是一样的 .

有没有办法在通过playbook运行时从命令的stdout / stderr获得格式良好的输出?我可以想到一些可能的方法(使用sed格式输出?重定向输出到主机上的文件,然后将该文件返回并将其回显到屏幕?),但由于我对shell / ansible的了解有限,我需要一天来尝试一下 .

7 回答

  • 7

    扩展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
    
  • 59

    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"
        ]
    }
    
  • 0

    这可能是一个开始:

    - 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
    

    NOTE: 关于 ps.stdout_lines 的文档包含在这里:('Register Variables' chapter) .

  • 14

    我发现使用带有ansible-playbook的minimal stdout_callback 给出了与使用ad-hoc 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
    

    给出这样的输出,就像ad-hoc命令一样

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

    我正在使用ansible-playbook 2.2.1.0

  • 2

    如果您需要特定的退出状态,Ansible提供了一种通过 callback plugins 执行此操作的方法 .

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

    如果没有,您可以随时使用Debug Module,这是此使用情况的标准 .

    干杯

  • -1

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

    然后STDOUT的任务将有一个部分:

    STDOUT:
    
    What ever was in STDOUT
    
  • 2

    也许不相关如果你更容易在我的 .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,
    $
    

    不用说功能对你不起作用 . 您需要适当更新它 .

相关问题