最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ansible - Redirecting selected logs - Stack Overflow

programmeradmin0浏览0评论

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.

Share Improve this question edited Feb 4 at 10:39 U880D 12.1k6 gold badges33 silver badges63 bronze badges asked Feb 4 at 4:43 ArjunArjun 6,73910 gold badges35 silver badges37 bronze badges 4
  • Have you tried adding log_file in ansible.cfg like log_path = /path/to/ansible.log ? – Trusha Jadeja Commented Feb 4 at 5:06
  • With log_file, ALL logs are going to console as well. I want only selective messages to console, and all logs to console. I don't want to use no_log for each task, as there are 1000+ tasks. – Arjun Commented Feb 4 at 5:14
  • Have you tried using verbosity to control what appears in the console? – Trusha Jadeja Commented Feb 4 at 5:42
  • This question is similar to: How to print debug messages to stdout, but not into log file, in Ansible when log_path is set?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – U880D Commented Feb 4 at 6:11
Add a comment  | 

1 Answer 1

Reset to default 1

Q: "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.

发布评论

评论列表(0)

  1. 暂无评论