Role directory structure
An Ansible role has a defined directory structure with seven main standard directories. You must include at least one of these directories in each role. You can omit any directories the role does not use. For example:
By default, Ansible will look in most role directories for amain.ymlfile for relevant content (alsomain.yamlandmain):
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 (seeUsing variablesfor 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 (seeUsing variablesfor 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.
You can add other YAML files in some directories, but they won’t be used by default. They can be included/imported directly or specified when usinginclude_role/import_role.
For example, you can place platform-specific tasks in separate files and refer to them in thetasks/main.ymlfile:
Or call those tasks directly when loading the role, which bypasses themain.ymlfiles:
Directoriesdefaultsandvarsmay also includenested directories. If your variables file is a directory, Ansible reads all variables files and directories inside in alphabetical order. If a nested directory contains variables files as well as directories, Ansible reads the directories first. Below is an example of avars/maindirectory: