All exams← All Red Hat Certified Engineer (RHCE) exam (EX294) notes

Managing Ansible Roles: Red Hat Certified Engineer (RHCE) exam (EX294) revision notes

Revision notes

## Managing Ansible Roles

Ansible roles are a fundamental concept for organizing and reusing Ansible content. They provide a standardized directory structure that groups related tasks, handlers, variables, templates, files, and metadata into a cohesive unit. This modular approach significantly improves the manageability and scalability of complex automation projects.

## Why Use Roles?

  • Modularity and Reusability: Roles allow you to encapsulate specific functionalities (e.g., "install web server," "configure database") that can be easily reused across multiple playbooks and projects.
  • Readability and Maintainability: By separating different components into their designated directories, roles make playbooks easier to read, understand, and maintain.
  • Sharing: Roles are the standard way to share Ansible content, often via Ansible Galaxy.
  • Organization: They enforce a consistent structure for your automation code, making collaboration simpler.

## Standard Role Directory Structure

A typical role directory structure looks like this:

  • `roles/my_role/`
  • `tasks/main.yml`: Contains the main list of tasks for the role.
  • `handlers/main.yml`: Contains handlers that can be notified by tasks.
  • `vars/main.yml`: Defines variables specific to this role, with higher precedence than `defaults`.
  • `defaults/main.yml`: Defines default variables for the role, with the lowest precedence. These are easily overridden.
  • `files/`: Stores static files that the role might deploy (e.g., configuration files).
  • `templates/`: Stores Jinja2 templates that the role renders and deploys.
  • `meta/main.yml`: Contains metadata about the role, including author information, license, and role dependencies.
  • `library/`: For custom modules.

## Applying Roles in Playbooks

Roles are applied within a playbook using the `roles:` keyword.

```yaml

  • name: Apply web server role

hosts: webservers

roles:

  • web_server_role

```

For more dynamic or conditional role application, `include_role` (dynamic) and `import_role` (static) modules can be used. `import_role` processes the role at playbook parsing time, while `include_role` processes it at runtime, allowing for loops and conditionals.

## Role Variable Precedence

Understanding variable precedence is crucial:

1. `defaults/main.yml`: Lowest precedence. Easily overridden.

2. `vars/main.yml`: Higher precedence than `defaults`.

3. Playbook variables: Variables defined directly in the playbook.

4. `extra_vars`: Variables passed via `ansible-playbook -e "key=value"`, highest precedence.

## Ansible Galaxy

Ansible Galaxy is a public repository for sharing and finding roles. You can initialize a new role structure with `ansible-galaxy init my_new_role` and install roles from Galaxy using `ansible-galaxy install username.role_name`.

Key facts

Flashcards

What is the primary purpose of an Ansible role?
To organize and reuse Ansible content (tasks, handlers, variables, templates, files) into a standardized, modular structure.
tap to reveal
Which file in a role defines variables with the lowest precedence?
`defaults/main.yml`
tap to reveal
Where would you define role-specific variables that should override `defaults` but be overridden by playbook variables?
`vars/main.yml`
tap to reveal
How do you apply a role named `web_server` in an Ansible playbook?
Using the `roles:` keyword: ```yaml - hosts: webservers roles: - web_server ```
tap to reveal
What is the difference between `import_role` and `include_role`?
`import_role` is static (processed at playbook parsing time), while `include_role` is dynamic (processed at runtime, allowing for loops and conditionals).
tap to reveal
Where are role dependencies declared?
In the `meta/main.yml` file within the role's directory.
tap to reveal
Which command is used to create a new role's directory structure?
`ansible-galaxy init <role_name>`
tap to reveal
What is the main entry point for tasks within an Ansible role?
`tasks/main.yml`
tap to reveal
Practice questions

Managing Ansible Roles: 5 practice questions

Q1. Which command is used to create the standard directory structure for a new Ansible role named `webserver`?
  • A.  ansible new-role webserver
  • B.  ansible-role init webserver
  • C.  ansible-galaxy init webserver
  • D.  ansible-playbook --create-role webserver
Show answer & explanation
✓ Answer: C. ansible-galaxy init webserver
The `ansible-galaxy` command with the `init` action is the standard tool for scaffolding a new Ansible role with its correct directory structure.
Q2. In an Ansible role, where should you define default variable values that have the lowest precedence and are intended to be easily overridden by the user?
  • A.  defaults/main.yml
  • B.  meta/main.yml
  • C.  tasks/main.yml
  • D.  vars/main.yml
Show answer & explanation
✓ Answer: A. defaults/main.yml
The `defaults/main.yml` file is for variables with the lowest precedence, making it the intended place for default values that users can easily override.
Q3. How do you specify that one Ansible role depends on another, ensuring the dependency is executed automatically before the main role?
  • A.  List the dependency using the `depends_on` keyword in `tasks/main.yml`.
  • B.  Use the `import_role` task at the beginning of the playbook's `pre_tasks` section.
  • C.  Define the dependency within the `dependencies` list in the `meta/main.yml` file.
  • D.  Create a `dependencies.yml` file in the role's root directory.
Show answer & explanation
✓ Answer: C. Define the dependency within the `dependencies` list in the `meta/main.yml` file.
The `meta/main.yml` file is the correct and standard place to define role dependencies. Ansible will automatically resolve and execute these before the dependent role.
Q4. A playbook needs to install multiple roles defined in a file named `requirements.yml`. Which command correctly installs all roles listed in this file?
  • A.  ansible-playbook --install-roles requirements.yml
  • B.  ansible-galaxy get -f requirements.yml
  • C.  ansible-galaxy install -r requirements.yml
  • D.  ansible install --from-file requirements.yml
Show answer & explanation
✓ Answer: C. ansible-galaxy install -r requirements.yml
The `ansible-galaxy install` command with the `-r` (or `--role-file`) flag is the standard way to process a requirements file and install the roles it defines.
Q5. What is a key difference between using `include_role` and `import_role` to execute a role within a play?
  • A.  `import_role` must be used at the top level of a play, while `include_role` can only be used inside a block.
  • B.  `import_role` is dynamic and evaluated at runtime, allowing for conditional execution.
  • C.  `include_role` is dynamic and processed during execution, while `import_role` is static and pre-processed when the playbook is parsed.
  • D.  `include_role` is static and parsed when the playbook is loaded, making it faster.
Show answer & explanation
✓ Answer: C. `include_role` is dynamic and processed during execution, while `import_role` is static and pre-processed when the playbook is parsed.
`import_role` is static; Ansible pre-processes it at parse time. `include_role` is dynamic; it is processed during the execution of the play, allowing for conditional inclusion.
Test yourself
See the Red Hat Certified Engineer (RHCE) exam (EX294) page

More Red Hat Certified Engineer (RHCE) exam (EX294) topics