0% found this document useful (0 votes)
4 views12 pages

Ansible Roles Explained With Examples

The document provides a comprehensive guide on Ansible roles, explaining their purpose, structure, and how to create them with examples. It details the use of the 'ansible-galaxy' command to initialize roles and outlines the directory structure, including tasks, handlers, and files. A practical example of creating an Ansible role for deploying an Apache web server is also included, demonstrating the steps to implement and verify the deployment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Ansible Roles Explained With Examples

The document provides a comprehensive guide on Ansible roles, explaining their purpose, structure, and how to create them with examples. It details the use of the 'ansible-galaxy' command to initialize roles and outlines the directory structure, including tasks, handlers, and files. A practical example of creating an Ansible role for deploying an Apache web server is also included, demonstrating the steps to implement and verify the deployment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

Ansible Roles Explained with Examples - Ansible


Tutorials
 February 04, 2018

This article explains you about what is Ansible Roles and how to create Ansible roles with
examples.

In the previous posts, we have explained the below topics. Refer those links to understand this
topic from basics.

1. What is Ansible, How Ansible works, Ansible Introduction, Ansible Basic Tutorials
2. Ansible Inventory Introduction - Ansible Beginner Tutorials
3. Ansible Ad hoc Commands - Ansible Tutorial for Beginners
4. Managing Ansible Configuration Files Explained with Examples
5. Understanding Ansible Playbook - Write your First Playbook
6. Ansible Roles Explained with Examples - Ansible Tutorials
7. How to use Ansible Vault to Protect Ansible Playbooks

Lets get started.

[Link] 1/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

Also Watch this "Ansible Roles" Tutorial video on our YouTube Channel.

Ansible Roles Explained in Detail - Create Your Ansible Role…


Role…

What is Ansible roles?

1. Ansible roles are consists of many playbooks, which is similar to modules in puppet and cook
books in chef. We term the same in ansible as roles.
2. Roles are a way to group multiple tasks together into one container to do the automation in
very effective manner with clean directory structures.
3. Roles are set of tasks and additional files for a certain role which allow you to break up the
configurations.
4. It can be easily reuse the codes by anyone if the role is suitable to someone.
5. It can be easily modify and will reduce the syntax errors.

How do we create Ansible Roles?

To create a Ansible roles, use ansible-galaxy command which has the templates to create it.
This will create it under the default directory /etc/ansible/roles and do the modifications else we
need to create each directories and files manually.

[root@learnitguide ~]# ansible-galaxy init /etc/ansible/roles/apache --offline


- apache was created successfully
[root@learnitguide ~]#

[Link] 2/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

where, ansible-glaxy is the command to create the roles using the templates.
init is to initiliaze the role.
apache is the name of role,
offline - create offline mode rather than getting from online repository.

List out the directory created under /etc/ansible/roles.

[root@learnitguide ~]# tree /etc/ansible/roles/apache/


