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

Creating Plays and Playbooks: Red Hat Certified Engineer (RHCE) exam (EX294) revision notes

Revision notes

## Introduction to Ansible Playbooks

Ansible Playbooks are the core of Ansible's automation capabilities, allowing you to define, orchestrate, and execute complex IT workflows. They are written in YAML (YAML Ain't Markup Language) and provide a human-readable way to describe a desired state for your managed nodes. Playbooks are idempotent, meaning they can be run multiple times without causing unintended side effects, only making changes when necessary.

## Playbook Structure

A playbook is an ordered list of plays. Each play defines a set of tasks to be executed on a specific group of hosts from your inventory. The basic structure includes:

  • `hosts:`: Specifies the target hosts or host groups from your inventory where the play's tasks will run (e.g., `webservers`, `all`, `localhost`).
  • `become:`: A boolean (`yes`/`no`) or a dictionary to control privilege escalation (e.g., `sudo`, `su`). Often used with `become_user:` to specify which user to become (e.g., `root`).
  • `vars:`: Defines variables specific to the play, which can then be used in tasks within that play.
  • `tasks:`: An ordered list of actions to be performed on the target hosts. Each task calls an Ansible module.

## Tasks and Modules

Each task in a play executes a specific Ansible module with a set of arguments. Modules are reusable units of code that perform specific functions, such as installing packages, managing services, copying files, or executing commands. Common modules include:

  • `ansible.builtin.yum` (or `dnf`): Manages packages on RHEL-based systems.
  • `ansible.builtin.service`: Manages system services (start, stop, restart, enable).
  • `ansible.builtin.copy`: Copies files from the control node to managed nodes.
  • `ansible.builtin.file`: Manages files and directories (create, delete, change permissions).
  • `ansible.builtin.command` / `ansible.builtin.shell`: Executes arbitrary commands.

## Handlers and Tags

  • Handlers: Special tasks that are only triggered when explicitly `notify:`-ed by another task and when that task reports a change. They are typically used for actions like restarting services after a configuration file has changed. Handlers are defined in a `handlers:` section at the play level.
  • Tags: Allow you to logically group tasks or plays. You can then selectively run only specific parts of a playbook using the `--tags` or `--skip-tags` options with `ansible-playbook`.

## Running Playbooks

To execute a playbook, use the `ansible-playbook` command:

`ansible-playbook [options] <playbook.yml>`

Key options include:

  • `--check`: Perform a "dry run" to show what changes would be made without actually making them.
  • `--syntax-check`: Verify the playbook's YAML syntax.
  • `-i <inventory>`: Specify an inventory file.
  • `-l <limit>`: Limit execution to a subset of hosts.
  • `-v`, `-vv`, `-vvv`: Increase verbosity for more detailed output.
  • `--extra-vars "key=value"`: Pass additional variables at runtime.

Understanding these components is crucial for automating system administration tasks effectively with Ansible.

Key facts

Flashcards

What language are Ansible Playbooks written in?
YAML (YAML Ain't Markup Language)
tap to reveal
What is the primary purpose of the `hosts:` directive in a play?
To specify which hosts or host groups from the inventory the play's tasks will target.
tap to reveal
How do you enable privilege escalation (e.g., sudo) within an Ansible play?
By using `become: yes` (and optionally `become_user:` to specify the target user).
tap to reveal
What is an Ansible 'handler' and when is it executed?
A handler is a special task that is only executed when explicitly notified by another task, and only if the notifying task reports a change.
tap to reveal
Which `ansible-playbook` option allows you to test a playbook without making any actual changes?
`--check` (or `-C`)
tap to reveal
How can you run only a specific subset of tasks within a playbook?
By assigning `tags:` to tasks and then using the `--tags` option with `ansible-playbook`.
tap to reveal
Name two common Ansible modules used for managing packages and services on RHEL systems.
`ansible.builtin.yum` (or `dnf`) for packages and `ansible.builtin.service` for services.
tap to reveal
Practice questions

Creating Plays and Playbooks: 5 practice questions

Q1. In an Ansible playbook, what is the top-level element that defines a set of tasks to be executed on a specific group of hosts?
  • A.  Task
  • B.  Play
  • C.  Handler
  • D.  Module
Show answer & explanation
✓ Answer: B. Play
A playbook is a list of one or more plays. Each play maps a group of hosts, defined by the `hosts` keyword, to a set of tasks.
Q2. Which command is used to execute an Ansible playbook file named `configure-web.yml`?
  • A.  ansible-playbook configure-web.yml
  • B.  ansible-run configure-web.yml
  • C.  ansible configure-web.yml
  • D.  ansible-execute configure-web.yml
Show answer & explanation
✓ Answer: A. ansible-playbook configure-web.yml
The `ansible-playbook` command is the correct tool for running playbooks, while the `ansible` command is for executing ad-hoc commands.
Q3. Which directive, at the play or task level, is used to specify that tasks should be executed with elevated privileges (e.g., as root)?
  • A.  run_as: root
  • B.  sudo: yes
  • C.  elevate: true
  • D.  become: true
Show answer & explanation
✓ Answer: D. become: true
The `become: true` directive is the modern and correct way to instruct Ansible to use a privilege escalation method like sudo. The `sudo` directive is deprecated.
Q4. What is the primary purpose of a handler in an Ansible playbook?
  • A.  To define a variable that can be used globally.
  • B.  To execute a task only when explicitly notified by another task.
  • C.  To gather facts from the remote hosts before any tasks are run.
  • D.  To run a task at the very beginning of a play.
Show answer & explanation
✓ Answer: B. To execute a task only when explicitly notified by another task.
Handlers are special tasks that only run when a change is made by another task and a `notify` directive is triggered, ideal for restarting services.
Q5. A task uses the `ansible.builtin.service` module to ensure a service is started. If the playbook is run and the service is already running, what will be the reported state of that task?
  • A.  skipped
  • B.  changed
  • C.  ok
  • D.  failed
Show answer & explanation
✓ Answer: C. ok
Due to idempotence, if the service is already in the desired state (running), the task will report 'ok' and not 'changed' because no action was needed to achieve the state.
Test yourself
See the Red Hat Certified Engineer (RHCE) exam (EX294) page

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