Ansible: Roles

ANSIBLE ROLES:

  • Ansible roles are the organized way to perform the tasks in different playbooks according to their functionality in a directory structure way.
  • Roles allow you to call a set of variables, tasks, and handlers by simply specifying a defined role.
  • In the role file inside every directory, there is a main.yml file.
  • By default, the execution starts from the main.yml file.

Ansible Role Directory Structure:

Mainly ansible roles contain below the directories below.

  • Tasks (a series of tasks are defined here)
  • Handlers (All handlers are defined here)
  • vars (all the variables are defined here)
  • files (all the files will be placed inside this directory)
  • meta (It is used to declare role dependences and information about a role)

 

Example:

#vi playbooks/pb1.yml
 ---
 - hosts: dev 
   remote_user: root 
   roles: 
     - myrole

Create the directories in the role directory.

#cd playbooks
#mkdir -p roles/myrole/{files,handlers,tasks,vars,meta}

Create task files.

#vi roles/myroles/tasks/main.yml

- include: pack_install.yml
- include: deploy_app.yml

Create the installation files.

#vi roles/myrole/tasks/pack_install.yml

- name: install {{pack1}}
  apt:
  name: {{pack1}}
  state: latest
  when: ansible_distribution=='Ubuntu'
  notify:
   - start apache2

- name: install {{pack2}}
  apt: 
  name: {{pack2}}
  state: latest
  when: ansible_distribution=='Ubuntu'
  notify:
  - start tomcat

- name: install {{pack3}}
  apt:
  name: {{pack3}}
  state: latest
  when: ansible_distribution=='Ubuntu'
  notify:
  - start mysql

Create deploy file.

#cd /playbooks/roles/myrole/tasks

#vi deploy_app.yml

- name: to deploy index.html
  copy: 
    src: index.html
    dest: /var/www/html/index.html

- name: to deploy sample.war
  copy: 
    src: sample.war
    dest: /var/lib/tomcat7/webapps/sample.war

Create handlers file.

#vi roles/myrole/handlers/main.yml

- name: start apache
  service:
  name: apache2
  state: started

- name: start tomcat
  service:
  name: tomcat7
  state: started

- name: start mysql
  service:
  name: mysql
  state: started

Create variables file.

#vi roles/myrole/vars/main.yml

pack1: apache2
pack2: tomcat7
pack3: mysql-server

Create sample files.

#vi roles/myrole/files/index.html

<h1> Hello from ansible role</h1>

See the files in the files directory

#vi playbooks/roles/myrole/files# ls

index.html  sample.war

Check the syntax

#ansible-playbook playbooks/myrole.yml --syntax-check

Execute the Roles

#ansible-playbook playbooks/myrole.yml

 

Role tree Structure:

After creating the roles directories and all files, the tree view will display like the example below.

Common role directory tree structure

                                                                                          

Liked it? Take a second to support Venkata Narayana Yarava on Patreon!

Venkata Narayana Yarava

Venkata Narayana Yarava is an experienced Cloud DevOps Engineer with exposure to various technologies. He has a huge passion for automation technologies, specifically DevOps Tools, OpenStack, AWS, Linux, and open-source software. If you enjoy our articles please support us and make a small contribution: Buy Us A Coffee