## 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.
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