Role

Contains the related Tasks and Play and variables that related to that role.

Ansible role let you automatically load related vars, files and handlers based on a known file structure

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #
            bar.txt       #  <-- files for use with the copy resource
            foo.sh        #  <-- script files for use with the script resource
        vars/             #
            main.yml      #  <-- variables associated with this role
        defaults/         #
            main.yml      #  <-- default lower priority variables for this role
        meta/             #
            main.yml      #  <-- role dependencies
        library/          # roles can also include custom modules
        module_utils/     # roles can also include custom module_utils
        lookup_plugins/   # or other types of plugins, like lookup in this case

    webtier/              # same kind of structure as "common" was above, done for the webtier role
    monitoring/           # ""
    fooapp/               # ""

By default, Ansible will look in most role directories for a main.yml file for relevant content (also main.yaml and main):

  • tasks/main.yml - A list of tasks that the role provides to the play for execution.
  • handlers/main.yml - handlers that are imported into the parent play for use by the role or other roles and tasks in the play.
  • defaults/main.yml - very low precedence values for variables provided by the role (see Using Variables for more information). A role’s own defaults will take priority over other role’s defaults, but any/all other variable sources will override this.
  • vars/main.yml - high precedence variables provided by the role to the play (see Using Variables for more information).
  • files/stuff.txt - one or more files that are available for the role and it’s children.
  • templates/something.j2 - templates to use in the role or child roles.
  • meta/main.yml - metadata for the role, including role dependencies and optional Galaxy metadata such as platforms supported. This is required for uploading into galaxy as a standalone role, but not for using the role in your play.

How to use Roles

  1. Create your folder: roles/role_name
  2. Create the folders inside roles/role_name depending on what you need. For example tasks: roles/role_name/tasks
  3. Create the main file: roles/role_name/tasks/main.yml
  4. Include the roles in roles: on the main playbook. It will automatically execute the tasks in roles/role_name/tasks

Example:

Create roles/db/tasks

- name: install postgres
  apt: 
    purge: yes
    name: postgresql

Create roles/webservers/tasks

- name: Update the cache
  apt: 
    purge: yes
    name: apache2
    state: present

- name: start apache2
  ansible.builtin.systemd_service:
    name: apache2
    enabled: true
    state: started

In main playbook (site.yml)

---
- name: Install required dependencies for web app
  hosts: app
  become: yes
  roles:
  - webservers


- name: Install required dependencies for db
  hosts: db
  become: yes
  roles:
  - db

Use inventory file (staging.yml)

app:
  hosts:
    app1: 
      ansible_host: 192.168.60.4
    app2: 
      ansible_host: 192.168.60.5

db:
  hosts:
    app3:
      ansible_host: 192.168.60.6

server:
  children:
    db:
    app:
  vars:
    ansible_user: vagrant
    ansible_ssh_private_key_file: ~/.vagrant.d/insecure_private_key

We can then run using

 ansible-playbook -i staging.yml site.yml