Sunday, August 1, 2021

Ansible Reusability with roles and ansible galaxy

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 roles Galaxy
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.

  1.  ./roles <== top
  2.  ./ansible/roles 
  3.  /etc/ansible/roles 
  4.  /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 :

  1. download and install roles from Galaxy or from GitHub. 
  2. 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


Removal of Ansible role
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:

Categories

Kubernetes (24) Docker (20) git (13) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) docker EE (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create deployment (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker container (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)