animind - Fotolia
Ansible roles are self-contained packages designed to promote modularization and code reuse within the configuration management tool.
But what are Ansible roles exactly? They're groupings of variables, tasks, handlers and other components within a standard file structure. These roles provide a way to break out an Ansible playbook into multiple independent files. In addition to promoting reuse, roles offer an easy way to share Ansible content among multiple users.
The following Ansible role example highlights the benefits of the feature.
An admin wants to migrate multiple and separate ad hoc automation scripts to Ansible. Each script performs specific tasks, such as configuring the Secure Socket Shell service to meet the company's requirements. This task is necessary for some servers but not for others. How does the admin programmatically sort the servers in Ansible to reap the efficiency benefits of configuration management?
While one option is to have scripts that call other scripts, that model can become complex and difficult to manage reliably. With Ansible roles, the admin can place different configuration items and tasks into a single playbook and deploy only those items that are defined in the role for a particular server type; this ensures a machine only receives relevant configuration items.
Each Ansible role can run in isolation. However, variables, which denote differences between systems, can be used across Ansible configurations. For example, an administrator must maintain a standard HTTP setup across machines, but different systems have different requirements -- one needs Apache and PHP, while another needs MySQL. The Ansible role includes templates for configuration files, as well as several variables to account for these individual needs.
How and when to split roles is an art form in itself, and Ansible users will get better at it with experience. Once you understand what Ansible roles are and how they work independently, be sure to research the commands, such as init, to create and manage them. Get familiar with other key concepts, such as the correct way to assign a role's name and metadata, to master role creation.
Dig Deeper on Configuration Management and DevOps
Related Q&A from Stuart Burns
Both Ansible and Ansible Tower offer enterprises a wealth of resources for config management, but they aren't equals. Assess their differences in ... Continue Reading
Even though Ansible has its roots in open source software and Linux, is it possible to use the configuration management tool for Windows environments? Continue Reading
An Ansible playbook boosts admin productivity when they manage configurations across machines. Learn how playbooks work and how to avoid live errors. Continue Reading