Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2023-02-25T13:45:59.880Z
Answers: 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.
Answers: Jinesh Choksi
Answers: CC BY-SA 4.0
Answers: 2019-04-19T02:33:04.040Z
Answers: 2019-04-19T02:33:04.040Z
Answers: Ryan O.
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2019-04-18T19:34:10.380Z
Answers: 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.
Answers: Ryan O.
Answers: CC BY-SA 4.0
Answers: 2019-04-18T19:37:36.483Z
Answers: 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).
Answers: Vladimir Botka
Answers: CC BY-SA 4.0
Answers: 2019-04-18T19:28:34.143Z
Answers: 2019-04-18T19:28:34.143Z
Answers: Vladimir Botka
Body: <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>
  
Tags: <ansible>
Title: Get Ansible to print lines for include_role(s)
UserDisplayName: Ryan O.
ThreadText: # 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](https://docs.ansible.com/ansible/latest/modules/include_role_module.html#include-role-load-and-execute-a-role) is a task and the name is printed (see below). [block](https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html#blocks) is not a task and name is not printed by any [plugin](https://docs.ansible.com/ansible/latest/plugins/callback.html#plugin-list).
  
  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).  
  
  ---
  
  