Prefixing Role and Project Variables in Ansible

Prefix role variables with the role name and project variables with a project-specific prefix.

Problem

Without a role or project prefix it is not always clear where variables are used. Without a prefix there is also a chance of conflicting or duplicate variable names.

Context

In a typical inventory project many kinds of variables are created and used. Some variables belong to Ansible roles while others are introduced only in the inventory. This distinction can be confusing and may lead to mistakes.

Role variables follow a well-known naming convention that is checked by linters such as ansible-lint. Project variables, however, are defined in group_vars or host_vars and are not part of any role. These variables require an explicit project prefix to avoid clashes and to make their origin clear.

Solution

  1. Prefix every role variable with the role name, for example harbor_.
  2. Prefix every project variable that exists only in group_vars or host_vars with a project prefix, for example c2_.

Benefits

  • Makes the origin of each variable immediately visible.
  • Prevents accidental duplication or override of variables.
  • Aligns with community standards and enables automated linting for role variables.
  • Improves maintainability of large inventory projects.

Alternatives (Optional)

Using a generic prefix such as var_ for all variables reduces clarity and is not recommended.

Examples and Implementation

  1. harbor_hostname is a role variable for the c2platform.mgmt.harbor role. The prefix harbor_ follows the role name.
  2. In the C2 inventory project c2platform/ansible the prefix c2_ is used for project variables, for example c2_cacerts2_ca_dir in group_vars/all/smallca.yml.
  3. In the RWS inventory project c2platform/rws/ansible-gis the prefix gs_ is used. See Configuring a Web Proxy Server Example for MS Windows Hosts for the use of gs_proxy_regedit.

Last modified August 11, 2026: guideline variable prefix PHX-661 (32b7e68)