Introduction
Ansible, a popular automation tool, simplifies the orchestration and management of infrastructure, configurations, and applications. It employs a modular structure, including roles, to organize and reuse automation tasks. However, to ensure consistency and prevent errors in your Ansible playbooks, it's crucial to follow best practices and guidelines. In this article, we'll explore Ansible Error 106, "role-name," in [Ansible-Lint](/articles/ansible-lint) which enforces specific rules for naming roles. We'll dive into why adhering to these naming conventions is important and how to create role names that comply with Ansible standards.
The Problem: Non-compliant Role Names
Ansible Error 106, "role-name", focuses on enforcing a set of rules regarding role names. These rules are designed to ensure uniformity and clarity in role naming conventions. Non-compliant role names can create confusion, reduce maintainability, and lead to issues when working with Ansible playbooks. Here are the key requirements for role names:
1. Lowercase Alphanumeric Characters: Role names should contain only lowercase alphanumeric characters, which are letters (a-z) and digits (0–9).
2. Underscore Allowed: The underscore character (\_) is permitted in role names.
3. Start with an Alphabetic Character: Role names must start with an alphabetic character (a-z).
Problematic Code Example:
``yaml
---
- name: Example playbook
hosts: localhost
roles:
- 1myrole # <- Does not start with an alphabetic character.
- myrole2[*^ # <- Contains invalid special characters.
- myRole_3 # <- Contains uppercase alphabetic characters.
`
In the problematic code above, role names do not adhere to the specified naming conventions. The first role name starts with a numeric character, the second contains special characters, and the third includes uppercase alphabetic characters.
Output:
`bash
WARNING Listing 1 violation(s) that are fatal
syntax-check[specific]: the role '1myrole' was not found in /Users/lberton/prj/gitlab/ansible-pilot/troubleshooting/roles:/Users/lberton/.cache/ansible-compat/680d12/roles:/Users/lberton/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/Users/lberton/prj/gitlab/ansible-pilot/troubleshooting
106.yml:5:7
Rule Violation Summary
count tag profile rule associated tags
1 syntax-check[specific] min core, unskippable
Failed: 1 failure(s), 0 warning(s) on 1 files.
`
Correcting Role Names
To address Ansible Error 106 and create role names that conform to the rules, follow these guidelines:
1. Start with an Alphabetic Character:
`yaml
---
- name: Example playbook
hosts: localhost
roles:
- myrole1 # <- Starts with an alphabetic character.
`
2. Use Only Alphanumeric Characters and Underscores:
``yaml
---
- name: Example playbook
hosts: localhost
roles:
- myrole2 # <- Contains only alphanumeric charac