/etc/ansible/roles/apache/
|-- [Link]
|-- defaults
| `-- [Link]
|-- files
|-- handlers
| `-- [Link]
|-- meta
| `-- [Link]
|-- tasks
| `-- [Link]
|-- templates
|-- tests
| |-- inventory
| `-- [Link]
`-- vars
`-- [Link]
8 directories, 8 files
[root@learnitguide ~]#

We have got the clean directory structure with the ansible-galaxy command. Each directory
must contain a [Link] file, which contains the relevant content.

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 this
role.
defaults - default variables for the role.
vars - other variables for the role. Vars has the higher priority than defaults.
files - contains files required to transfer or deployed to the target machines via this role.
templates - contains templates which can be deployed via this role.
meta - defines some data / information about this role (author, dependency, versions,
examples, etc,.)

Lets take an example to create a role for Apache Web server.

[Link] 3/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

Below is a sample playbook codes to deploy Apache web server. Lets convert this playbook
codes into Ansible roles.

---
- hosts: all
tasks:
- name: Install httpd Package
yum: name=httpd state=latest
- name: Copy httpd configuration file
copy: src=/data/[Link] dest=/etc/httpd/conf/[Link]
- name: Copy [Link] file
copy: src=/data/[Link] dest=/var/www/html
notify:
- restart apache
- name: Start and Enable httpd service
service: name=httpd state=restarted enabled=yes
handlers:
- name: restart apache
service: name=httpd state=restarted

First, move on to the Ansible roles directory and start editing the yml files.

cd /etc/ansible/roles/apache

1. Tasks

Edit [Link] available in the tasks folder to define the tasks to be executed.

[root@learnitguide apache]# vi tasks/[Link]


---
- name: Install httpd Package

[Link] 4/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

yum: name=httpd state=latest


- name: Copy httpd configuration file
copy: src=/data/[Link] dest=/etc/httpd/conf/[Link]
- name: Copy [Link] file
copy: src=/data/[Link] dest=/var/www/html
notify:
- restart apache
- name: Start and Enable httpd service
service: name=httpd state=restarted enabled=yes

Altogether, you can add all your tasks in this file or just break the codes even more as below
using "import_tasks" statements.

[root@learnitguide apache]# cat tasks/[Link]


---
# tasks file for /etc/ansible/roles/apache
- import_tasks: [Link]
- import_tasks: [Link]
- import_tasks: [Link]

Lets create [Link], [Link], [Link] included in the [Link] with actions in the
same directory.

[Link]

[root@learnitguide apache]# cat tasks/[Link]


---
- name: Install httpd Package
yum: name=httpd state=latest

[Link]

[root@learnitguide apache]# cat tasks/[Link]


---
- name: Copy httpd configuration file
copy: src=files/[Link] dest=/etc/httpd/conf/[Link]
- name: Copy [Link] file
copy: src=files/[Link] dest=/var/www/html
notify:
- restart apache

[Link]

[Link] 5/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

[root@learnitguide apache]# cat tasks/[Link]


---
- name: Start and Enable httpd service
service: name=httpd state=restarted enabled=yes

2. Files
Copy the required files ([Link] and [Link]) to the files directory.

[root@learnitguide apache]# ll files/*


-rw-r--r-- 1 root root 11753 Feb 4 10:01 files/[Link]
-rw-r--r-- 1 root root 66 Feb 4 10:02 files/[Link]
[root@learnitguide apache]# cat files/[Link]
This is a homepage created by [Link] for ansible roles.
[root@learnitguide apache]#

3. Handlers
Edit handlers [Link] to restart the server when there is a change. Because we have already
defined it in the tasks with notify option. Use the same name "restart apache" within the
[Link] file as below.

[root@learnitguide apache]# cat handlers/[Link]


---
# handlers file for /etc/ansible/roles/apache
- name: restart apache
service: name=httpd state=restarted

4. Meta
Edit meta [Link] to add the information about the roles like author, descriptions, license,
platforms supported.

[root@learnitguide apache]# cat meta/[Link]


galaxy_info:
author: [Link]
description: Apache Webserver Role
company: [Link]
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: [Link]
# Some suggested licenses:
# - BSD (default)
# - MIT
# - GPLv2

[Link] 6/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

# - GPLv3
# - Apache
# - CC-BY
license: license (GPLv2, CC-BY, etc)
min_ansible_version: 1.2
# If this a Container Enabled role, provide the minimum Ansible Container version.
------skipped

List out the created files now,

[root@learnitguide apache]# tree


.
|-- [Link]
|-- defaults
| `-- [Link]
|-- files
| |-- [Link]
| `-- [Link]
|-- handlers
| `-- [Link]
|-- meta
| `-- [Link]
|-- tasks
| |-- [Link]
| |-- [Link]
| |-- [Link]
| `-- [Link]
|-- templates
|-- tests
| |-- inventory
| `-- [Link]
`-- vars
`-- [Link]
8 directories, 13 files
[root@learnitguide apache]#

We have got all the required files for Apache roles. Lets apply this role into the ansible playbook
"[Link]" as below to deploy it on the client nodes.

[root@learnitguide apache]# cat /etc/ansible/[Link]


---
- hosts: node2

[Link] 7/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

roles:
- apache
[root@learnitguide apache]#

We have defined this changes should be run only on node2, you can also use "all" if need.
Specify the role name as "apache", also if you have created multiple roles, you can use the
below format to add it.
- apache
- nfs
- ntp

Lets verify for syntax errors:

[root@learnitguide apache]# ansible-playbook /etc/ansible/[Link] --syntax-check


playbook: /etc/ansible/[Link]
[root@learnitguide apache]#

No errors found. Let move on to deploy the roles.

[root@learnitguide apache]# ansible-playbook /etc/ansible/[Link]


PLAY [node2]
**********************************************************************************************
*****
TASK [Gathering Facts]
*****************************************************************************************
ok: [node2]
TASK [apache : Install httpd Package]
**************************************************************************
changed: [node2]
TASK [apache : Copy httpd configuration file]
******************************************************************
Changed: [node2]
TASK [apache : Copy [Link] file]
***************************************************************************
changed: [node2]
TASK [apache : Start and Enable httpd service]
*****************************************************************
changed: [node2]
RUNNING HANDLER [apache : restart apache]
**********************************************************************
changed: [node2]
PLAY RECAP
**********************************************************************************************
[Link] 8/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

*******
node2 : ok=6 changed=5 unreachable=0 failed=0

That's it, We have successfully deployed the Apache webserver using Ansible Roles to the client
node "node2".

Login into the client node "node2" and verify the following things.

[root@node2 ~]# rpm -q httpd


[Link].6.x86_64
[root@node2 ~]# systemctl status httpd
[Link] - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/[Link]; enabled)
Active: active (running) since Sun 2018-02-04 10:23:44 IST; 1min 58s ago
Docs: man:httpd(8)
man:apachectl(8)

Access the webpage using elinks command or using browser, you will be able to access it.

[root@node2 ~]# elinks [Link]


This is a homepage created by [Link] for ansible roles.

So hope you have got an idea about Ansible Roles. Going forward we will play more with ansible
tool. Keep practicing and have fun. thank you. ansible, ansible roles, ansible role, ansible roles explained, create ansible roles,
how to create ansible roles, what is ansible roles, understanding ansible roles, ansible examples, ansible roles with examples, ansible documentation,

Support Us: Share with your friends and groups. ansible roles explained with examples, playbook roles, ansible roles vs ansible
playbooks, creating ansible roles, how does ansible roles works, ansible roles tutorial, ansible roles directory structure, ansible role examples

Stay connected with us on social networking sites, Thank you.


Youtube Channel : [Link]
Facebook : [Link]
Twitter : [Link]
Pinterest : [Link]
RSS : [Link]
,ansible roles path, ansible best practice, ansible galaxy, ansible roles youtube, ansible roles training, ansible roles beginners, ansible roles beginners tutorial

[Link] 9/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

 Tags Ansible 9 Ansible Videos 7 Devops 45 Devops Videos 26 Trending 61 Videos 39

Next Previous
DevOps Tutorial - What is DevOps, How to Understanding Ansible Playbook - Write your
 Start with DevOps First Playbook 
([Link] ([Link]
[Link]) [Link])

POST A COMMENT

Disqus Facebook Blogger

[Link] 10/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials

Sponsored Links

Ultra Luxury 3,4 & 5BHK Apartments in Bangalore


SNN Builders

With its cutting-edge technology and advanced engineering solutions, Mitsubishi Electric is Partnering
With India’s Dream To Be No.1.
Mitsubishi Electric

Indians Born Before 1981 Are Eligible For This Second Income
Survey Compare

Apartments Sale In Bangalore | 1&2 BHK Ready To Move In @80 Lacs


Cauvery Estates

Valentine's Day Celebrations Around The World


Hale 'N' Hearty

8 Best Foods That Can Reduce Blood Pressure Naturally!


Zolearn

2 Comments learnitguide 
1 Login

 Recommend t Tweet f Share Sort by Newest

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

bhanu • 7 months ago


very useful Article
△ ▽ • Reply • Share ›

Admin Mod > bhanu • 7 months ago


Thanks for your comment, we have lot of other tools related to new technologies, please
checkout and make use of it Stay connected with us
[Link] 11/12
2/5/2019 Ansible Roles Explained with Examples - Ansible Tutorials
checkout and make use of it. Stay connected with us.
△ ▽ • Reply • Share ›

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy

Sponsored Links

Samsung Galaxy M10 #IMPOWERD


TOI | Samsung

With Rising Pollution, An AC Should Do More Than Just Cooling


Bloomberg | Quint

Hearing aids with bluetooth will change your life!


[Link]

With its cutting-edge technology and advanced engineering solutions, Mitsubishi Electric
is Partnering With India’s Dream To Be No.1.
Mitsubishi Electric

Indians Born Before 1981 Are Eligible For This Second Income
Survey Compare

Ultra Luxury 3,4 & 5BHK Apartments in Bangalore


SNN Builders

[Link] 12/12

You might also like