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.
More on Project Structure and Design
What does a production Ansible repository look like?BeginnerMonorepo or separate repos for roles? How do you decide?IntermediateHow do you structure inventories for dev, stage and prod?BeginnerHow do you prevent someone from running a playbook against production by accident?IntermediateWhat does "thin playbooks, fat roles" mean?IntermediateWhat makes a well-designed role?Intermediate