Answers: <p>Not tested this, but is <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_role_module.html" rel="nofollow noreferrer">include_role</a> any help? so instead of a playbook like this,</p>
  <pre><code> ---
   - name: Example
     hosts: all
     roles:
      - first_role
      - second_role
      - third_role
  </code></pre>
  <p>maybe this,</p>
  <pre><code> ---
   - name: Example
     hosts: all
     tasks:
      - etckeeper_task
      - include_role:
          name: first_role
      - etckeeper_task
      - include_role:
          name: second_role
      - etckeeper_task
      - include_role:
          name: third_role
      - etckeeper_task
  </code></pre>
  <p>alternatively (also untested) you could build a role, let's call it <code>wrap_role</code> with <code>tasks/main.yml</code> containing,</p>
  <pre><code>---
  # Replace this task with your actual before-role etckeeper task,
  - name: etckeeper pre-actions
    meta: noop
  
  - include_role:
    name: {{role_name}}
  
  # Replace this task with your actual after-role etckeeper task,
  - name: etckeeper post-actions
    meta: noop
  </code></pre>
  <p>Now your top-level playbook can be something like,</p>
  <pre><code> ---
   - name: Example
     hosts: all
     roles:
       - { role: wrap_role, role_name: first_role }
       - { role: wrap_role, role_name: second_role }
       - { role: wrap_role, role_name: third_role }
  </code></pre>
  <p>not ideal based on your question, but at least it leaves the roles untouched, even if you have to alter top-level playbooks. If I get the time I'll test the above suggestions and update this answer accordingly.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-09-30T08:34:31.823Z
Answers: Thank you for your suggestions, but as you have noted this does not solve my problem.
Answers: random_dude
Answers: CC BY-SA 4.0
Answers: 2020-09-28T21:39:36.010Z
Answers: 2020-09-28T21:39:36.010Z
Answers: Andrew Richards
Body: <p>I am looking for a way to execute some actions before and after each role has run.
  <code>pre_tasks</code> and <code>post_tasks</code> look like a good candidate, but I have found no way to include them transparently for all defined roles/playbooks. I don't want to add these manually to all playbooks since this seems like a maintenance nightmare.</p>
  <p>Another angle to solve this might be to abuse the fact that Ansible logs in via ssh. However I am not aware of any identifying properties of an Ansible login as opposed to a regular user login.</p>
  <p>My specific issue is:
  I want to trigger <a href="http://etckeeper.branchable.com/" rel="nofollow noreferrer">etckeeper</a> before and after each execution of a role.</p>
  <p>If you have any other &quot;out-of-the-box&quot; ideas to solve this, I will be grateful to hear them.</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-10-29T13:48:34.567Z
Comments: Long shoot (and this should be a comment really). Create another role with a task for the pre and a handler for the post than add the role as a dependency (in meta/main.yml) to the other roles. The dependencies are executed only once and with any luck the handlers will be executed at the end. It’s not exactly what you’re looking for but it’s close.
Comments: boca
Tags: <ansible>
Title: Include pre- and post_tasks transparently for all Ansible roles
UserDisplayName: random_dude
ThreadText: # Include pre- and post_tasks transparently for all Ansible roles
  
  (Asked by: username_0 on 2020-08-04)
  
  I am looking for a way to execute some actions before and after each role has run.
  `pre_tasks` and `post_tasks` look like a good candidate, but I have found no way to include them transparently for all defined roles/playbooks. I don't want to add these manually to all playbooks since this seems like a maintenance nightmare.
  
  Another angle to solve this might be to abuse the fact that Ansible logs in via ssh. However I am not aware of any identifying properties of an Ansible login as opposed to a regular user login.
  
  My specific issue is:
  I want to trigger [etckeeper](http://etckeeper.branchable.com/) before and after each execution of a role.
  
  If you have any other "out-of-the-box" ideas to solve this, I will be grateful to hear them.
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: Long shoot (and this should be a comment really). Create another role with a task for the pre and a handler for the post than add the role as a dependency (in meta/main.yml) to the other roles. The dependencies are executed only once and with any luck the handlers will be executed at the end. It’s not exactly what you’re looking for but it’s close.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-09-28. Score: 1
  
  Not tested this, but is [include\_role](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_role_module.html) any help? so instead of a playbook like this,
  
  ```
   ---
   - name: Example
     hosts: all
     roles:
      - first_role
      - second_role
      - third_role
  
  ```
  
  maybe this,
  
  ```
   ---
   - name: Example
     hosts: all
     tasks:
      - etckeeper_task
      - include_role:
          name: first_role
      - etckeeper_task
      - include_role:
          name: second_role
      - etckeeper_task
      - include_role:
          name: third_role
      - etckeeper_task
  
  ```
  
  alternatively (also untested) you could build a role, let's call it `wrap_role` with `tasks/main.yml` containing,
  
  ```
  ---
  # Replace this task with your actual before-role etckeeper task,
  - name: etckeeper pre-actions
    meta: noop
  
  - include_role:
    name: {{role_name}}
  
  # Replace this task with your actual after-role etckeeper task,
  - name: etckeeper post-actions
    meta: noop
  
  ```
  
  Now your top-level playbook can be something like,
  
  ```
   ---
   - name: Example
     hosts: all
     roles:
       - { role: wrap_role, role_name: first_role }
       - { role: wrap_role, role_name: second_role }
       - { role: wrap_role, role_name: third_role }
  
  ```
  
  not ideal based on your question, but at least it leaves the roles untouched, even if you have to alter top-level playbooks. If I get the time I'll test the above suggestions and update this answer accordingly.
  
  > **Comments:**
  > **username_0**: Thank you for your suggestions, but as you have noted this does not solve my problem.  
  
  ---
  
  