Package and reuse content with roles

An role is a portable, self-contained unit of automation that bundles tasks, variables, templates, and handlers. Roles allow you to organize and reuse automation content to fit specific system needs.

Instead of creating huge playbooks with hundreds of tasks, you can use roles to break the tasks apart into smaller, more discrete units of work.

Create a role

Create an Ansible role by using the ansible-galaxy CLI tool to build reusable automation components.

About this task

The Ansible Automation Platform bundle includes the Ansible Galaxy CLI tool. Access role-specific commands from the role subcommand:

ansible-galaxy role init <role_name>

Standalone roles outside of Collections are supported. Create new roles inside a Collection to take advantage of the features Ansible Automation Platform has to offer.

Procedure

  1. In a terminal, navigate to the roles directory inside a collection.
  2. Create a role called my_role inside the collection:
    $ ansible-galaxy role init my_role

    The collection now includes a role named my_role inside the roles directory, as you can see in this example:

    ~/.ansible/collections/ansible_collections/<my_namespace>/<my_collection_name>
        ...
        └── roles/
            └── my_role/
                ├── .travis.yml
                ├── README.md
                ├── defaults/
                │   └── main.yml
                ├── files/
                ├── handlers/
                │   └── main.yml
                ├── meta/
                │   └── main.yml
                ├── tasks/
                │   └── main.yml
                ├── templates/
                ├── tests/
                │   ├── inventory
                │   └── test.yml
                └── vars/
                    └── main.yml
  3. A custom role skeleton directory can be supplied by using the --role-skeleton argument. This allows organizations to create standardized templates for new roles to suit their needs.
    $ ansible-galaxy role init my_role --role-skeleton ~/role_skeleton

    This creates a role named my_role by copying the contents of ~/role_skeleton into my_role. The contents of role_skeleton can be any files or folders that are valid inside a role directory.