我有一本包含多个任务的剧本。我需要Ansible仅打印包含STDOUT / debug任务的任务
- 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选项的剧本以获得更清晰的输出。输出如下所示。
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提供输出。我唯一需要的输出是
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可操作的回调”
“当你不关心“确定”或“跳过”时,请使用此回调。此回调可抑制任何非“失败”或“已更改”状态。
例如,下面的剧本
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
该可操作的回调将抑制输出。例如
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
你可以通过控制其更改的状态来启用所选任务的输出。例如
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
问:“如何使用“默认”而不是“可操作”执行我的剧本? ”
答:下面的命令给出相同的结果
shell> ANSIBLE_STDOUT_CALLBACK=default ANSIBLE_DISPLAY_SKIPPED_HOSTS=false ANSIBLE_DISPLAY_OK_HOSTS=false ansible-playbook pb.yml -e "actionable=true"
谢谢你。使用您建议的步骤,我能够获得所需的输出。但是,我检查了ansible文档,并说已弃用了actionable,并说我们需要使用“ default”。如何使用“默认”而不是“可操作”执行我的剧本。
别客气。不推荐使用。你是对的。为了简单起见,我仍然使用它。在配置文件或环境中配置回调。我添加了一个有关如何配置环境的示例。
“选择性”回调看起来很有希望。请参阅
ansible-doc -t callback selective
。不幸的是,格式化不能在atm上正常工作。