Skip to content

Commit 2349f65

Browse files
committed
feat: added sample code for configuration heirarchy
1 parent 4cd0818 commit 2349f65

File tree

23 files changed

+355
-1
lines changed

23 files changed

+355
-1
lines changed

_posts/2025-12-10-how-we-used-configuration-heirarchies-to-support-scale-of-1million+-users-per-day-in-our-saas-infra.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ all:
181181
182182
### converged configuration
183183
184-
convergence of configuration came out as a side effect of the `inventory/hosts` pattern that was gave with `parent/child` relationship
184+
convergence of configuration came out as a side effect of the `inventory/hosts` pattern that came with `parent/child` relationship
185185

186186
ansible merges configuration finally at [per host level](https://docs.ansible.com/projects/ansible/latest/inventory_guide/intro_inventory.html#inheriting-variable-values-group-variables-for-groups-of-groups) which you specify in your `tasks` file which can be observed as below
187187

@@ -284,6 +284,9 @@ now in the playbooks, you could define specific stacks together to be provisione
284284
# ansible-playbook -i inventories/DEV/dev01/user1 playbooks/aws/control-plane/create.yml
285285
```
286286

287+
## show me the code 🧑‍💻
288+
sample code to understand the pattern is available here ->
289+
287290
## value additions
288291

289292
✅ no config sprawl
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
# configuration heirarchies
2+
3+
this repo gives a sample of the configuration heirarchy pattern that we used to scale our infra code base for our app. blog post ->
4+
5+
## structure
6+
```text
7+
.
8+
├── inventories
9+
│   ├── DEV
10+
│   │   └── dev01
11+
│   │   ├── group_vars
12+
│   │   │   ├── DEV -> ../../../group_vars/DEV/
13+
│   │   │   └── dev01
14+
│   │   │   ├── db.yaml
15+
│   │   │   └── eks.yaml
16+
│   │   ├── hosts
17+
│   │   └── user1
18+
│   │   ├── group_vars
19+
│   │   │   ├── DEV -> ../../../../group_vars/DEV/
20+
│   │   │   ├── dev01 -> ../../group_vars/dev01/
21+
│   │   │   └── user1
22+
│   │   │   └── feature_flags.yaml
23+
│   │   └── hosts
24+
│   ├── group_vars
25+
│   │   ├── aws_us_east_1_pre_account
26+
│   │   │   └── common.yaml
27+
│   │   ├── aws_us_east_1_prod_account
28+
│   │   │   └── common.yaml
29+
│   │   ├── aws_us_east_2_pre_account
30+
│   │   │   └── common.yaml
31+
│   │   ├── aws_us_east_2_prod_account
32+
│   │   │   └── common.yaml
33+
│   │   ├── DEV
34+
│   │   │   └── eks.yaml
35+
│   │   ├── PRE
36+
│   │   │   └── eks.yaml
37+
│   │   └── PROD
38+
│   │   ├── eks.yaml
39+
│   │   └── feature_flags.yaml
40+
│   ├── PRE
41+
│   │   └── pre01
42+
│   │   ├── group_vars
43+
│   │   │   ├── PRE -> ../../../group_vars/PRE/
44+
│   │   │   └── pre01
45+
│   │   │   └── feature_flags.yaml
46+
│   │   └── hosts
47+
│   └── PROD
48+
│   └── prod
49+
│   ├── group_vars
50+
│   │   └── PROD -> ../../../group_vars/PROD/
51+
│   └── hosts
52+
├── playbooks
53+
├── README.md
54+
└── roles
55+
```
56+
57+
## how the heirarchy works
58+
- Dev env: DEV -> dev01 -> user1
59+
- Pre env: PRE -> pre01
60+
- Prod env: Prod -> prod
61+
62+
## how to view the inheritance of vars
63+
64+
- ensure to have ansible installed
65+
66+
- clone/fork the repo and navigate to `./code/configuration-heirarchies`
67+
68+
- for `DEV -> dev01`:
69+
```shell
70+
ansible-inventory -i inventories/DEV/dev01/hosts --host deploy_host_local
71+
# output:
72+
# {
73+
# "ansible_connection": "local",
74+
# "ansible_python_interpreter": "/usr/bin/env python3",
75+
# "eks_cluster_version": "1.34",
76+
# "eks_k8s_version": "1.34",
77+
# "eks_network_calico_version": "3.31.2",
78+
# "eks_nodes_ami_type": "AL2023_x86_64_STANDARD",
79+
# "eks_nodes_instance_types": [
80+
# "m7i-flex.xlarge"
81+
# ],
82+
# "eks_nodes_version": "1.34",
83+
# "eks_ssm_parameter_name": "/aws/service/eks/optimized-ami/{{ eks_cluster_version }}/amazon-linux-2023/x86_64/standard/recommended/release_version",
84+
# "rds_allocated_storage": 32,
85+
# "rds_engine_version": "15.7",
86+
# "rds_max_allocated_storage": 64,
87+
# "rds_parameter_group_family": "postgres15"
88+
# }
89+
```
90+
91+
- for `DEV -> dev01 -> user1`:
92+
```shell
93+
ansible-inventory -i inventories/DEV/dev01/user1/hosts --host deploy_host_local
94+
95+
# NOTES:
96+
# - notice 2 vars: feature_flags_enable_feature_x, feature_flags_enable_feature_y got added into the list
97+
98+
# output:
99+
# {
100+
# "ansible_connection": "local",
101+
# "ansible_python_interpreter": "/usr/bin/env python3",
102+
# "eks_cluster_version": "1.34",
103+
# "eks_k8s_version": "1.34",
104+
# "eks_network_calico_version": "3.31.2",
105+
# "eks_nodes_ami_type": "AL2023_x86_64_STANDARD",
106+
# "eks_nodes_instance_types": [
107+
# "m7i-flex.xlarge"
108+
# ],
109+
# "eks_nodes_version": "1.34",
110+
# "eks_ssm_parameter_name": "/aws/service/eks/optimized-ami/{{ eks_cluster_version }}/amazon-linux-2023/x86_64/standard/recommended/release_version",
111+
# "feature_flags_enable_feature_x": false,
112+
# "feature_flags_enable_feature_y": false,
113+
# "rds_allocated_storage": 32,
114+
# "rds_engine_version": "15.7",
115+
# "rds_max_allocated_storage": 64,
116+
# "rds_parameter_group_family": "postgres15"
117+
# }
118+
```
119+
120+
- for `PRE -> pre01`
121+
```shell
122+
ansible-inventory -i inventories/PRE/pre01/hosts --host deploy_host_aws_us_east_1
123+
# output
124+
# {
125+
# "ansible_connection": "local",
126+
# "ansible_python_interpreter": "/usr/bin/env python3",
127+
# "eks_cluster_version": "v2",
128+
# "eks_k8s_version": "1.33",
129+
# "feature_flags_enable_feature_x": false,
130+
# "feature_flags_enable_feature_y": true
131+
# }
132+
```
133+
134+
- for `PROD -> prod`
135+
```shell
136+
ansible-inventory -i inventories/PROD/prod/hosts --host deploy_host_aws_us_east_1
137+
# output:
138+
# {
139+
# "ansible_connection": "local",
140+
# "ansible_python_interpreter": "/usr/bin/env python3",
141+
# "eks_cluster_version": "v2",
142+
# "eks_k8s_version": "1.33",
143+
# "feature_flags_enable_feature_x": true,
144+
# "feature_flags_enable_feature_y": true
145+
# }
146+
```
147+
148+
## how to view the parent-child relationship
149+
```shell
150+
ansible-inventory -i inventories/DEV/dev01/hosts --list
151+
ansible-inventory -i inventories/DEV/dev01/user1/hosts --list
152+
ansible-inventory -i inventories/PRE/pre01/hosts --list
153+
ansible-inventory -i inventories/PROD/prod/hosts --list
154+
155+
# output
156+
# {
157+
# "PRE": {
158+
# "children": [
159+
# "pre01"
160+
# ]
161+
# },
162+
# "_meta": {
163+
# "hostvars": {
164+
# "deploy_host_aws_us_east_1": {
165+
# "ansible_connection": "local",
166+
# "ansible_python_interpreter": "/usr/bin/env python3",
167+
# "eks_cluster_version": "v2",
168+
# "eks_k8s_version": "1.33",
169+
# "feature_flags_enable_feature_x": false,
170+
# "feature_flags_enable_feature_y": true
171+
# },
172+
# "deploy_host_aws_us_east_2": {
173+
# "ansible_connection": "local",
174+
# "ansible_python_interpreter": "/usr/bin/env python3",
175+
# "eks_cluster_version": "v2",
176+
# "eks_k8s_version": "1.33",
177+
# "feature_flags_enable_feature_x": false,
178+
# "feature_flags_enable_feature_y": true
179+
# }
180+
# }
181+
# },
182+
# "all": {
183+
# "children": [
184+
# "ungrouped",
185+
# "PRE"
186+
# ]
187+
# },
188+
# "aws_us_east_1_pre_account": {
189+
# "hosts": [
190+
# "deploy_host_aws_us_east_1"
191+
# ]
192+
# },
193+
# "aws_us_east_2_pre_account": {
194+
# "hosts": [
195+
# "deploy_host_aws_us_east_2"
196+
# ]
197+
# },
198+
# "pre01": {
199+
# "children": [
200+
# "aws_us_east_1_pre_account",
201+
# "aws_us_east_2_pre_account"
202+
# ]
203+
# }
204+
# }
205+
```
206+
207+
## at what levels is variables can be placed
208+
209+
- DEV account
210+
```text
211+
# NOTE:
212+
- this means the vars are inherited from DEV -> dev01 -> user1 where it allows users to set only required vars at each level
213+
214+
DEV -> vars for account level
215+
dev01 -> vars for dev01 env
216+
user1 -> vars for dev01/user1 env
217+
```
218+
219+
- PRE account
220+
```text
221+
# NOTE:
222+
- this means the vars are inherited from PRE -> pre01 where it allows users to set only required vars at each level
223+
224+
PRE -> vars for account level
225+
pre01 -> vars for pre01 env
226+
```
227+
228+
- PROD account
229+
```text
230+
# NOTE:
231+
- this means the vars are inherited from PROD -> prod where it allows users to set only required vars at each level
232+
233+
PROD -> vars for account level
234+
prod -> vars for prod env
235+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../group_vars/DEV/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rds_parameter_group_family: postgres15
2+
rds_allocated_storage: 32
3+
rds_max_allocated_storage: 64
4+
rds_engine_version: "15.7"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
eks_cluster_version: "1.34"
2+
eks_ssm_parameter_name: "/aws/service/eks/optimized-ami/{{ eks_cluster_version }}/amazon-linux-2023/x86_64/standard/recommended/release_version"
3+
eks_network_calico_version: 3.31.2
4+
eks_nodes_version: "1.34"
5+
eks_nodes_instance_types:
6+
- m7i-flex.xlarge
7+
eks_nodes_ami_type: AL2023_x86_64_STANDARD
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
all:
3+
hosts:
4+
deploy_host_local:
5+
ansible_connection: local
6+
children:
7+
DEV:
8+
children:
9+
dev01:
10+
hosts:
11+
deploy_host_local:
12+
vars:
13+
ansible_python_interpreter: /usr/bin/env python3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../group_vars/DEV/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../group_vars/dev01/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
feature_flags_enable_feature_x: false
2+
feature_flags_enable_feature_y: false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
all:
3+
hosts:
4+
deploy_host_local:
5+
ansible_connection: local
6+
children:
7+
DEV:
8+
children:
9+
dev01:
10+
children:
11+
user1:
12+
hosts:
13+
deploy_host_local:
14+
vars:
15+
ansible_python_interpreter: /usr/bin/env python3

0 commit comments

Comments
 (0)