Is it possible to selectively redirect some of the console logs to file?
E.g.,
- name: "Installing service1" // This msg should go to CONSOLE and file.
host: localhost
tasks:
- name: Performing imp operation // This msg should go to CONSOLE and file.
# ...
- name: print var // This msg should go to file
debug:
msg: var={{ my_var }} // THis msg should go to file
With log_file
, all logs are going to console as well. I want only selective messages to console, and all logs to file. I don't want to use no_log
for each task, as there are 1000+ tasks.
Is it possible to selectively redirect some of the console logs to file?
E.g.,
- name: "Installing service1" // This msg should go to CONSOLE and file.
host: localhost
tasks:
- name: Performing imp operation // This msg should go to CONSOLE and file.
# ...
- name: print var // This msg should go to file
debug:
msg: var={{ my_var }} // THis msg should go to file
With log_file
, all logs are going to console as well. I want only selective messages to console, and all logs to file. I don't want to use no_log
for each task, as there are 1000+ tasks.
1 Answer
Reset to default 1Q: "Is it possible to selectively redirect some of the console logs to file? Appreciate any pointers."
A: Yes, of course, but it will involve some development effort. I can't provide a full working solution yet, instead some guidance and options which might be helpful.
Your question seems to be similar to How to print debug messages to stdout
, but not into log file, in Ansible when log_path
is set?, but you'll need to implement a kind of opposite behavior.
The module in question here is ansible/plugins/action/debug.py
. A quick and fast approach could be to change the behavior of the Action Plugin at
# get task verbosity
verbosity = int(self._task.args.get('verbosity', 0))
from 0
to 1
.
That would result into a debug
task which is skipped
if the playbook wasn't run via -v
, but also in a log result of
Feb 04 2025 08:45:00 - log_plays.yml - Debug task - debug - SKIPPED - {"skipped_reason": "Verbosity threshold not met.", "_ansible_no_log": false, "changed": false}
instead of
Feb 04 2025 08:46:00 - log_plays.yml - Debug task - debug - OK - {"msg": "Debug output", "_ansible_verbose_always": true, "_ansible_no_log": false, "changed": false}
Another option to look at would be to evaluate the check_mode
attribute within the debug
module, in example to change the behavior that output is only done when --check
mode was set during playbook run.
# get task verbosity
verbosity = int(self._task.args.get('verbosity', 0))
if verbosity <= self._display.verbosity and self._task.check_mode:
This simple conditional will add check mode support to the Action Plugin debug
and would result into a debug
task which is skipped
if the playbook wasn't run in --check
mode, but also in the above log result.
log_file
inansible.cfg
likelog_path = /path/to/ansible.log
? – Trusha Jadeja Commented Feb 4 at 5:06verbosity
to control what appears in the console? – Trusha Jadeja Commented Feb 4 at 5:42