Sei sulla pagina 1di 1

Ansible Roles Cheatsheet

Roles provide a way of automatically loading To invoke a role in a playbook, you may use
certain var_files, tasks, and handlers based on importrole, includerole, or use the role keyword.
a known file structure.
Example:
Roles also make sharing of configuration ---
templates easier. - hosts: webservers
tasks:
- name: Include common role with variables
Roles require a particular directory structure. - include_role:
name: common
vars:
Role definitions must contain at least one of common_dist: centos
the noted directories: common_size: medium
- name: Include webserver role from non-standard path
include_role:
name: /opt/roles/webserver
in /path /to/role:
Example using two roles in a playbook using various
keywords as needed:
tasks - Contains the main list of tasks to be executed by
the role. ---
- hosts: webservers
handlers - Contains handlers, which may be used by this roles:
role or even anywhere outside this role. - common
- role: foo_app_instance
defaults - Default variables for the role (see Variables for vars:
more information). dir: '/opt/a'
vars - Other variables for the role (see Variables for more app_port: 5000
- role: foo_app_instance
information). vars:
files - Contains files which can be deployed via this role. dir: '/opt/b'
app_port: 5001
templates - Contains templates which can be deployed
via this role.
meta - Defines some meta data for this role. See below When a role is called in a playbook, Ansible looks for
for more details. the role definition in ${PWD}/roles/<role_name>

If ${PWD}/roles does not contain the sought role, then


Unused directories need not exist. /etc/ansible/roles is checked.
The "roles_path" configuration in ansible.cfg contains a
With the exception of templates and files, each colon delimited list of paths where roles are searched
directory must include a main.yml if that for by ansible.
directory is being used. The full path to a role may also be specified with the role
keyword to use a non-default path.
main.yml serves as the entry point for the role.
Example:

Files in the tasks, templates, and files directories ---


may be referenced without path within the role. - hosts: webservers
roles:
- role: '/path/to/my/roles/common'

Potrebbero piacerti anche