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.
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.
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.
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.
Below is a sample playbook codes to deploy Apache web server. Lets convert this playbook codes into Ansible roles.
[ads-post]
Edit main.yml available in the tasks folder to define the tasks to be executed.
install.yml
Copy the required files (httpd.conf and index.html) to the files directory.
Edit handlers main.yml 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 main.yml file as below.
Edit meta main.yml to add the information about the roles like author, descriptions, license, platforms supported.
- apache
- nfs
- ntp
Lets verify for syntax errors:
Login into the client node "node2" and verify the following things.
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 : https://www.youtube.com/learnitguide
Facebook : http://www.facebook.com/learnitguide
Twitter : http://www.twitter.com/learnitguide
Pinterest : http://www.pinterest.com/learnitguide
RSS : http://feeds.feedburner.com/learnitguide
,ansible roles path, ansible best practice, ansible galaxy, ansible roles youtube, ansible roles training, ansible roles beginners, ansible roles beginners tutorial
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
Also Watch this "Ansible Roles" Tutorial video on our YouTube Channel.
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 --offlinewhere, ansible-glaxy is the command to create the roles using the templates.
- apache was created successfully
[root@learnitguide ~]#
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/We have got the clean directory structure with the ansible-galaxy command. Each directory must contain a main.yml file, which contains the relevant content.
/etc/ansible/roles/apache/
|-- README.md
|-- defaults
| `-- main.yml
|-- files
|-- handlers
| `-- main.yml
|-- meta
| `-- main.yml
|-- tasks
| `-- main.yml
|-- templates
|-- tests
| |-- inventory
| `-- test.yml
`-- vars
`-- main.yml
8 directories, 8 files
[root@learnitguide ~]#
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.
Below is a sample playbook codes to deploy Apache web server. Lets convert this playbook codes into Ansible roles.
---First, move on to the Ansible roles directory and start editing the yml files.
- hosts: all
tasks:
- name: Install httpd Package
yum: name=httpd state=latest
- name: Copy httpd configuration file
copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf
- name: Copy index.html file
copy: src=/data/index.html 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
cd /etc/ansible/roles/apache1. Tasks
[ads-post]
Edit main.yml available in the tasks folder to define the tasks to be executed.
[root@learnitguide apache]# vi tasks/main.ymlAltogether, you can add all your tasks in this file or just break the codes even more as below using "import_tasks" statements.
---
- name: Install httpd Package
yum: name=httpd state=latest
- name: Copy httpd configuration file
copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf
- name: Copy index.html file
copy: src=/data/index.html dest=/var/www/html
notify:
- restart apache
- name: Start and Enable httpd service
service: name=httpd state=restarted enabled=yes
[root@learnitguide apache]# cat tasks/main.ymlLets create install.yml, confgure.yml, service.yml included in the main.yml with actions in the same directory.
---
# tasks file for /etc/ansible/roles/apache
- import_tasks: install.yml
- import_tasks: configure.yml
- import_tasks: service.yml
install.yml
[root@learnitguide apache]# cat tasks/install.ymlconfigure.yml
---
- name: Install httpd Package
yum: name=httpd state=latest
[root@learnitguide apache]# cat tasks/configure.ymlservice.yml
---
- name: Copy httpd configuration file
copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: Copy index.html file
copy: src=files/index.html dest=/var/www/html
notify:
- restart apache
[root@learnitguide apache]# cat tasks/service.yml2. Files
---
- name: Start and Enable httpd service
service: name=httpd state=restarted enabled=yes
Copy the required files (httpd.conf and index.html) to the files directory.
[root@learnitguide apache]# ll files/*3. Handlers
-rw-r--r-- 1 root root 11753 Feb 4 10:01 files/httpd.conf
-rw-r--r-- 1 root root 66 Feb 4 10:02 files/index.html
[root@learnitguide apache]# cat files/index.html
This is a homepage created by learnitguide.net for ansible roles.
[root@learnitguide apache]#
Edit handlers main.yml 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 main.yml file as below.
[root@learnitguide apache]# cat handlers/main.yml4. Meta
---
# handlers file for /etc/ansible/roles/apache
- name: restart apache
service: name=httpd state=restarted
Edit meta main.yml to add the information about the roles like author, descriptions, license, platforms supported.
[root@learnitguide apache]# cat meta/main.ymlList out the created files now,
galaxy_info:
author: LearnItGuide.net
description: Apache Webserver Role
company: LearnITGuide.net
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Some suggested licenses:
# - BSD (default)
# - MIT
# - GPLv2
# - 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
[root@learnitguide apache]# treeWe have got all the required files for Apache roles. Lets apply this role into the ansible playbook "runsetup.yml" as below to deploy it on the client nodes.
.
|-- README.md
|-- defaults
| `-- main.yml
|-- files
| |-- httpd.conf
| `-- index.html
|-- handlers
| `-- main.yml
|-- meta
| `-- main.yml
|-- tasks
| |-- configure.yml
| |-- install.yml
| |-- main.yml
| `-- service.yml
|-- templates
|-- tests
| |-- inventory
| `-- test.yml
`-- vars
`-- main.yml
8 directories, 13 files
[root@learnitguide apache]#
[root@learnitguide apache]# cat /etc/ansible/runsetup.ymlWe 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.
---
- hosts: node2
roles:
- apache
[root@learnitguide apache]#
- apache
- nfs
- ntp
Lets verify for syntax errors:
[root@learnitguide apache]# ansible-playbook /etc/ansible/runsetup.yml --syntax-checkNo errors found. Let move on to deploy the roles.
playbook: /etc/ansible/runsetup.yml
[root@learnitguide apache]#
[root@learnitguide apache]# ansible-playbook /etc/ansible/runsetup.ymlThat's it, We have successfully deployed the Apache webserver using Ansible Roles to the client node "node2".
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 index.html file] ***************************************************************************
changed: [node2]
TASK [apache : Start and Enable httpd service] *****************************************************************
changed: [node2]
RUNNING HANDLER [apache : restart apache] **********************************************************************
changed: [node2]
PLAY RECAP *****************************************************************************************************
node2 : ok=6 changed=5 unreachable=0 failed=0
Login into the client node "node2" and verify the following things.
[root@node2 ~]# rpm -q httpdAccess the webpage using elinks command or using browser, you will be able to access it.
httpd-2.4.6-67.el7.centos.6.x86_64
[root@node2 ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since Sun 2018-02-04 10:23:44 IST; 1min 58s ago
Docs: man:httpd(8)
man:apachectl(8)
[root@node2 ~]# elinks http://node2So 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,
This is a homepage created by learnitguide.net for ansible roles.
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 : https://www.youtube.com/learnitguide
Facebook : http://www.facebook.com/learnitguide
Twitter : http://www.twitter.com/learnitguide
Pinterest : http://www.pinterest.com/learnitguide
RSS : http://feeds.feedburner.com/learnitguide
,ansible roles path, ansible best practice, ansible galaxy, ansible roles youtube, ansible roles training, ansible roles beginners, ansible roles beginners tutorial
Post a Comment