Qui*_*etz 225 debugging stderr stdout ansible
如何查看 ansible-playbook 命令的标准输出?-v 只显示 ansible 输出,而不是单个命令。如果我能立即弄清楚如何执行此操作,那就太好了,因此如果出现故障或挂起,我可以了解原因。
例如
- name: print to stdout
action: command echo "hello"
Run Code Online (Sandbox Code Playgroud)
会打印
TASK: [print variable] ********************************************************
hello
Run Code Online (Sandbox Code Playgroud)
小智 218
我认为您可以将结果注册到变量,然后使用调试进行打印。
- name: print to stdout
command: echo "hello"
register: hello
- debug: msg="{{ hello.stdout }}"
- debug: msg="{{ hello.stderr }}"
Run Code Online (Sandbox Code Playgroud)
小智 135
我建议使用stdout_lines而不是stdout。对于多行输出,这更好,例如
- hosts: all
tasks:
- name: Run ls.sh and output "ls /"
script: ls.sh
register: out
- debug: var=out.stdout_lines
Run Code Online (Sandbox Code Playgroud)
给
TASK: [debug var=out.stdout_lines] ********************************************
ok: [local] => {
"var": {
"out.stdout_lines": [
"total 61",
"lrwxrwxrwx 1 root root 7 Feb 15 2015 bin -> usr/bin",
"drwxr-xr-x 6 root root 1024 Aug 24 22:08 boot",
"drwxr-xr-x 22 root root 3580 Sep 8 18:41 dev",
[...]
"drwxr-xr-x 9 root root 4096 Aug 25 19:14 usr",
"drwxr-xr-x 13 root root 4096 Feb 25 2015 var"
]
}
}
Run Code Online (Sandbox Code Playgroud)
关于用于调试目的的实时输出,有一个关闭的错误报告 https://github.com/ansible/ansible/issues/3887#issuecomment-54672569 讨论了为什么这是不可能的并且不会实施的原因。
Jas*_*n S 25
我发现,用最少的 stdout_callback
有ansible-剧本给了相似的输出使用特设ansible。
在您的 ansible.cfg 中(请注意,我使用的是 OS X,因此请修改callback_plugins
路径以适合您的安装)
stdout_callback = minimal
callback_plugins = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/plugins/callback
Run Code Online (Sandbox Code Playgroud)
所以像这样的任务
---
- hosts: example
tasks:
- name: Say hi
command: echo "hi ..."
Run Code Online (Sandbox Code Playgroud)
给出这样的输出,就像临时命令一样
example | SUCCESS | rc=0 >>
hi ...
Run Code Online (Sandbox Code Playgroud)
我正在使用 ansible-playbook 2.2.1.0
如果你真的想实时观看输出,有一种方法可以解决它,至少对于 ansibleshell
模块是这样。
在任何 shell 脚本中包装您对 ansible 的调用,在后台作业中触摸和跟踪日志文件。然后重定向 ansible shell 命令的输出以附加到该日志文件。您需要确保在 ansible 完成后终止后台 tail 作业,否则它将悬而未决。
例如,在调用 ansible 的 bash 脚本中:
set -m
touch /tmp/debug.log && tail -f /tmp/debug.log &
ansible-playbook ... call playbook here
kill %1 # ensure the background tail job is stopped
Run Code Online (Sandbox Code Playgroud)
然后在一些 ansible 角色:
- name: Run a script and print stdout/stderr
shell: bash -c "/run/something.sh 2>&1 >> /tmp/debug.log"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
431551 次 |
最近记录: |