this is the technic i am using to trap log message of playbook:
configure ansible.cfg and add log-path option:
[defaults]
inventory = hosts.yml
log_path = logs/logging.log
sample of playbook:
pre_tasks:
- name: launchsetup
setup:
filter:
- ansible_play_*
- set_fact:
log: "{{ lookup('file', 'logs/logging.log').split('\n') }}"
- debug:
var: log
sample log:
ok: [ansible-cible-2] => {
"log": [
"2021-11-16 13:35:25,598 p=6163 u=formation-ansible n=ansible | PLAY [all] *********************************************************************************************************************************************************************************",
"2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | TASK [launchsetup] *************************************************************************************************************************************************************************",
"2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021 13:35:25 +0000 (0:00:00.010) 0:00:00.010 ****** ",
"2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | [WARNING]: The value \"['ansible_play_*']\" (type list) was converted to \"u\"['ansible_play_*']\"\" (type string). If this does not look like what you expect, quote the entire value to ensure",
"it does not change.",
"",
"2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-3]",
"2021-11-16 13:35:26,341 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-2]",
"2021-11-16 13:35:28,688 p=6163 u=formation-ansible n=ansible | fatal: [ansible-cible-1]: UNREACHABLE! => {\"changed\": false, \"msg\": \"Failed to connect to the host via ssh: ssh: connect to host 172.17.10.101 port 22: No route to host\", \"unreachable\": true}",
"2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | TASK [set_fact] ****************************************************************************************************************************************************************************",
"2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021 13:35:28 +0000 (0:00:03.088) 0:00:03.098 ****** ",
"2021-11-16 13:35:28,742 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-3]"
]
}
ok: [ansible-cible-3] => {
"log": [
"2021-11-16 13:35:25,598 p=6163 u=formation-ansible n=ansible | PLAY [all] *********************************************************************************************************************************************************************************",
"2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | TASK [launchsetup] *************************************************************************************************************************************************************************",
"2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021 13:35:25 +0000 (0:00:00.010) 0:00:00.010 ****** ",
"2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | [WARNING]: The value \"['ansible_play_*']\" (type list) was converted to \"u\"['ansible_play_*']\"\" (type string). If this does not look like what you expect, quote the entire value to ensure",
"it does not change.",
"",
"2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-3]",
"2021-11-16 13:35:26,341 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-2]",
"2021-11-16 13:35:28,688 p=6163 u=formation-ansible n=ansible | fatal: [ansible-cible-1]: UNREACHABLE! => {\"changed\": false, \"msg\": \"Failed to connect to the host via ssh: ssh: connect to host 172.17.10.101 port 22: No route to host\", \"unreachable\": true}",
"2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | TASK [set_fact] ****************************************************************************************************************************************************************************",
"2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021 13:35:28 +0000 (0:00:03.088) 0:00:03.098 ******"
]
}
so now you have a variable log which contains the log after the first task where you could search anything in it...
as you could see in my log i have a fatal error on ubreachable and i have a named task launchsetup...
dont forget to delete log file before to launch playbook, it seems to append each time..