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
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\"",
]
}
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\"",
]
}
It's possible to select only changed tasks by using the callback actionable. See "ansible-doc -t callback actionable"
"Use this callback when you don't care about OK nor Skipped. This callback suppresses any non Failed or Changed status."
For example, the playbook below
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
gives
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
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
Q: "How can I execute my playbook using 'default' instead of 'actionable'?"
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"
Thank you. I was able to get the desired output using the steps you suggested. However I checked the ansible doc and it says actionable is deprecated and it says we need to use "default". How can i execute my playbook using "default" instead of "actionable".
You're welcome. It's deprecated. You're right. I still keep it using for simplicity. Configure the callback either in the configuration files or in the environment. I've added an example of how to configure the environment.
The "selective" callback looks promising. See
ansible-doc -t callback selective
. Unfortunately, the formatting is not working properly atm.