defaults:
Contains the role's default variables. Since default variables have the lowest priority, overriding them is simple.
vars:
Contains the role's variables. Variables in the vars directory are given precedence over those in the defaults directory.
tasks:
These consist of the primary set of actions that the position is expected to take.
files:
It consists of files we wish to copy to the remote host. The path to the resources kept in this directory does not need to be specified.
templates:
Includes file templates that allow changes from the role. For building templates, we employ the Jinja2 templating language.
meta:
Provides information about dependencies, support platforms, and roles like an author.
handlers:
It contains handlers connected to services that can be called by "notify" directives.
How should variables be defined in a role?
- The layout of the ansible roles directory allows for the definition of variables at various levels.
- The default variables used for default role functioning are contained in the main.yml file in the vars folder. It is not intended to overwrite them.
- Default variables may be found in defaults/main.yml. Variables can replace these variables with the same name set in the playback and have higher precedence because they have lower priority.
- Variables set in the playbook will always take precedence over those specified in the role. Secrets and encrypted data from the vault should always be controlled from the playbook because role variables are meant to be general.
- According to the playbook's definition, role variables have the highest precedence and overwrite inventory variables and playbook variables when a role is called.
Different locations for Ansible roles
Roles may be readily used from playbooks by storing them in the default place.
- ./roles The highest precedence is ./roles.
- /.ansible/roles After that, /.ansible/roles are checked
- /etc/ansible/roles Next, /etc/ansible/roles are examined.
Creating a New Role
Refactoring an Ansible playbook into a role is a typical strategy. To do that, we must take apart a playbook into its component components and then use the directories we just saw in the previous section to piece them back together as an Ansible role.
The installation and configuration of a primary Nginx web server from scratch will be demonstrated in this section using an example of adding a new role. You must have VirtualBox, Ansible, and Vagrant installed locally if you want to follow along.
In common pathways like the directory of the orchestrating playbook, the roles/ directory, or the set roles path configuration variable, Ansible looks for referenced roles. Additionally, a custom path can be provided when referring to a role:
- hosts: all
roles:
- role: "/custom_path/to/the/role
Creating a Simple Ansible Role
Our Ansible roles can be as straightforward or as sophisticated as required. Let's begin by establishing a specific position that will use the apt package manager to update all the packages on a remote system. Only a minimal role will be created, and we'll consider how to use it in our playbook.
Setting Up Our Ansible Role
We need to construct our role first. Although we can manually create each directory, it is easier to allow ansible-galaxy to handle this for us:
ansible-galaxy init apt_update
The following will then appear in our console output:
- Role apt_update was created successfully
Creating Our Tasks
The job of updating the packages on our remote system will then be created in the role.
# tasks file for apt_update
- apt:
name: "*"
state: latest
The data above will be included in our tasks/main.yml file.
We won't add any variables, templates, or files just now because we want to create a simple role. We will next proceed to construct a playbook that includes this job.
Creating Our Playbook
We will need to construct a new playbook that includes this job now that we have established our role to update packages.
- hosts: something
become: yes
roles:
- /full_path_to_role/galaxy/apt_update
Remember that this playbook doesn't have to be in the same directory as our role; it can be found anywhere on our system.
Running Our Playbook
We have now developed our playbook and included our contribution to it. All that is left for us to execute this script, at which point the packages on our remote system will be updated and displayed on our terminal.
ansible-playbook apt_update_playbook.yml
Frequently Asked Questions
What are Ansible Server requirements?
In order to install Linux in a virtual machine if you use Windows, you must be a Windows user. Python 2.6 or later is necessary.
What is Ansible Galaxy?
Ansible includes this utility to build a base directory structure. On the website Galaxy, individuals can discover and distribute Ansible material. The following command can be used to download roles from the website:
$ ansible-galaxy install username.role_name
What’s an ad hoc command?
Without utilizing a playbook, users start ad hoc commands to take action on a host. Think of it as a single command.
Conclusion
In this article, we have extensively discussed his article explains the details of Ansible - Roles along with the details of Role Directory Structure, variable defining, location for Ansible-Roles, and Creating a New Role
We hope this blog has helped you enhance your knowledge regarding Ansible Roles. If you want to learn more, you can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!!
