Creating Ansible roles and galaxy
An Ansible role can be used to separate one significant Ansible playbook into reusable pieces called roles. A role defines a set of tasks that you want to run on your target hosts. To run these tasks on your hosts, you must reference the role in your Ansible playbook.
Ansible Galaxy is a repository for Ansible roles that are available to drop directly into your Playbooks to streamline your automation projects. A new sysadmin might start automating with Ansible in a matter of a few hours.
You can create your own roles or use existing roles from Ansible Galaxy.
Creating your own roles in Ansible
To streamline your Ansible playbook, you can decide to separate out playbook tasks by creating roles and referencing them in your playbook. Use it to automatically load related variables, files, tasks, handlers, and other Ansible artifacts based on a known file structure.
-
Identify the tasks in your playbook that you want to reuse across multiple hosts. For example, you can group tasks that you want to run on all your hosts, and tasks that you want to run on your web servers and your databases. Each group of tasks can become its own role.
-
Create the Ansible role structure in your GitHub repository. Roles must be stored in a
roles
directory relative to your Ansible playbook. The roles directory can have a subdirectory such as/roles/db/
describing the tasks in themain.yml
file.├── roles └── db └── tasks └── main.yml ├── playbook.yaml ├── README.md
-
Add the tasks that you want to run to a
main.yml
file. In the following example, you separate out the task to download the MySQL community repo from your main playbook and put it into amain.yml
file.- name: Download MySQL Community Repo get_url: url: https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm dest: /tmp
-
Reference the role in your Ansible playbook.
- name: deploy MySQL and configure the databases hosts: all remote_user: root roles: - db tasks: - shell: "hostname"
For more information about other files and conditions that you can add to your role, see the Ansible documentation.
Installing roles from Ansible Galaxy
You can choose to use existing roles from Ansible Galaxy in your playbook. Ansible Galaxy offers pre-packaged units of work from the Ansible community that are made available as roles and collections.
-
Browse the Ansible Galaxy repository to find the roles that you want.
-
Create a
requirements.yml
file where you specify all the roles that you need. For an overview of how to reference Ansible Galaxy roles, see the Ansible documentation. In the following example, you want to use the roleandrewrothstein.kubectl
from Ansible Galaxy.--- roles: - name: andrewrothstein.kubectl
-
Add a
roles
folder to your GitHub repository that is relative to the playbook, and store therequirements.yml
file in this folder as shown in this example.├── roles └── requirements.yml ├── playbook.yaml ├── README.md
-
Reference the role in your Ansible playbook. In this example, the role with the name
andrewrothstein.kubectl
is used.- hosts: all roles: - role: andrewrothstein.kubectl
For more information about Ansible playbook examples, see that IBM provided Ansible playbook