Lab 06: Ansible Foundations

Time: 45 minutes | Level: Architect | Docker: docker run -it --rm ubuntu:22.04 bash

Overview

Ansible is the industry-standard agentless automation tool that uses SSH and YAML playbooks to manage infrastructure at scale. In this lab you will install Ansible, explore inventory formats, run ad-hoc commands, configure ansible.cfg, and write your first playbook — all verified inside a Docker container.

Prerequisites

  • Docker installed and running

  • Basic YAML knowledge

  • Familiarity with SSH concepts


Step 1: Install Ansible via pip3

docker run --rm ubuntu:22.04 bash -c "
apt-get update -qq 2>/dev/null
apt-get install -y -qq python3-pip python3 2>/dev/null
pip3 install ansible --quiet 2>/dev/null
ansible --version
"

📸 Verified Output:

💡 Tip: pip3 install ansible installs the full Ansible package including ansible-core. For minimal installs use pip3 install ansible-core. Always pin versions in production: pip3 install ansible==9.x.x.


Step 2: Inventory Formats — INI and YAML

📸 Verified Output:

💡 Tip: Both formats are equivalent. YAML is preferred for large inventories because it supports nesting and is easier to generate programmatically. INI is simpler for small static inventories.


Step 3: ansible-inventory — Inspect Your Inventory

📸 Verified Output:

💡 Tip: Use ansible-inventory --list --yaml for YAML output or --graph for a visual tree. This is invaluable for debugging dynamic inventories.


Step 4: Configure ansible.cfg

📸 Verified Output:

💡 Tip: Ansible searches for ansible.cfg in this order: $ANSIBLE_CONFIG env var → ./ansible.cfg (current dir) → ~/.ansible.cfg/etc/ansible/ansible.cfg. Project-local configs override global ones.


Step 5: host_vars and group_vars

📸 Verified Output:

💡 Tip: host_vars override group_vars for the same variable name. This allows you to set defaults in group_vars and exceptions in host_vars. Note http_port is 8080 (host override) not 80 (group default).


Step 6: Ad-hoc Commands

📸 Verified Output:

💡 Tip: Use -m command for simple commands (no shell features), -m shell when you need pipes/redirects/variables. The command module is more secure and predictable.


Step 7: First Playbook — YAML Structure

📸 Verified Output:

💡 Tip: Every playbook has the structure: hosts (target), gather_facts (collect system info), become (privilege escalation), vars (variables), tasks (ordered list of actions). The register keyword captures task output for later use.


Step 8: Capstone — ansible --check and --diff Mode

Scenario: Your team needs to validate configuration changes before deploying to 50 production servers. Use --check (dry-run) and --diff (show changes) to audit what Ansible would change without touching anything.

📸 Verified Output:

💡 Tip: Always run --check --diff before deploying to production. This is your "preview" mode — it shows unified diffs for file changes without executing them. Add --limit web1 to test against a single host first.


Summary

Concept
Command/File
Purpose

Install

pip3 install ansible

Install Ansible via pip

Version

ansible --version

Show version and config info

INI inventory

[group]\nhost ansible_host=x

Simple static inventory

YAML inventory

all.children.group.hosts

Structured static inventory

Inspect inventory

ansible-inventory --list

View parsed inventory as JSON

Config file

ansible.cfg

Set defaults (forks, user, etc.)

Group variables

group_vars/groupname.yml

Variables for all hosts in group

Host variables

host_vars/hostname.yml

Variables for specific host

Ad-hoc ping

ansible -m ping all

Test connectivity

Ad-hoc command

ansible -m command -a 'cmd'

Run one-off command

Ad-hoc copy

ansible -m copy -a 'content= dest='

Copy content to file

Playbook structure

hosts/gather_facts/become/vars/tasks

YAML automation definition

Syntax check

ansible-playbook --syntax-check

Validate playbook YAML

Dry run

ansible-playbook --check --diff

Preview changes without applying

Last updated