Ansible is an open-source automation engine that automates software provisioning, configuration management, and application deployment. It is agentless, meaning no special software needs to be installed on the managed nodes, relying primarily on SSH for communication and Python for module execution.
## Control Node & Managed Nodes
The Control Node is the machine where Ansible is installed and executed. It's where you run `ansible` commands or `ansible-playbook`. Managed Nodes (also called hosts) are the target servers or devices that Ansible manages. Communication between the control node and managed nodes typically occurs over SSH.
## Inventory
The Inventory is a file (or set of files) that defines the managed nodes Ansible will operate on. It lists hosts, groups them, and can assign variables specific to hosts or groups. Inventories can be static (INI or YAML files) or dynamic (generated by scripts).
You can use the `ansible-inventory` command to view and manage your inventory.
## Modules & Tasks
Modules are the actual units of code Ansible executes on managed nodes. Each module is designed to perform a specific action, such as installing a package, managing a service, or copying a file. Modules are generally idempotent, meaning they can be run multiple times without causing unintended changes beyond the initial execution.
A Task is a call to an Ansible module with specific arguments. Tasks are defined within plays in a playbook.
## Playbooks & Plays
Playbooks are YAML formatted files that are the core of Ansible's configuration, deployment, and orchestration language. They contain one or more Plays.
A Play is a block within a playbook that maps a set of tasks to a specific group of hosts defined in the inventory. Each play targets a specific set of hosts and runs tasks on them.
```yaml
---
hosts: webservers
tasks:
ansible.builtin.dnf:
name: nginx
state: present
```
## Variables & Facts
Variables are used to store values that can be reused throughout playbooks, making them more dynamic and flexible. They can be defined in inventory, playbooks, or separate variable files.
Facts are pieces of information about managed nodes (e.g., operating system, IP addresses, memory) that Ansible gathers automatically using the `setup` module. These facts can be accessed as variables within playbooks and are useful for conditional logic or dynamic configuration.
## Plugins
Plugins extend Ansible's core functionality. Examples include: Connection plugins (how Ansible connects), Lookup plugins (fetch external data), Filter plugins (manipulate data), and Callback plugins (add new behaviors).
## Ansible Installation and Configuration
Ansible is an agentless automation engine that uses SSH to communicate with managed nodes. Understanding its installation and configuration is fundamental for the RHCE exam, even if Ansible is pre-installed in the exam environment.
## Installation
On Red Hat Enterprise Linux (RHEL) 8 or 9, Ansible is installed from the AppStream repository using `dnf`:
Managed nodes (the systems Ansible manages) do not require Ansible to be installed. They only need Python (version 3.6 or later is recommended for RHEL 8/9) and its dependencies, which are typically present by default.
## Configuration Files
Ansible's behavior is controlled by configuration files, primarily `ansible.cfg`. Ansible resolves its configuration using a specific order of precedence:
1. The path specified by the ANSIBLE_CONFIG environment variable.
2. `ansible.cfg` in the current working directory.
3. `.ansible.cfg` in the user's home directory (`~/.ansible.cfg`).
4. The system-wide configuration file at `/etc/ansible/ansible.cfg`.
Key sections within `ansible.cfg` include:
## Inventory
The inventory defines the managed hosts and groups that Ansible will operate on. By default, Ansible looks for `/etc/ansible/hosts`.
## SSH Connection and Authentication
Ansible uses OpenSSH for communication. Key parameters for SSH configuration include:
## Privilege Escalation (become)
Ansible uses the `become` directive to escalate privileges on managed nodes, typically to `root`. This is equivalent to using `sudo` or `su`.
These settings can be defined globally in `ansible.cfg`, in playbooks, or as host/group variables.
## 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:
## 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:
## Handlers and Tags
## Running Playbooks
To execute a playbook, use the `ansible-playbook` command:
`ansible-playbook [options] <playbook.yml>`
Key options include:
Understanding these components is crucial for automating system administration tasks effectively with Ansible.
## System Administration with Ansible Modules
The Red Hat Certified Engineer (RHCE) exam (EX294) heavily emphasizes using Ansible modules to automate common system administration tasks. Ansible is an agentless automation engine, meaning it doesn't require special software on managed nodes, relying on SSH for communication. Playbooks, written in YAML, define the desired state of your systems.
## Key Ansible Concepts
## Essential Modules for RHCE
You'll need proficiency with various modules to manage RHEL systems:
```yaml
dnf:
name: httpd
state: present
```
```yaml
systemd:
name: httpd
state: started
enabled: yes
```
Mastering these modules and understanding playbook structure is key to passing the RHCE exam.
## 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?
## Standard Role Directory Structure
A typical role directory structure looks like this:
## Applying Roles in Playbooks
Roles are applied within a playbook using the `roles:` keyword.
```yaml
hosts: webservers
roles:
```
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`.
## 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
## 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.
## Managing Secrets with Ansible Vault
Ansible Vault is a feature that allows you to encrypt sensitive data such as passwords, API keys, and other confidential information within your Ansible projects. This ensures that sensitive data is not stored in plain text, making your automation more secure, especially when storing playbooks in version control systems.
To interact with encrypted files, Ansible needs the vault password. You can provide it in several ways:
Encrypted files can be included in playbooks just like regular variable files. For example, if you have `group_vars/all/secrets.yml` encrypted with vault, Ansible will automatically prompt for the password (or use a configured password file) when it needs to access variables from that file. Variables defined within these encrypted files can then be used in your playbooks and templates.
## Creating and Using Templates in Ansible
Ansible templates are a powerful feature for creating customized configuration files dynamically on managed hosts. They allow you to embed variables and control structures directly into static file content, which is then rendered on the target system. Ansible uses the Jinja2 templating language for this purpose.
Templates are essential for:
The core of using templates in Ansible is the `template` module.
It takes a source template file (on the control node) and renders it on the managed node, substituting Jinja2 expressions with actual values.
Key parameters for the `template` module:
Understanding basic Jinja2 syntax is crucial for writing effective templates:
```jinja2
{% for user in users %}
User {{ user.name }} with UID {{ user.uid }}
{% endfor %}
```
```jinja2
{% if enable_feature %}
Feature is enabled.
{% else %}
Feature is disabled.
{% endif %}
```
Variables used within templates can originate from various Ansible sources, including inventory variables, `group_vars`, `host_vars`, `vars` sections in playbooks, `set_fact` tasks, and Ansible facts.