Reputation: 7770
I want to better debug my Ansible scripts and search for an option to print the role besides the already printed tasks name. I dig around but could not find any solution and therefore need help.
That's the output I currently have
### RUN ansible-playbook - install
[WARNING]: Not prompting as we are not in interactive mode
PLAY [Setup system] ************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [homebrew : Check if homebrew is Installed] *******************************
ok: [localhost]
That's the output I want
### RUN ansible-playbook - install
[WARNING]: Not prompting as we are not in interactive mode
PLAY [Setup system] ************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
ROLE [homebrew] <----- HERE IS THE ROLE
TASK [homebrew : Check if homebrew is Installed] *******************************
ok: [localhost]
Upvotes: 2
Views: 1906
Reputation: 311606
Ansible doesn't have any option for displaying the role name like that when a role starts executing.
The output you see when running Ansible is controlled by a stdout callback plugin. These plugins don't receive any notification about the start of a role, so it's tricky to get the output you want.
That said, it appears the that we can use the get_dep_chain
method on a task
object to figure out the name of the current role, and if we keep track of when that changes we can get something like what you're looking for.
Using the following as my stdout_plugin
(installed as callback_plugins/roller.py
adjacent to my playbook):
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.callback.default import CallbackModule as DefaultCallbackModule
from ansible import constants as C
current_role = None
class CallbackModule(DefaultCallbackModule):
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'stdout'
CALLBACK_NAME = 'roller'
def v2_playbook_on_task_start(self, task, is_conditional):
global current_role
deps = task.get_dep_chain()
if deps:
role = deps[-1]
if role != current_role:
print('ROLE: [{}]'.format(role))
current_role = role
super().v2_playbook_on_task_start(task, is_conditional)
And a playbook that includes a role (which itself includes another role), I get output like this:
PLAY [localhost] *****************************************************************************
TASK [include_role : role1] ******************************************************************
ROLE: [role1]
TASK [role1 : sample task] *******************************************************************
ok: [localhost] => {
"msg": "This is a task in role1"
}
TASK [include role2] *************************************************************************
ROLE: [role2]
TASK [role2 : sample task] *******************************************************************
ok: [localhost] => {
"msg": "This is a task in role2"
}
PLAY RECAP ***********************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
While the above works, I wouldn't suggest using it in practice: I don't think you really gain all that much, since Ansible does display the current role name appended to the task name.
Upvotes: 2