Ansible Naming Conventions
By Sudheer S
Purpose Of Having Naming Conventions For An Ansible Project
- Consistency: Adopting a naming convention standardizes naming across the project and organization. This makes it easier for developers to switch between projects. Typically, an organization with an infrastructure team will have several Ansible projects and source code repositories. A developer working on one such Ansible project can seamlessly switch to another given a standard naming convention.
- Error Reduction: With improper naming there can be pitfalls. For instance, using hyphens(
-
) in variable names can cause parsing errors since Ansible might interpret them as the minus operator. Standardizing naming for variables can help avoid collisions. - Readability: With a standard naming convention, it helps developers to find variables, files and tasks quickly.
Naming Conventions For Ansible Roles And Playbooks
- Role name : use lowercase letters and hyphens to separate words: For example,
web-server
ordatabase-backup
. - Task file name: user lowercase letters and underscores to separate words. When statements such as include
are used, it is convenient to have file names without hyphens. For example:
install_web_ubuntu.yml
- YAML file extension:
yml
insteadyaml
. To be consistent and succinct. - Task name: start with a verb: Use an action verb at the beginning of the task name to indicate what the role does. For example, Install Nginx or Configure firewall. Start with a capital letter. No need to end with a period for a few words of task description.
- Variable name: lowercase letters and underscores to separate words. The variable must start with the role
name. For example, if the role name is nginx, the variable name should be
nginx_default_hostname
.
Content Guide
- Always include a README file for the role. Describe in detail how to use the role. Create a table to show the role variables and their defaults.
- Include automated tests using Molecule.