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

Using Advanced Ansible Features: Red Hat Certified Engineer (RHCE) exam (EX294) revision notes

Revision notes

## Using Advanced Ansible Features

The Red Hat Certified Engineer (RHCE) exam (EX294) requires proficiency in advanced Ansible features to manage complex environments efficiently and securely. These features promote reusability, security, and robust automation, moving beyond basic task execution to enterprise-grade configuration management.

## Ansible Roles

Ansible Roles are the standard way to organize Ansible content (tasks, handlers, variables, templates, files, and metadata) into reusable, self-contained units. They enforce a specific, well-defined directory structure, making playbooks more modular, readable, and shareable across projects and teams. You can initialize a role structure using `ansible-galaxy init <role_name>`. Roles are activated in playbooks using the `roles:` directive, simplifying complex playbook logic.

## Ansible Vault

Ansible Vault is crucial for securing sensitive data like passwords, API keys, or SSL certificates within your Ansible projects. It encrypts files, variables, or entire directories using strong AES256 encryption. Key commands include `ansible-vault create`, `ansible-vault edit`, `ansible-vault view`, `ansible-vault encrypt`, and `ansible-vault decrypt`. To use vault-encrypted files or variables in a playbook, you must provide the vault password, typically via the `--ask-vault-pass` command-line option or by specifying a vault password file with `--vault-password-file <path>`.

## Advanced Playbook Control

  • Error Handling: For robust automation, Ansible offers several error handling mechanisms. The `block`, `rescue`, and `always` keywords allow you to define a set of tasks (`block`) to run, tasks to execute if an error occurs within the block (`rescue`), and tasks that always run regardless of success or failure (`always`). You can also use `ignore_errors: true` to skip failures for specific tasks or `failed_when` to define custom failure conditions based on task output or registered variables.
  • Delegation: The `delegate_to` keyword allows a task to be executed on a different host than the one it's currently iterating over. For example, a task might run on the Ansible control node or a specific management host instead of the target inventory host. `run_once: true` ensures a task executes only once, typically on the first host in the current batch, even if multiple hosts are targeted. This is useful for tasks that should only happen a single time, like creating a database or updating a load balancer.
  • Conditionals and Loops: Advanced usage includes complex `when` conditions with `and`/`or` operators and nested loops using `loop_control` for specific iteration behaviors, such as `label` or `pause`.

## Dynamic Inventories

Dynamic Inventories allow Ansible to retrieve inventory information from external sources (e.g., cloud providers like AWS, Azure, VMware, or CMDBs) at runtime, rather than relying on static `ini` or `YAML` files. This is achieved using inventory plugins. You specify the plugin configuration in a YAML file or use a script that outputs JSON, allowing your inventory to be automatically updated as your infrastructure changes, providing flexibility and scalability.

Key facts

Flashcards

What are Ansible Roles primarily used for?
To organize Ansible content (tasks, handlers, variables, etc.) into reusable, self-contained, and structured units.
tap to reveal
How do you encrypt sensitive data like passwords in Ansible?
Using Ansible Vault with commands like `ansible-vault encrypt <file>` or `ansible-vault create <file>`.
tap to reveal
What is the purpose of the `delegate_to` keyword in an Ansible task?
To execute the task on a different host (e.g., the control node or another management host) than the current target host from the inventory.
tap to reveal
Explain the `block`, `rescue`, and `always` keywords in Ansible.
`block` defines a set of tasks to run; `rescue` tasks run if any task in the block fails; `always` tasks run regardless of success or failure of the block.
tap to reveal
How do dynamic inventories work in Ansible?
They use inventory plugins or scripts to retrieve host information from external sources (like cloud providers or CMDBs) at runtime, keeping inventory up-to-date.
tap to reveal
What Ansible command is used to initialize the standard directory structure for a new role?
`ansible-galaxy init <role_name>`
tap to reveal
How do you provide the vault password when running an Ansible playbook that uses encrypted files?
By using the `--ask-vault-pass` option or by specifying a vault password file with `--vault-password-file <path>`.
tap to reveal
What does `run_once: true` achieve in an Ansible task?
It ensures the task is executed only once, typically on the first host in the current batch, even if multiple hosts are targeted by the playbook.
tap to reveal
Practice questions

Using Advanced Ansible Features: 5 practice questions

Q1. Which command correctly initializes the standard directory structure for a new Ansible role named 'webserver'?
  • A.  ansible-role create webserver
  • B.  ansible-galaxy role init webserver
  • C.  mkdir -p roles/webserver/{tasks,handlers,vars,defaults,meta}
  • D.  ansible-playbook --init-role webserver
Show answer & explanation
✓ Answer: B. ansible-galaxy role init webserver
The `ansible-galaxy role init` command is the standard, automated method for creating the boilerplate directory structure and files for a new Ansible role.
Q2. To run a playbook that uses a vault-encrypted file, which command-line flag prompts the user to enter the vault password interactively?
  • A.  --vault-id @prompt
  • B.  --ask-vault-pass
  • C.  --vault-password-prompt
  • D.  --get-vault-password
Show answer & explanation
✓ Answer: B. --ask-vault-pass
The `--ask-vault-pass` flag is used with `ansible-playbook` to interactively prompt for the vault password at the terminal before execution begins.
Q3. In a Jinja2 template, a variable `packages` is a list: `['httpd', 'mariadb', 'php']`. Which expression correctly converts this list into the string 'httpd,mariadb,php'?
  • A.  {{ packages.join(',') }}
  • B.  {{ packages | to_string(',') }}
  • C.  {{ packages | list_to_csv }}
  • D.  {{ packages | join(',') }}
Show answer & explanation
✓ Answer: D. {{ packages | join(',') }}
The `join` filter is a standard Jinja2 and Ansible filter that concatenates the items of an iterable (like a list) into a single string using the specified separator.
Q4. When using the Ansible `uri` module to send a POST request with a JSON payload, which parameter is the most effective way to both format the body correctly and set the 'Content-Type' header to 'application/json'?
  • A.  method: 'JSON_POST'
  • B.  content_type: 'application/json'
  • C.  headers: { 'Accept': 'application/json' }
  • D.  body_format: 'json'
Show answer & explanation
✓ Answer: D. body_format: 'json'
Using `body_format: 'json'` automatically serializes the data provided in the `body` parameter to a JSON string and sets the correct `Content-Type` header for you.
Q5. How do you correctly reference the `firewalld` module from the `ansible.posix` collection within a task, using its Fully Qualified Collection Name (FQCN)?
  • A.  ansible::posix::firewalld
  • B.  ansible/posix/firewalld
  • C.  ansible.posix.firewalld
  • D.  posix.ansible.firewalld
Show answer & explanation
✓ Answer: C. ansible.posix.firewalld
The FQCN syntax is `namespace.collection_name.content_name`. For the `firewalld` module in the `ansible.posix` collection, this translates to `ansible.posix.firewalld`.
Test yourself
See the Red Hat Certified Engineer (RHCE) exam (EX294) page

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