如何使 Ansible 仅打印具有标准输出的任务 [英] How to make Ansible print only the task that has stdout
问题描述
我有一个包含多个任务的剧本.我需要 Ansible 只打印包含 STDOUT/debug 任务的任务
I have a playbook which contains multiple Tasks. I need Ansible to print only the Tasks which contains STDOUT /debug task
- hosts: "{{ v_host }}"
gather_facts: no
tasks:
- name: "Create /tmp in target in all VM's "
file:
path: /tmp
state: directory
owner: oracle
group: oinstall
mode: 0775
- copy:
src: /xxx
dest: /yyy
owner: oracle
group: oinstall
force: yes
mode: 0755
- name: "Performing {{ patch_action }} of CPU patch on {{ v_host }} "
shell: |
cd /tmp
sudo -u oracle ./patch.sh
register: out
- debug: var=out.stdout_lines
我在没有 -v 选项的情况下执行剧本以获得更清晰的输出.输出如下所示.
I execute the playbook without -v option to have a cleaner output. The output looks like below.
ansible-playbook test.yml --extra-vars v_host=xyz.com.
TASK [Create /tmp in target in all VM's] ********************************************************************
ok: [ixyz.com]
TASK [copy] ********************************************************************
ok: [xyz.com]
TASK [Performing of CPU patch on xyz.com] ***
changed: [xyz.com]
TASK [debug] *******************************************************************
ok: [xyz.com] => {
"out.stdout_lines": [
"Patch 123456 : applied on Tue Sep 29 10:11:58 GMT 2020",
"Patch description: \"one-off\"",
"Patch 987654 : applied on Tue Sep 29 10:08:39 GMT 2020",
"Patch description: \"One-off\"",
]
}
当我执行剧本时是否有一个选项,它只为我提供 stdout/debug 的输出.我需要的唯一输出是
Is there an option when i execute the playbook, it gives me the output only for the stdout/debug . The only output that i need is
TASK [debug] *******************************************************************
ok: [xyz.com] => {
"out.stdout_lines": [
"Patch 123456 : applied on Tue Sep 29 10:11:58 GMT 2020",
"Patch description: \"one-off\"",
"Patch 987654 : applied on Tue Sep 29 10:08:39 GMT 2020",
"Patch description: \"One-off\"",
]
}
推荐答案
可以使用回调 actionable 仅选择更改的任务.请参阅ansible-doc -t callback actionable"
It's possible to select only changed tasks by using the callback actionable. See "ansible-doc -t callback actionable"
"当您不关心 OK 或 Skipped 时使用此回调.此回调会抑制任何非失败"或已更改"状态."
例如下面的剧本
shell> cat pb.yml
- hosts: localhost
tasks:
- stat:
path: /etc/passwd
register: result
- debug:
var: result.stat.mode
- debug:
msg: test message
changed_when: actionable|default(false)|bool
给予
shell> ansible-playbook pb.yml
PLAY [localhost] ****
TASK [stat] ****
ok: [localhost]
TASK [debug] ****
ok: [localhost] => {
"result.stat.mode": "0644"
}
TASK [debug] ****
ok: [localhost] => {
"msg": "test message"
}
PLAY RECAP ****
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
<小时>
actionable 回调将抑制输出.例如
The actionable callback will suppress the output. For example
shell> ANSIBLE_STDOUT_CALLBACK=actionable ansible-playbook pb.yml
PLAY [localhost] ****
PLAY RECAP ****
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
<小时>
您可以通过控制所选任务的已更改状态来启用其输出.例如
You can enable the output of the selected tasks by controlling their changed status. For example
shell> ANSIBLE_STDOUT_CALLBACK=actionable ansible-playbook pb.yml -e "actionable=true"
PLAY [localhost] ****
TASK [debug] ****
changed: [localhost] => {
"msg": "test message"
}
PLAY RECAP ****
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
<小时>
问:如何使用默认"而不是可操作"来执行我的剧本?
A:下面的命令给出相同的结果
A: The command below gives the same results
shell> ANSIBLE_STDOUT_CALLBACK=default ANSIBLE_DISPLAY_SKIPPED_HOSTS=false ANSIBLE_DISPLAY_OK_HOSTS=false ansible-playbook pb.yml -e "actionable=true"
这篇关于如何使 Ansible 仅打印具有标准输出的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!