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

Ansible Installation and Configuration: Red Hat Certified Engineer (RHCE) exam (EX294) revision notes

Revision notes

## 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`:

  • `sudo dnf install ansible`

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:

  • `[defaults]`: Global settings like inventory path, remote_user, private_key_file, and host_key_checking.
  • `[privilege_escalation]`: Settings for `become` (sudo, su, etc.).
  • `[ssh_connection]`: SSH-specific parameters.

## Inventory

The inventory defines the managed hosts and groups that Ansible will operate on. By default, Ansible looks for `/etc/ansible/hosts`.

  • The inventory can be specified using the `-i` option with `ansible` or `ansible-playbook` commands, or by setting the `inventory` parameter in `ansible.cfg`.
  • Inventory files can be in INI or YAML format.
  • They support defining groups, host variables, and group variables.

## SSH Connection and Authentication

Ansible uses OpenSSH for communication. Key parameters for SSH configuration include:

  • `remote_user`: The user Ansible connects as on managed nodes.
  • `private_key_file`: Path to the SSH private key for authentication.
  • Host key checking: A critical security feature. `host_key_checking = True` (default) verifies the identity of the remote host. For lab environments, it's sometimes set to `False` (e.g., in `ansible.cfg`) but should be `True` in production.

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

  • `become: yes` (or `become: true`) enables privilege escalation.
  • `become_method`: Specifies the method (default is `sudo`). Other options include `su`, `doas`, `pbrun`, `pfexec`.
  • `become_user`: Specifies the user to become (default is `root`).

These settings can be defined globally in `ansible.cfg`, in playbooks, or as host/group variables.

Key facts

Flashcards

How do you install Ansible on RHEL 8 or 9?
`sudo dnf install ansible`
tap to reveal
Do managed nodes need Ansible installed?
No, only Python (and its dependencies) is required on managed nodes.
tap to reveal
What is the default system-wide Ansible configuration file?
`/etc/ansible/ansible.cfg`
tap to reveal
What is the order of precedence for Ansible configuration files?
ANSIBLE_CONFIG env var > current directory `ansible.cfg` > `~/.ansible.cfg` > `/etc/ansible/ansible.cfg`
tap to reveal
What does `become: yes` achieve in an Ansible playbook?
It enables privilege escalation (e.g., using `sudo`) on the managed node.
tap to reveal
What is the default location for Ansible's inventory file?
`/etc/ansible/hosts`
tap to reveal
Which `ansible.cfg` parameter controls the verification of remote SSH host keys?
`host_key_checking`
tap to reveal
Practice questions

Ansible Installation and Configuration: 5 practice questions

Q1. Which command is the standard method to install the core Ansible automation engine on a Red Hat Enterprise Linux 9 system from the official repositories?
  • A.  dnf install ansible-core
  • B.  yum install ansible-engine
  • C.  rpm -ivh ansible.rpm
  • D.  pip install ansible
Show answer & explanation
✓ Answer: A. dnf install ansible-core
On RHEL 9, the `ansible-core` package provides the essential Ansible engine and is installed from the AppStream repository using the `dnf` package manager.
Q2. Ansible searches for its configuration file, `ansible.cfg`, in several locations. Which of the following has the highest precedence?
  • A.  The `ansible.cfg` file in the current working directory
  • B.  The `ansible.cfg` file in the user's home directory (`~/.ansible.cfg`)
  • C.  The `ansible.cfg` file in `/etc/ansible/`
  • D.  The path specified by the `ANSIBLE_CONFIG` environment variable
Show answer & explanation
✓ Answer: D. The path specified by the `ANSIBLE_CONFIG` environment variable
The `ANSIBLE_CONFIG` environment variable always takes the highest precedence, allowing a user to explicitly override all other configuration file locations.
Q3. In a standard INI-formatted Ansible inventory file, how do you correctly define a group named `dbservers` that will contain host definitions?
  • A.  "dbservers":
  • B.  <dbservers>
  • C.  group: dbservers
  • D.  [dbservers]
Show answer & explanation
✓ Answer: D. [dbservers]
In an INI-style inventory, group names are defined by enclosing them in square brackets. Hosts listed below this line, until the next group definition, belong to this group.
Q4. Which command should be used to create a new, encrypted YAML file named `secrets.yml` for storing sensitive variables using Ansible Vault?
  • A.  ansible-vault encrypt --new secrets.yml
  • B.  ansible-vault create secrets.yml
  • C.  ansible-vault new secrets.yml
  • D.  ansible-vault init secrets.yml
Show answer & explanation
✓ Answer: B. ansible-vault create secrets.yml
The `ansible-vault create` command is specifically used to create a new file and encrypt it simultaneously, prompting the user for a vault password.
Q5. You want to specify a custom directory, `/opt/ansible/custom_roles`, for Ansible to search for roles. Which parameter should be set in the `[defaults]` section of `ansible.cfg`?
  • A.  ansible_roles = /opt/ansible/custom_roles
  • B.  custom_roles_path = /opt/ansible/custom_roles
  • C.  roles_path = /opt/ansible/custom_roles
  • D.  role_dir = /opt/ansible/custom_roles
Show answer & explanation
✓ Answer: C. roles_path = /opt/ansible/custom_roles
The `roles_path` parameter within the `[defaults]` section of `ansible.cfg` defines a colon-separated list of filesystem paths to search for roles.
Test yourself
See the Red Hat Certified Engineer (RHCE) exam (EX294) page

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