Roles of Ansible in DevOps

Roles of Ansible: Roles are a robust feature of Ansible that facilitates reuse and further promotes the modularization of the configuration, but the roles of Ansible are often overlooked instead of simple.  The good news is that the roles of Ansible They are easy to configure and allow complexity when necessary.   Join me as I go through the basics of how to set up and deploy a role Simple of Ansible.

Roles of Ansible

The anatomy of a paper Ansible

The concept of roles of ansible is simple;   is a group of variables, tasks, files and controllers that are stored in a standardized file structure.

The most complicated part of a role is to remember the structure of the directory, but there is help.  The built-in ansible  -galaxy command has a subcommand that will create our role skeleton for us.

Simply use     ansible   -galaxy   init   <ROLE_NAME>     to create a new role in your current working directory.  You will see here that several directories and files are created within the new role:

The number of files and directories may seem intimidating, but they are quite simple.     Most directories contain a main.yml record ; Ansible   use each of these files as an entry point to read the contents of the directory (except files, templates and proof).     You have the freedom to divide your tasks and variables into other files within each directory.   But when you do this, you should use the include directive in a directory of main.yml to have your files used as part of the function. We will examine this more closely after a brief summary of the purpose of each directory.

The defaults directory is designated for the default values ​​of variables that have the lowest priority.     Put another way: if a variable is not defined anywhere else, the definition given in predetermined values ​​/  main.yml It will be used.

the records Y templates The directories have a similar purpose.   They contain affiliate files and   templates   Ansible   (respectively) that are used within therole.    The nice part of these directories is that   Ansible   You do not need a route for the resources stored in them when you work in the role.   Ansible   check first.    You can still use the full path if you want to reference files outside the role, however, best practices suggest that you keep all the components of the role together.

The   directory of   controllers   It is used to store   controllers   Ansible. If you are not familiar, the   handlers   Ansible   they are simply tasks that can be markedduring a play so that they are executed at the end of the execution.     You may have as many or as few handlers as are necessary for your function.

The   goal   directory contains authoring information that is useful if you choose to publish your role in . The   directory   goal   it can also be used to define role dependencies.     As you may suspect, a role dependency allows you to request that other roles be installed before the role in question.

the file is simply a README file in markdown format.   This file is essential for the roles published in and, honestly, the file should include a general description of how your role works, even if you do not make it available to the public.

the homework directory is where most of your paper will be written.  This directory includes all the tasks that your role will perform. Ideally, each set of logically related tasks would be established in their own files, and will simply be included through main.yml file in the Chores directory.

the test directory contains a sample inventory and a test.yml Playbook     This can be useful if you have an automated test process based on your function.     It can also be useful while building your role, but its use is not mandatory.

The last directory created is the vars directory.     This is where you create the variable files that define the variables needed for your role.     The variables defined in this directory are for internal use only.     It is a good idea to make a name space in the names of the role variables, to avoid possible name conflicts with variables outside of their role.     For example, if you need a variable called configuration file In your reference playbook, you may want to name your variable baseline_config_ file , to avoid conflicts with another possible configuration file variable defined elsewhere.

Creating a simple role of ansible

As mentioned above, the roles of Ansible They can be as complex or simple as you need them. Sometimes, it is useful to start in a simple way and iterate towards more complex role as it underpins the basic functionality. Let’s try that, and let’s define a role called base_httpd that install httpd with a simple configuration and a very simple website.

To begin with, we will need to create our role. We could create each directory and file by hand, but it’s much easier to let ansible  -galaxy Do the hard work for us by simply running     ansible-   galaxy   init   base_ httpd   :

Next, we can create our simple web page in the file directory. For our academic purposes, we can create a file called index.html that contains a tried and true sample text:

We will create a template for our httpd.conf file by copying an existing one from a new installation of   httpd   .     Let’s take this opportunity to define a couple of predetermined variables in our role. We will make a default listening port of 80 and a   LogLevel   which by default will warn.   We can do this by adding an entry to default /  main.yml :

You will notice here that the template file has the .j2 extension as personalized, and I’ve used   grep   to highlight where we have customized the template by replacing the default values ​​in httpd.conf with   Variables   ansibles   . Then I show where the variables are defined in default /  main.yml . Here we prefer the  use defaults instead of  vars , since it allows a later personalization without having to change the real role.

Now that we have our website and simple configuration, we will need to create the tasks to give life to our web server.     As an example,   isolate   configuration   httpd  to your own   archive   yaml   in chores /  httpd.yml and then include that file in the chores /  main.yml :

We use the yum, template, copy and service modules to install, configure and start our web server.     It is worth noting here that I refer to the httpd.yml , httpd.conf.j2 , Y index.html Files without complete routes, since they are stored inside the function.

Unfolding the roles of Ansible

Most of the hard work was completed when we built the paper itself.  The deployment of the function, in comparison, is easy.  We just need to set up a simple playbook and select the function using the appropriate keyword:

We could easily add typical tasks after implementation of the role, or we could also implement additional roles simply adding them to the list. In addition, we can override the default variables that we configure using the same variable names as shown below:

The   functions ansible provide a robust way to organize the   artifacts   and tasks of   implementation   in order to reuse and share them. The tooling provided in ansible  -galaxy Makes configuring a new role as simple as ever.   Anyone with a knowledge   basic   how   game writing books   Ansible   can   create a   role of  Ansible with the   same   ease   .

So these were roles of Ansible in DevOpsInnovative Technology Solutions is authorized Red Hat Training Partner in India & It offers number of Red Hat Certification in India.