Creating a base role
In the previous chapter, we created a simple playbook with all plays written inside the same file. After discovering new and exciting information about roles, we will start refactoring our code and making it modular.
Refactoring our code – creating a base role
We have written two plays in the simple_playbook.yml
file. We intended to run the first play on all hosts. This play has tasks to create users, install essential packages, and so on:
It's a good practice to combine all such essential tasks and create a base role. You can name it as base, common, essential, or whatever you please, but the concept remains the same. We will now move this code to the base role:
- Create the directory layout for the base role. Since we are only going to specify tasks, we just need one subdirectory inside the base:
$ mkdir -p roles/base/tasks
- Create the
main.yml
file insideroles/base/tasks
to specify tasks for the base role. - Edit the
main.yml
file and add the following code:--...