Creating Your First Ansible Role
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
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:
paulh@ansible-controller:~/code$ tree
.
├── ansible.cfg
├── ansible.cfg.example
├── first_playbook.yml
├── inventory
└── roles
└── common
├── defaults
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
9 directories, 4 files
Make sure your output looks the same as mine as it will be important in future lectures.
Recall each folder's purpose within a role
Try to recall the purpose of each of these folders:
See if you can identify them, what they do, and what types of files should be located in each. Also, note that these are not ALL of the folders that can exist within a role, but these are definitely the most common.
Conclusion
That is all we need to do to setup our Ansible Role... see you in the next lesson!
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.