大网数据广告活动
服务器运维问答

shell/command modules not providing output from playbook on Raspberry Pis

· 52CE 我爱测

Using Ansible 2.11.6 from Ubuntu. I have two Raspberry Pis in inventory . One is an older Pi3 with Stretch; the other a Pi4 with Buster. $ ansible -i inventory -m shell -a "df -h" all shows the result of df -h for both Pis, as ex…

问题原文

原帖链接 · Dave W. Smith · CC BY-SA 4.0

Using Ansible 2.11.6 from Ubuntu. I have two Raspberry Pis in inventory. One is an older Pi3 with Stretch; the other a Pi4 with Buster.

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

shows the result of df -h for both Pis, as expected. But

$ ansible-playbook -i inventory main.yml

where main.yml is

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

after gathering facts, shows

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

without showing the output of df -h.

I've tried command: instead of shell:. I've tried executable: /bin/bash. No difference. The same main.yml works with a Vagrant ansible provisioner when building an Ubuntu VM, and shows df -h output.

What am I missing?

Update This was a mistake on my end. The Vagrantfile template I'd been using included ansible.verbose = true. Remove that, and everything behaves consistently.

已采纳解答

原帖链接 · flowerysong · CC BY-SA 4.0

At the default verbosity task results are not shown by the default stdout callback. Ad hoc commands normally use the minimal callback, which has different behaviour. If you are seeing it in the Vagrant output it is either using a different callback or running with a higher verbosity, where more information is displayed:

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   

You can also explicitly output things using 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   

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-10-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。