This is my ansible role:
/roles
/foo
/tasks
main.yml <----- I want to split this
The main.yml
file is really big, so I want to split it into multiple files, and call them in sequence.
/roles
/foo
/tasks
run-this-first.yml <--- 1st
run-this-last.yml <--- last
run-this-second.yml <--- 2nd
How do I invoke those files, and how do I ensure they are run in order?
Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify keyword and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.
Step 1 — Navigate to /etc/ansible/roles directory and create the roles for prerequisites, MongoDB and NodeJS. You should now see three roles in your 'roles' directory. Step 2 — Write main. yml for prerequisites which installs Git.
Ansible playbook is a script file which contains all the tasks that need to be performed along with all the ingredients required to perform these tasks. Roles are ways of automatically certain var files, tasks, and handlers based on the known file structure.
You can do it with include_tasks
:
/roles
/foo
/tasks
main.yml
run-this-first.yml <--- 1st
run-this-last.yml <--- last
run-this-second.yml <--- 2nd
As you can notice that there is also main.yml
inside the tasks
directory and your main.yml
simply contains this:
---
- include_tasks: run-this-first.yml
- include_tasks: run-this-second.yml
- include_tasks: run-this-last.yml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With