Configuration Management

Automate infrastructure with Ansible and other tools.

Configuration management makes servers reproducible. Instead of “SSH in and click around”, you describe desired state (packages installed, services running, files present) and let tools apply it consistently across many machines.

Ansible Basics

Ansible is agentless: it uses SSH to connect and applies changes using modules.

# Install Ansible
$ pip install ansible

# Run ad-hoc command
$ ansible all -m ping

# Run playbook
$ ansible-playbook site.yml

Inventory (who are we managing?)

Inventory defines hosts and groups. Start simple with an INI inventory.

# inventory.ini
[webservers]
203.0.113.10 ansible_user=deploy

[db]
203.0.113.20 ansible_user=deploy

$ ansible -i inventory.ini all -m ping

Sample Playbook

---
- hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: started

Dry runs and diffs

Use check mode to preview changes and reduce risk.

$ ansible-playbook -i inventory.ini site.yml --check --diff

Best practices for scale

  • Idempotency: tasks should converge (running again = no changes)
  • Roles: organize by component (nginx, app, monitoring) for reuse
  • Variables: keep env-specific values in group_vars/host_vars
  • Secrets: use ansible-vault (never commit plaintext secrets)
  • Tags: allow partial runs (e.g., --tags nginx)

✅ Practice (30 minutes)

  • Create an inventory.ini and run ansible -m ping against a test VM.
  • Write a playbook that installs nginx and ensures it is enabled + running.
  • Run the playbook twice and confirm the second run reports “ok” with no changes.