Ansible Automation
Use this for repeatable server and infrastructure operations with Ansible.
Project Fit Check
Before changing automation:
- Read inventories, group vars, host vars, roles, collections, vault policy, runbooks, and CI jobs.
- Identify target hosts, privilege escalation model, SSH user, environment, and whether changes are safe to run repeatedly.
- Preserve existing variable naming, role layout, and collection versions.
- Do not run against production without confirmation of inventory and limit.
- Treat secrets as vault or external secret-manager material, never plain vars.
Playbook Rules
- Make tasks idempotent; repeated runs should converge without surprise.
- Use handlers for service reloads/restarts.
- Prefer modules over shell commands.
- If shell is necessary, add
creates,removes,changed_when, orfailed_when. - Keep templates minimal and render from explicit vars.
- Use tags for operational subsets only when they remain safe alone.
Inventory Rules
- Keep environments explicit: staging, production, homelab, or similar.
- Use group vars for shared policy and host vars for true host differences.
- Avoid embedding secrets or personal paths in inventory.
- Document required external vars and vault files.
Verification
ansible-playbook --syntax-check playbook.yml
ansible-playbook --check --diff playbook.yml
ansible-playbook --limit <safe-target> playbook.yml
Use check mode cautiously; not every module predicts changes perfectly.
Red Flags
- raw shell task where a module exists
- production inventory selected by default
- secret in plaintext YAML
- restart on every run
- unbounded
hosts: allfor risky changes - task depends on local machine state without documenting it