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