Reputation: 20579
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?
Upvotes: 24
Views: 15984
Reputation: 10957
You essentially have 2 ways (two builtin modules to be precise) to achieve this. Displayed for both approaches is how your tasks/main.yml
file might look like:
include_tasks
module
---
- name: First tasks
ansible.builtin.include_tasks:
file: run-this-first.yml
- name: second tasks
ansible.builtin.include_tasks:
file: run-this-second.yml
- name: Last tasks
ansible.builtin.include_tasks:
file: run-this-last.yml
import_tasks
module
---
- name: First tasks
ansible.builtin.import_tasks:
file: run-this-first.yml
- name: second tasks
ansible.builtin.import_tasks:
file: run-this-second.yml
- name: Last tasks
ansible.builtin.import_tasks:
file: run-this-last.yml
Note that, according to the ansible documentation about Roles the import_tasks
module is the suggested way to go.
The difference in the above examples is really just include_tasks/import_tasks
.
However, which module you use will affect how the tasks are imported as the include_tasks
imports tasks in a dynamic manner, while import_tasks
imports them in a static way.
A comparison of the two approaches can be found here in the official guide about re-using artifacts and you can read more about when you might want to use which module here in this same guide.
Upvotes: 11
Reputation: 23781
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
Upvotes: 36