Roles In Ansible

Roles In Ansible

Roles in Ansible

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed modules in Ansible.

https://cloudaffaire.com/modules-in-ansible/

In this blog post, we will discuss modules in Ansible.

Roles in Ansible:

Roles are ways of automatically loading certain vars_files, tasks, and handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users. Roles expect files to be in certain directory names.

Roles must include at least one of these directories, however it is perfectly fine to exclude any which are not being used. When in use, each directory must contain a main.yml file, which contains the relevant content.

Roles directory structure:

  • tasks: contains the main list of tasks to be executed by the role.
  • handlers: contains handlers, which may be used by this role or even anywhere outside the role.
  • defaults: default variables for the role and configurations for deployment.
  • vars: other variables for the role.
  • files: contains files which can be deployed via the role.
  • templates – contains templates which can be deployed via the role.
  • meta: defines some meta data for the role.

Role order of execution:

  • Any pre_tasks defined in the play.
  • Any handlers triggered so far will be run.
  • Each role listed in roles will execute in turn. Any role dependencies defined in the roles meta/main.yml will be run first, subject to tag filtering and conditionals.
  • Any tasks defined in the play.
  • Any handlers triggered so far will be run.
  • Any post_tasks defined in the play.
  • Any handlers triggered so far will be run.

Note: We have already covered the order of execution in https://cloudaffaire.com/ansible-playbook-components/

Role Search Path: Ansible will search for roles in the following way

  • A roles/ directory, relative to the playbook file.
  • By default, in /etc/ansible/roles

Ansible Galaxy:

Ansible Galaxy is a free site for finding, downloading, rating, and reviewing all kinds of community developed Ansible roles and can be a great way to get a jumpstart on your automation projects. The client ansible-galaxy is included in Ansible. The Galaxy client allows you to download roles from Ansible Galaxy, and also provides an excellent default framework for creating your own roles.

Next, we will create a role to deploy apache webserver in Centos 7.

Role Demo:

Create your role directory structure using ansible-galaxy command.

Note: You can also create the files manually without using ansible-galaxy.

Define your httpd configuration settings using role default variables. This will be imported in your httpd.conf file.

Create configuration file for your httpd server using role template, this imports the default variable in the httpd.conf file.

Declare variables for your role. We are defining the package that will be installed through this role and the httpd.conf file location.

Define the main task for your role. This will install and configure httpd in your server.

Define handlers for your role. This will restart your httpd service post configuration.

Define meta for your role. This is optional if you are planning to upload your role in ansible galaxy you can define this.

Create files for your role usage. We are creating index.html file for our webserver.

Create playbook for your role execution.

Execute the playbook

Check if your web server is working.

Hope you have enjoyed this article.

To get more details on Ansible, please refer below Ansible documentation.

https://docs.ansible.com/

 

Leave a Reply

Close Menu