【问题标题】:Ansible playbook does not show output from a certain shell pipeAnsible playbook 不显示来自某个 shell 管道的输出
【发布时间】:2020-07-02 02:37:12
【问题描述】:

从 bash 运行以下命令,会产生以下输出:

$ sudo dmidecode --type 17 |& egrep -iH 'Type|Speed|Size|Part Number' | grep -v 'DMI type 17' | sort -u
(standard input):       Configured Memory Speed: 1333 MT/s
(standard input):       Part Number: CMZ16GX3M2A1600C9 
(standard input):       Size: 8192 MB
(standard input):       Speed: 1333 MT/s
(standard input):       Type: DDR3
(standard input):       Type Detail: Synchronous

但是,当我尝试从以下 ansible-playbook 运行它时:

$ cat stackoverflow.yml 
---

- name: get memory inf on our servers
  hosts: servers_memory
  remote_user: qa
  become_user: root
  become: yes
  become_method: sudo

  tasks:
  - name: test connection
    ping:

  - name: get memory size
    register: memory_size
    shell: |
      set timeout 300
      free -h | egrep 'total|Mem:'

  - debug: var=memory_size.stdout_lines

  - name: get memory data
    register: memory_type
    shell: |
      set timeout 300
      dmidecode --type 17 |& egrep -iH 'Type|Speed|Size|Part Number' | grep -v 'DMI type 17' | sort -u

#  - debug: var=memory_type.stdout_lines
  - debug: msg={{ memory_type }}

没有为TASK [get memory data]捕获输出:

$ ansible-playbook  -i /etc/ansible/hosts stackoverflow.yml --ask-pass --ask-become-pass
SSH password: 
BECOME password[defaults to SSH password]: 

PLAY [get memory inf on our servers] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [drf-vb2.local]

TASK [test connection] ********************************************************************************************************************************************************************************************
ok: [drf-vb2.local]

TASK [get memory size] ********************************************************************************************************************************************************************************************
changed: [drf-vb2.local]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [drf-vb2.local] => {
    "memory_size.stdout_lines": [
        "              total        used        free      shared  buff/cache   available", 
        "Mem:            22G        1.3G        231M        545M         21G         20G"
    ]
}

TASK [get memory data] ********************************************************************************************************************************************************************************************
changed: [drf-vb2.local]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [drf-vb2.local] => {
    "msg": {
        "changed": true, 
        "cmd": "set timeout 300\ndmidecode --type 17 |& egrep -iH 'Type|Speed|Size|Part Number' | grep -v 'DMI type 17' | sort -u\n", 
        "delta": "0:00:00.005970", 
        "end": "2020-03-20 16:24:42.965191", 
        "failed": false, 
        "rc": 0, 
        "start": "2020-03-20 16:24:42.959221", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "", 
        "stdout_lines": []
    }
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
drf-vb2.local              : ok=6    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

你能指出我的错误吗?

(操作系统为 CentOS 7.6)

【问题讨论】:

    标签: bash ansible centos centos7


    【解决方案1】:

    默认 shell /bin/sh 无法识别隐式 stderr 重定向 |&(在 Bash4 中添加)。它应该报告

    "stderr": "/bin/sh: 1: Syntax error: \"&\" unexpected"
    

    我不确定您为什么看不到此错误。无论如何,bash 按预期工作。例如

        - name: get memory data
          register: memory_type
          shell:
            executable: /usr/bin/bash
            cmd: "set timeout 300;
                  dmidecode --type 17 |&
                  egrep -iH 'Type|Speed|Size|Part Number' |
                  grep -v 'DMI type 17' |
                  sort -u"
    
        - debug:
            var: memory_type.stdout_lines
    

    给予

        "memory_type.stdout_lines": [
            "(standard input):\tConfigured Memory Speed: 1867 MT/s", 
            "(standard input):\tPart Number: K4E6E304EB-EGCF   ", 
            "(standard input):\tSize: 4096 MB", 
            "(standard input):\tSpeed: 1867 MT/s", 
            "(standard input):\tType Detail: Synchronous Unbuffered (Unregistered)", 
            "(standard input):\tType: LPDDR3"
        ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2016-04-05
      • 2013-12-09
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      相关资源
      最近更新 更多