Ansible playbook не показывает вывод из определенного канала оболочки - PullRequest
0 голосов
/ 21 марта 2020

Выполнение следующих команд из 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)

1 Ответ

0 голосов
/ 21 марта 2020

Оболочка по умолчанию /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"
    ]
...