sorin
sorin

Reputation: 170390

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.

Upvotes: 10

Views: 25276

Answers (4)

sorin
sorin

Reputation: 170390

Not sure why I missed to answer this, since a long time ago ansible fully supports ANSIBLE_VERBOSITY=[0|1|2|3|4].

For reference, ansible documentation

Upvotes: 16

user2863715
user2863715

Reputation: 21

I can't find it documented anywhere other than sorin's answer, but if you set ANSIBLE_VERBOSITY=[0|1|2|3|4] environment variable, ansible-playbook will pick this up and use it, unless you specify the verbosity on the command line. E.g. in Unix-type shells:

export ANSIBLE_VERBOSITY=2
ansible-playbook my-playbook.yml

I only stumbled upon it because I tried setting ANSIBLE_VERBOSITY='-vv' in a pipeline and Ansible started moaning about it not being an integer!

Upvotes: 2

Mircea Vutcovici
Mircea Vutcovici

Reputation: 2374

You can create/edit ansible.cfg file in the local folder and add in section [defaults]:

[defaults]
verbosity=4

Alternatively you can add the same option in /etc/ansible/ansible.cfg file.

Upvotes: 2

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68239

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.

Upvotes: 9

Related Questions