ansible

Split ansible role's tasks into multiple files


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?


Solution

  • 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