Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split ansible role's tasks into multiple files

Tags:

ansible

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?

like image 496
lonix Avatar asked Aug 25 '19 17:08

lonix


People also ask

What is difference between tasks and handlers in Ansible?

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.

How do I use multiple roles in Ansible?

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.

What is the difference between an Ansible role and a playbook?

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.


1 Answers

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
like image 175
Arbab Nazar Avatar answered Oct 13 '22 21:10

Arbab Nazar