BACK TO ARTICLES

Ansible Roles: Step-By-Step Example

Published on August 24, 2020

Fakhar ul Hassan

Infrastructure Consultant, Cloud Architect & Automation, DevOps, Infrastructure as Code (IaC), Ansible, Terraform

Ansible Roles: Step-By-Step Example

In Ansible, the role is a way to break/divide a playbook into multiple components/files. This makes the complex playbooks very simple and reusable.

Each role has well defined limited functionality with the option of required output/results. Roles are used via playbooks. Roles cannot be executed directly.

Example:

Current Ansible Environment:

No alt text provided for this image

Pre-Requisites:

1-     One Ansible Control Node

2-     Two Ansible managed hosts (You may use as many as you want)

3-     Network access between control node and managed nodes

4-     SSH keys should have already been generated at control node and shared with managed nodes (see this article to configure SSH Keys: http://tiny.cc/ro75fz)

Step-By-Step Details:

Consider the name of the new role is “httpd”. Following will be the step-by-step procedure to create a role, which will start the httpd service.

Step-1: Create a folder named “httpd” at path /etc/ansible/roles

Step-2: Inside the httpd folder create folders named:

  • defaults
  •  files
  •  handlers
  • meta
  • tasks
  • templates
  • vars

Important Note:

Not all of the above folders will be used in this example, but remember these are the standard folders used for the ansible role’s creation (with different requirements).

[You can create all these role folders automatically using ansible-galaxy command also i.e. ansible-galaxy init httpd]

No alt text provided for this image

Step-3: Copy the httpd template file (which you want to use) named “httpd_conf.j2” at path “/etc/ansible/roles/httpd/template”. This template files is the default standard “httpd.conf” file, with following changes:

1-     Addition of one below line

# {{ ansible_managed }

2- Changing the listening port number from 80 to 81, as shown below:

No alt text provided for this image

Step-4: Create a file named “deployHttpdTemplate.yml” at path “/etc/ansible/roles/httpd/tasks”, as shown below:

No alt text provided for this image

Step-5: Create a file named “main.yml” at path “/etc/ansible/roles/httpd/tasks”, as shown below:’

No alt text provided for this image

See below how files are placed at path “/etc/ansible/roles/httpd/tasks”:

No alt text provided for this image

Step-6: Create our inventory file for the target managed hosts, as shown below:

No alt text provided for this image

Step-7: Create a playbook to deploy the “httpd” role:

No alt text provided for this image

Step-8: Run the playbook with following command:

[root@CentOS-Ctrl-Node playbooks]# ansible-playbook –i inventory httpdPlaybook.yml

No alt text provided for this image

Check the results (highlighted in yellow) in the below httpd.conf file at one of the managed host:

No alt text provided for this image
Written by

Fakhar ul Hassan

Infrastructure Consultant, Cloud Architect & Automation, DevOps, Infrastructure as Code (IaC), Ansible, Terraform