Answers: <p>What you are looking for is the <code>meta/main.yml</code> in the roles of Ansible.</p>
  
  <p>So make your every <code>playbook</code> (the ones you want to run after a task/role) as a <code>role</code> itself (to make it secondary execution) and what ever task/role you want to run before it should be added to the <code>meta/main.yml</code> of all the secondary execution roles. This would make all your roles execute the role mentioned in <code>meta/main.yml</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-28T07:38:07.650Z
Answers: I don't think that this answers the question. This will make any playbook _that uses the role specified_ run the dependencies of that role automatically. What OP wants is for any playbook, _irrespective of roles used_ to run specified tasks automatically. An analogy might be the startup programs of your boot sequence which get run automatically before any application is opened.
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2019-12-28T13:37:12.647Z
Answers: Yes, exactly.  It is an interesting suggestion, but it does not address the request: I would need to explicitly list a role whose `meta/main.yml` file contained a dependency to the required role in every playbook.  The request is that nothing needs to be explicited on the playbook, so I could even copy a playbook from a different location and it would automatically include the role, unaltered.
Answers: caxcaxcoatl
Answers: CC BY-SA 4.0
Answers: 2019-12-28T04:24:35.483Z
Answers: 2019-12-28T04:24:35.483Z
Answers: Zohaib Amanzai
Body: <p>Is there a way to run a specific task or role at the very beginning of every <code>ansible-playbook</code> run?</p>
  
  <p>I'm looking for something that would go into <code>ansible.cfg</code> or something like that, so all playbooks using that file would run a specific task at the beginning.</p>
  
  <p>My intention is to use it for doing some verifications: for example, "<em>is the local host listed on the inventory?  If not, perhaps I'm using the wrong inventory</em>".</p>
  
  <p>I could <code>import</code> or <code>include</code> such a role in every playbook, but I want it to be automatic, and to work even for playbooks that don't know about that role.</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-12-13T08:57:32.223Z
Comments: Offhand, I think the answer to this question would be a sound "no". You would need to hack one of the plugins, or write your own one which gets loaded by your config file. It would be far easier to simply add `pre_tasks` to the playbooks.
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2020-01-27T06:40:43.213Z
Comments: this has been done to customise ansible tower so that all playbooks by all teams check for an open change request ticket and change window on a specific system else fail the playbook. i am not sure how they did it.
Comments: simbo1905
Comments: CC BY-SA 4.0
Comments: 2020-09-11T07:07:07.340Z
Comments: did you write a plugin for this?
Comments: jim smith
Comments: CC BY-SA 4.0
Comments: 2023-06-16T19:50:43.930Z
Comments: Did you ever find a solution for this?
Comments: cueedee
Comments: CC BY-SA 4.0
Comments: 2023-06-17T01:29:45.710Z
Comments: No, I haven't, @cueedee...  In fact, I don't even recall what I needed it for :p
Comments: caxcaxcoatl
Comments: CC BY-SA 4.0
Comments: 2023-06-20T13:41:42.473Z
Comments: I had hoped to use it to log [the complete command line arguments (`argv`) of  `ansible-playbook` invocations](https://stackoverflow.com/q/39502543/5110545) somewhere (another non-trivial problem, it seems).
Comments: cueedee
Tags: <ansible>
Title: Running task or role before any playbooks
UserDisplayName: caxcaxcoatl
ThreadText: # Running task or role before any playbooks
  
  (Asked by: username_0 on 2019-12-13)
  
  Is there a way to run a specific task or role at the very beginning of every `ansible-playbook` run?
  
  I'm looking for something that would go into `ansible.cfg` or something like that, so all playbooks using that file would run a specific task at the beginning.
  
  My intention is to use it for doing some verifications: for example, "*is the local host listed on the inventory? If not, perhaps I'm using the wrong inventory*".
  
  I could `import` or `include` such a role in every playbook, but I want it to be automatic, and to work even for playbooks that don't know about that role.
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: Offhand, I think the answer to this question would be a sound "no". You would need to hack one of the plugins, or write your own one which gets loaded by your config file. It would be far easier to simply add `pre_tasks` to the playbooks.  
  > **username_2**: this has been done to customise ansible tower so that all playbooks by all teams check for an open change request ticket and change window on a specific system else fail the playbook. i am not sure how they did it.  
  > **username_3**: did you write a plugin for this?  
  > **username_4**: Did you ever find a solution for this?  
  > **username_0**: No, I haven't, @username_4...  In fact, I don't even recall what I needed it for :p  
  > **username_4**: I had hoped to use it to log [the complete command line arguments (`argv`) of  `ansible-playbook` invocations](https://stackoverflow.com/q/39502543/5110545) somewhere (another non-trivial problem, it seems).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_5 on 2019-12-28. Score: 1
  
  What you are looking for is the `meta/main.yml` in the roles of Ansible.
  
  So make your every `playbook` (the ones you want to run after a task/role) as a `role` itself (to make it secondary execution) and what ever task/role you want to run before it should be added to the `meta/main.yml` of all the secondary execution roles. This would make all your roles execute the role mentioned in `meta/main.yml`
  
  > **Comments:**
  > **username_1**: I don't think that this answers the question. This will make any playbook _that uses the role specified_ run the dependencies of that role automatically. What OP wants is for any playbook, _irrespective of roles used_ to run specified tasks automatically. An analogy might be the startup programs of your boot sequence which get run automatically before any application is opened.  
  > **username_0**: Yes, exactly.  It is an interesting suggestion, but it does not address the request: I would need to explicitly list a role whose `meta/main.yml` file contained a dependency to the required role in every playbook.  The request is that nothing needs to be explicited on the playbook, so I could even copy a playbook from a different location and it would automatically include the role, unaltered.  
  
  ---
  
  