Ansible

shell/command 模組不提供來自樹莓派上 playbook 的輸出

  • October 21, 2021

使用 Ubuntu 的 Ansible 2.11.6。我有兩個樹莓派inventory。一個是帶有 Stretch 的舊 Pi3;另一個是帶有 Buster 的 Pi4。

$ ansible -i inventory -m shell -a "df -h" all

df -h正如預期的那樣,顯示了兩個 Pis的結果。但

$ ansible-playbook -i inventory main.yml

main.yml在哪裡

---
- hosts: all
 tasks:
   - name: show disk space
     shell:
       cmd: df -h

收集事實後,展示

TASK [show disk space] *******************
changed: [pi4]
changed: [pi3]

沒有顯示的輸出df -h

我試過command:而不是shell:. 我試過了executable: /bin/bash。沒有不同。在建構 Ubuntu VM 時,同樣main.yml適用於 Vagrant ansible Provisioner,並顯示df -h輸出。

我錯過了什麼?

更新這是我的錯誤。我一直使用的 Vagrantfile 模板包含ansible.verbose = true. 刪除它,一切都會保持一致。

default預設情況下, stdout 回調不顯示任務結果。Ad hoc 命令通常使用minimal具有不同行為的回調。如果您在 Vagrant 輸出中看到它,它要麼使用不同的回調,要麼以更高的詳細程度執行,其中顯示了更多資訊:

ec2-user@pandora ~ $ ansible-playbook test.yml 

PLAY [localhost] ***************************************************************

TASK [command] *****************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ec2-user@pandora ~ $ ANSIBLE_STDOUT_CALLBACK=minimal ansible-playbook test.yml
localhost | CHANGED | rc=0 >>
Filesystem                                 Size  Used Avail Use% Mounted on
devtmpfs                                   3.9G     0  3.9G   0% /dev
tmpfs                                      3.9G  272K  3.9G   1% /dev/shm
tmpfs                                      3.9G  8.9M  3.9G   1% /run
tmpfs                                      3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/nvme0n1p1                              12G  7.3G  4.8G  61% /
/dev/nvme1n1                                40G  9.0G   32G  23% /home
fs-c2c955bb.efs.us-east-2.amazonaws.com:/  8.0E     0  8.0E   0% /ammit
tmpfs                                      788M     0  788M   0% /run/user/1913323
tmpfs                                      788M     0  788M   0% /run/user/257994
tmpfs                                      788M     0  788M   0% /run/user/1000
ec2-user@pandora ~ $ ansible-playbook test.yml -vvv
ansible-playbook [core 2.11.5] 
 config file = /home/ec2-user/ansible-aws/ansible/ansible.cfg
 configured module search path = ['/home/ec2-user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
 ansible python module location = /home/ec2-user/ansible-aws/.venv/lib/python3.8/site-packages/ansible
 ansible collection location = /home/ec2-user/ansible-aws/ansible/collections
 executable location = /home/ec2-user/ansible-aws/.venv/bin/ansible-playbook
 python version = 3.8.5 (default, Feb 18 2021, 01:24:20) [GCC 7.3.1 20180712 (Red Hat 7.3.1-12)]
 jinja version = 3.0.2
 libyaml = True
Using /home/ec2-user/ansible-aws/ansible/ansible.cfg as config file
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: test.yml *************************************************************
1 plays in test.yml

PLAY [localhost] ***************************************************************
META: ran handlers

TASK [command] *****************************************************************
task path: /home/ec2-user/test.yml:4
Using module file /home/ec2-user/ansible-aws/.venv/lib/python3.8/site-packages/ansible/modules/command.py
Pipelining is enabled.
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ec2-user
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n  -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-gljjnrdznzzibrxampvyyzigtjnozzra ; /home/ec2-user/ansible-aws/.venv/bin/python'"'"' && sleep 0'
changed: [localhost] => {
   "changed": true,
   "cmd": [
       "df",
       "-h"
   ],
   "delta": "0:00:00.004672",
   "end": "2021-10-20 12:22:07.355248",
   "invocation": {
       "module_args": {
           "_raw_params": "df -h",
           "_uses_shell": false,
           "argv": null,
           "chdir": null,
           "creates": null,
           "executable": null,
           "removes": null,
           "stdin": null,
           "stdin_add_newline": true,
           "strip_empty_ends": true,
           "warn": false
       }
   },
   "msg": "",
   "rc": 0,
   "start": "2021-10-20 12:22:07.350576",
   "stderr": "",
   "stderr_lines": [],
   "stdout": "Filesystem                                 Size  Used Avail Use% Mounted on\ndevtmpfs                                   3.9G     0  3.9G   0% /dev\ntmpfs                                      3.9G  124K  3.9G   1% /dev/shm\$
tmpfs                                      3.9G  8.8M  3.9G   1% /run\ntmpfs                                      3.9G     0  3.9G   0% /sys/fs/cgroup\n/dev/nvme0n1p1                              12G  7.3G  4.8G  61% /\n/dev/nvme1n1      
                         40G  9.0G   32G  23% /home\nfs-c2c955bb.efs.us-east-2.amazonaws.com:/  8.0E     0  8.0E   0% /ammit\ntmpfs                                      788M     0  788M   0% /run/user/1913323\ntmpfs                      
               788M     0  788M   0% /run/user/257994",
   "stdout_lines": [
       "Filesystem                                 Size  Used Avail Use% Mounted on",
       "devtmpfs                                   3.9G     0  3.9G   0% /dev",
       "tmpfs                                      3.9G  124K  3.9G   1% /dev/shm",
       "tmpfs                                      3.9G  8.8M  3.9G   1% /run",
       "tmpfs                                      3.9G     0  3.9G   0% /sys/fs/cgroup",
       "/dev/nvme0n1p1                              12G  7.3G  4.8G  61% /",
       "/dev/nvme1n1                                40G  9.0G   32G  23% /home",
       "fs-c2c955bb.efs.us-east-2.amazonaws.com:/  8.0E     0  8.0E   0% /ammit",
       "tmpfs                                      788M     0  788M   0% /run/user/1913323",
       "tmpfs                                      788M     0  788M   0% /run/user/257994"
   ]
}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

您還可以使用以下方式顯式輸出內容debug

---
- hosts: all
 tasks:
 - name: get disk space
   command:
     cmd: df -h
   register: result

 - debug:
     msg: "{{ result.stdout_lines }}"
ec2-user@pandora ~ $ ansible-playbook test.yml 

PLAY [localhost] ***************************************************************

TASK [get disk space] **********************************************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
   "msg": [
       "Filesystem                                 Size  Used Avail Use% Mounted on",
       "devtmpfs                                   3.9G     0  3.9G   0% /dev",
       "tmpfs                                      3.9G  124K  3.9G   1% /dev/shm",
       "tmpfs                                      3.9G  8.8M  3.9G   1% /run",
       "tmpfs                                      3.9G     0  3.9G   0% /sys/fs/cgroup",
       "/dev/nvme0n1p1                              12G  7.3G  4.8G  61% /",
       "/dev/nvme1n1                                40G  9.0G   32G  23% /home",
       "fs-c2c955bb.efs.us-east-2.amazonaws.com:/  8.0E     0  8.0E   0% /ammit",
       "tmpfs                                      788M     0  788M   0% /run/user/1913323",
       "tmpfs                                      788M     0  788M   0% /run/user/257994"
   ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

引用自:https://serverfault.com/questions/1081085