Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change ansible verbosity level without changing the command line arguments?

I want to control the verbosity of ansible playbooks using an environment variable or a global configuration item. This is because ansible is called from multiple places in multiple ways and I want to change the logging level for all further executions from the same shell session.

I observed that if I configure ANSIBLE_DEBUG=true ansible will run in debug mode but debug more is extremely verbose and I am only looking for something similar to the -vvv option (DEBUG is more verbose than even -vvvv option)

I tried to look for a variable inside https://github.com/ansible/ansible/blob/devel/lib/ansible/constants.py but I wasn't able to find one this fits the bill.

like image 313
sorin Avatar asked Dec 23 '22 20:12

sorin


1 Answers

I see two ways to do this:

alias

alias ansible-playbook="echo 'This is -vv alias'; ansible-playbook -vv"

This way your shell will call ansible-playbook -vv when you type ansible-playbook (and print friendly reminder about alias).

callback plugin

Drop this code as verbosity_env.py file into callback_plugins directory:

from ansible.plugins.callback import CallbackBase
import os

try:
    from __main__ import display
except ImportError:
    display = None

class CallbackModule(CallbackBase):

    def v2_playbook_on_start(self, playbook):
        v = os.environ.get('ANSIBLE_VERBOSITY')
        if v and display:
            display.display('Verbosity is set to {} with environment variable.'.format(v), color='blue')
            display.verbosity = int(v)

It is not production quality code, but does the job. This will check ANSIBLE_VERBOSITY environment variable and set display verbosity with its value.

like image 98
Konstantin Suvorov Avatar answered May 16 '23 08:05

Konstantin Suvorov