Ansible Roles
In this post, I've gone thru multiple articles and YouTube videos on 'ansible roles', which helped me to understand into a deeper level of Ansible roles and galaxy site and CLI commands with various options. So, sharing here with you
What is role in Ansible?
The role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.
Why should I use Roles in Ansible?
The following are the reasons to work with roles
- Address complexity
- Reusability and sharing
- Modular code
Learn how to build and create ansible roles to manage remote machines using an ansible configuration management tool. For example let's create an apache role to install, configure and easy to manage the Apache web server using the roles.
Ansible Galaxy ansible roles structure |
Defining Ansible Roles are having major role in simplifying a larger playbook split into multiple small files called roles and they can be used in two ways import or include roles. Ansible roles have a well-defined structured layout that is understood by the Ansible engine and executed accordingly.
Prerequisites
- Ansible installed ecosystem
- should aware how ansible works
Roles precedence(priorities) how it works helps you to understand the role flow. the separate folder for playbooks then use ./roles directory top priority.
- ./roles <== top
- ./ansible/roles
- /etc/ansible/roles
- /etc/share/ansible/roles <== low
Comparison with other Legacy configuration tools
In puppet - module, Chef - cookbook similarly in Ansible - roles will work for code once and re-use it as many as you wish to.
When you have repeated tasks to be performed, then the role is the way to reuse the play in the playbook.
Ansible HOME will have the roles folder where you can create new roles and call them in a playbook where you need it.
We can use the ansible-galaxy command to create the directory structure, if you don't want to download from the internet then use --offline Option.
ansible-galaxy init apache --offline
ls -l # check for the apache folder, which is a role created with the above command
ansible-galaxy execution in offline |
Understanding Ansible roles directory structure
The whole directory structure (containing defaults, files, handlers, meta, README.md, tasks, templates, vars) will be created by the ansible-galaxy command.
default directory in roles will be used for OS Booting time executable files, OS related changes or patching.to store default variables that can be used by the role.
files - these are static files which can be pushed to remote machines.
handlers - based on the events these handlers will be triggered to some action, one-time executors, first time only works.
Example: if httd.conf file changed, it should trigger service restart the httpd
meta - it will tell about the role, doesn't perform any action.
Example: Who is the author of the role, platform etc.
tasks - actual logic will be stored here, this will have the set of task related YAML files, which are reusable
templates - same like files copied to remote, but the data can be changed at runtime, in Ansible we can have jinja2 templating
vars - are like default variables, they have a higher priority - cannot be overridden
The roles folder should go into the ANSIBLE_CONFIG defined location, Usually, each project may have its own limited roles. If there is a need for some common functionality required for multiple projects then we can use global-level roles.
Ansible Role path configuration
In side your project folder let's say in our case 'qa' directory we will have ansible.cfg file.
Add the following line:
role_path: /home/vagrant/qa/roles
What is ansible-galaxy does?
It's a command-line tool bundled with ansible installation. The purpose of ansible-galaxy command is to :
- download and install roles from Galaxy or from GitHub.
- manages roles on the Galaxy website.
1. Galaxy Search from CLI
To search the roles on the Ansible Galaxy website we can go to the browser and search 🔍 in Ansible Galaxy page. Similarly from our command line we can use search option to display all available possible roles and collections available on Galaxy.
ansible-galaxy search jenkins
To filter the output of the search with platform-specific to LINUX platforms RHEL family CentOS, fedora, use will fall into EL type, other than that Ubuntu let me check with EL, then you can see here 177 roles available.
ansible-galaxy search jenkins --platform EL
2. Get info about a role
To get information about the Ansible role, this will give you the clarity about from which author and for which platform it will be useful to use the given role. Most importantly we can also look for how recently it is updated.
ansible-galaxy info bertvv.wordpress
3. Download and install the roles
To download and install the roles from the RedHt Galaxy website
Install a role Syntax:
ansible-galaxy install authorname.rolename
Example:
ansible-galaxy install geerlingguy.nginx
Download and Install Ansible roles from Galaxy website |
4. Installing to custom roles directory
ansible-galaxy install --roles-path /path/to/store/role authorname.rolename
ansible-galaxy install --roles-path /home/vagrant/myproject/roles lean_delivery.weblogic
Install Roles from any git-based SCM
We can use Ansible Galaxy roles can be downloaded and install the required Roles from any public SCM URLs
ansible-galaxy install [git-base-url]
Here we have two Examples for GitHub and GitLab URL:
GitHub
ansible-galaxy install git@https://github.com/acme/role.git,v1.2.0
GitLab
ansible-galaxy install git@gitlab.acme.com:mygroup/ansible-base.git,0b7cd353.
Install using requirements file
You could also install a role from requirements.yml file where we can define requirements.yml file with different software or cloud related roles you should change those parameters as per your Project needs.
- src: geerlingguy.nginx version: 2.7.0 name: nginx
Now you can run the galaxy command as follows:
ansible-galaxy install -r rolenginx/requirements.yml -p rolenginx/
Here option -p will be specify the directory where we want install the role
ansible-galaxy command using requirement.yml file |
5. Listing installed roles
To check what all the roles installed so far on your machine will be listed:
ansible-galaxy list
6. Remove a role
To remove the role that is already installed
ansible-galaxy remove authorname.rolename
Alert! Better to check the installed role list before and after the removal of the role. That will give you the confidence to proceed.
ansible-galaxy remove geerlingguy.nginx
Ansible role nginx deletion steps |
In the next, we will see the How to create and use the Custom Roles?
Hope you enjoyed this post, Keep learning, Keep smiling Keep sharing ... :)
...
HAPPY A U T O M A T I O N S
No comments:
Post a Comment