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.

  • 2
    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. Dec 13, 2019 at 8:57
  • 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.
    – simbo1905
    Jan 27, 2020 at 6:40
  • did you write a plugin for this?
    – jim smith
    Sep 11, 2020 at 7:07
  • Did you ever find a solution for this?
    – cueedee
    Jun 16, 2023 at 19:50
  • No, I haven't, @cueedee... In fact, I don't even recall what I needed it for :p Jun 17, 2023 at 1:29

1 Answer 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

  • 3
    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. Dec 28, 2019 at 7:38
  • 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. Dec 28, 2019 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.