使用 Ubuntu 中的 Ansible 2.11.6。我有两个 Raspberry Pi inventory
。一个是带有 Stretch 的旧款 Pi3;另一个是带有 Buster 的 Pi4。
$ ansible -i inventory -m shell -a "df -h" all
df -h
显示了两个 Pi的结果,正如预期的那样。但是
$ 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
。没有区别。main.yml
在构建 Ubuntu VM 时,同样的方法可以与 Vagrant ansible provisioner 配合使用,并显示df -h
输出。
我错过了什么?
更新这是我的错误。我一直使用的 Vagrantfile 模板包含ansible.verbose = true
。删除它,一切行为都会一致。
答案1
在默认详细程度下,stdout 回调不会显示任务结果default
。临时命令通常使用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
答案2
您缺少显示输出的调试任务。
---
- hosts: all
tasks:
- name: show disk space
shell:
cmd: df -h
register: df
- debug:
var: df
Ansible 不直接显示命令的输出。