Interview Prep/Ansible/Project Structure and Design

How do you avoid duplicating configuration between environments?

IntermediateProductionProject Structure and Design
Answer

Layer variables. Put the shared baseline in group_vars/all or role defaults, and override only what differs in each environment's inventory. Use a dict of defaults combined with a small override dict so environments only list the deltas.

# inventories/prod/group_vars/web.yml
nginx_overrides:
  worker_connections: 8192
nginx_config: "{{ nginx_defaults | combine(nginx_overrides, recursive=true) }}"

If dev, stage and prod each repeat 100 lines of variables, the environments will drift apart. Review diffs between environments regularly as a sanity check.

All Ansible questions Roadmap: Ansible

More on Project Structure and Design