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

<pre><code>    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')
</code></pre>

<p>Now the output looks like:</p>

<pre><code>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]
</code></pre>

<p>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 <code>block</code> is that it is impossible to do what I wanted, due to the way the block class is implemented in Ansible.</p>

CC BY-SA 4.0
2023-02-25T13:45:59.880Z
Thank you for this answer. It works. When you said you started with the default callback code, I assume you meant [default.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py). I found that I also needed to add the following line:

`self._current_role = None`


...just before the `super(CallbackModule, self).__init__()` line in the [__init__](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py#L42-L48) function.
Jinesh Choksi
CC BY-SA 4.0
2019-04-19T02:33:04.040Z
2019-04-19T02:33:04.040Z
Ryan O.
<p><a href="https://docs.ansible.com/ansible/latest/modules/include_role_module.html#include-role-load-and-execute-a-role" rel="nofollow noreferrer">include_role</a> is a task and the name is printed (see below). <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html#blocks" rel="nofollow noreferrer">block</a> is not a task and name is not printed by any <a href="https://docs.ansible.com/ansible/latest/plugins/callback.html#plugin-list" rel="nofollow noreferrer">plugin</a>.</p>

<p>The play</p>

<pre><code>tasks:
  - name: "I'm a block!"
    block:
      - name: "I'm a role!"
        include_role: name=role5
</code></pre>

<p>gives:</p>

<pre><code>PLAY [localhost]
TASK [I'm a role!]
TASK [role5 : I'm a task!]
</code></pre>

CC BY-SA 4.0
2019-04-18T19:34:10.380Z
You're right, it does normally, but I had ``display_skipped_hosts = no`` set, which apparently causes the default callback to skip printing role names. Is there a way to achieve what I want with a custom callback plugin? I've looked over the API but didn't see anything that would let me do so.
Ryan O.
CC BY-SA 4.0
2019-04-18T19:37:36.483Z
Unfortunately, neither did I. I have no experience with custom callback, sorry. FWIW here is the [Developer Guide](https://docs.ansible.com/ansible/latest/dev_guide/#developer-guide).
Vladimir Botka
CC BY-SA 4.0
2019-04-18T19:28:34.143Z
2019-04-18T19:28:34.143Z
Vladimir Botka
<p>I figured this would be trivial, but can't find the answer anywhere.</p>

<p>Let's say I have a playbook.yml that looks like:</p>

<pre><code>- hosts: all

  tasks:
  - name: I'm a block!
    block:
    - name: I'm a role!
      include_role: name=some_role
</code></pre>

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

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

<p>What I would expect / like to be printed is:</p>

<pre><code>BLOCK [I'm a block!] *********
ROLE [I'm a role!] *********
TASK [some_role : I'm a task!] *********
ok: [MACHINE]
</code></pre>

<p>Or something like that. Instead, what gets printed is:</p>

<pre><code>TASK [some_role : I'm a task!] *********
ok: [MACHINE]
</code></pre>

<p>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.</p>

<ansible>
Get Ansible to print lines for include_role(s)
Ryan O.
# Get Ansible to print lines for include_role(s)

(Asked by: username_0 on 2019-04-18)

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.

**Tags:** ansible


---

## 2 answers

### Answer by username_0 on 2019-04-19. Score: 3

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.

> **Comments:**
> **username_1**: Thank you for this answer. It works. When you said you started with the default callback code, I assume you meant [default.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py). I found that I also needed to add the following line:  `self._current_role = None`   ...just before the `super(CallbackModule, self).__init__()` line in the [__init__](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/default.py#L42-L48) function.  

---

### Answer by username_2 on 2019-04-18. Score: 0

include_role is a task and the name is printed (see below). block is not a task and name is not printed by any plugin.
The play
tasks:
  - name: "I'm a block!"
    block:
      - name: "I'm a role!"
        include_role: name=role5

gives:
PLAY [localhost]
TASK [I'm a role!]
TASK [role5 : I'm a task!]

> **Comments:**
> **username_0**: You're right, it does normally, but I had ``display_skipped_hosts = no`` set, which apparently causes the default callback to skip printing role names. Is there a way to achieve what I want with a custom callback plugin? I've looked over the API but didn't see anything that would let me do so.  
> **username_2**: Unfortunately, neither did I. I have no experience with custom callback, sorry. FWIW here is the [Developer Guide](https://docs.ansible.com/ansible/latest/dev_guide/#developer-guide).  

---

