Ansible: Roles

This blog receives a commission from affiliate partners through the affiliate links that we share with you in our posts. Although we receive a commission for using and linking their products, all our opinions and suggestions are unbiased.


  • 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)



#vi playbooks/pb1.yml
 - hosts: dev 
   remote_user: root 
     - 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}}
  name: {{pack1}}
  state: latest
  when: ansible_distribution=='Ubuntu'
   - start apache2

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

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

Create deploy file.

#cd /playbooks/roles/myrole/tasks

#vi deploy_app.yml

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

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

Create handlers file.

#vi roles/myrole/handlers/main.yml

- name: start apache
  name: apache2
  state: started

- name: start tomcat
  name: tomcat7
  state: started

- name: start mysql
  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


Venkata Narayana Yarava

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