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