HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Get Ansible to print lines for include_role(s)

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
include_roleprintgetforansiblelines

Problem

I figured this would be trivial, but can't find the answer anywhere.

Let's say I have a playbook.yml that looks like:

- hosts: all

  tasks:
  - name: I'm a block!
    block:
    - name: I'm a role!
      include_role: name=some_role


And then I have a main.yml in roles/some_role/tasks that looks like:

- name: I'm a task!
  git: repo=https://doesnexist.com/nope.git dest={{ install_path }}


What I would expect / like to be printed is:

BLOCK [I'm a block!] *********
ROLE [I'm a role!] *********
TASK [some_role : I'm a task!] *********
ok: [MACHINE]


Or something like that. Instead, what gets printed is:

TASK [some_role : I'm a task!] *********
ok: [MACHINE]


So why did I bother putting those names on the block and include_role? Is there any way to get what I want, or at least something close to it? The debug module doesn't do what I want, since it is very poorly formatted.

Solution

I was able to create a custom callback plugin. I started with the default callback code and modified it:

def v2_playbook_on_task_start(self, task, is_conditional):

        if(task._role is not None and task._role != self._current_role):
            self._current_role = task._role
            self._display.banner("ROLE [" + self._current_role.get_name() + "]", color=C.COLOR_DEBUG)

        self._task_start(task, prefix='TASK')


Now the output looks like:

ROLE [some_role] *****************************************************

TASK [some_role : I'm a task!] ***************************************
ok: [MACHINE]

TASK [some_role : Some other task!] **********************************
ok: [MACHINE]

ROLE [another_role] **************************************************

TASK [some_role : I'm a task!] ***************************************
ok: [MACHINE]


With the role line being bolded. It's a work in progress, but it at least makes it a lot more obvious where roles are being executed. My determination on block is that it is impossible to do what I wanted, due to the way the block class is implemented in Ansible.

Code Snippets

def v2_playbook_on_task_start(self, task, is_conditional):

        if(task._role is not None and task._role != self._current_role):
            self._current_role = task._role
            self._display.banner("ROLE [" + self._current_role.get_name() + "]", color=C.COLOR_DEBUG)

        self._task_start(task, prefix='TASK')
ROLE [some_role] *****************************************************

TASK [some_role : I'm a task!] ***************************************
ok: [MACHINE]

TASK [some_role : Some other task!] **********************************
ok: [MACHINE]

ROLE [another_role] **************************************************

TASK [some_role : I'm a task!] ***************************************
ok: [MACHINE]

Context

StackExchange DevOps Q#6899, answer score: 3

Revisions (0)

No revisions yet.