Creating Your First Ansible Role
In this lesson, we will create your first Ansible Role named common. This role will be responsible for updating the server. Unlike using ansible-galaxy, which we'll explore later, you'll learn to create this role manually to better understand its structure.
Creating the Ansible Role Directory Structure
When setting up a role , it's standard practice to create a specific directory structure which Ansible expects and uses to locate files and tasks to execute. Ansible will look for these folders in the directory where you run the ansible-playbook command. This is why it's important to run the playbook command in the same directory where you files are located.
__
Be sure to cd into the directory where you have your ansible config and inventory defined before continuing, which in this course is ~/code
Let's start by creating the directory structure for our common role. Open your terminal and execute the following commands:
mkdir -p roles/common/{tasks,handlers,templates,files,vars,defaults,meta}
For this example, most of these folders will remain empty, but it's important for you to create them so you know that they exist and you can practice using them later.
If you want to view our newly created directories in a pretty format, you can install the tree utility like so:
sudo apt install tree
This will allow us to run the tree command in our home directory and see our newly created role folders:
Make sure your output looks the same as mine as it will be important in future lectures.
Recall each fo…
No comments yet. Add the first comment to start the discussion.