Skip to content

Commit b251e76

Browse files
authored
fix: reorder variable merging logic in GetAllVariable for clarity and priority (#3094)
Signed-off-by: redscholar <blacktiledhouse@gmail.com>
1 parent 3f6a6c2 commit b251e76

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

pkg/variable/variable_get.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,28 @@ var GetAllVariable = func(hostname string) GetFunc {
130130
}
131131
}
132132

133-
// getHostsVariable builds a complete variable map for all hosts
134-
// by combining variables from multiple sources in a specific order
135133
getHostsVariable := func(v *variable) map[string]any {
136134
globalHosts := make(map[string]any)
137135
for hostname := range v.value.Hosts {
138136
hostVars := make(map[string]any)
139-
// Merge inventory-level variables first (lower priority than group vars)
137+
138+
// 1. Merge remote variables (lowest priority)
139+
hostVars = CombineVariables(hostVars, v.value.Hosts[hostname].RemoteVars)
140+
// 2. Merge runtime variables
141+
hostVars = CombineVariables(hostVars, v.value.Hosts[hostname].RuntimeVars)
142+
// 3. Merge inventory-level variables (vars)
140143
hostVars = CombineVariables(hostVars, Extension2Variables(v.value.Inventory.Spec.Vars))
141-
// Set group variables for hosts that belong to groups (override spec.vars)
144+
// 4. Merge group variables (groups > vars)
142145
for _, gv := range v.value.Inventory.Spec.Groups {
143146
if slices.Contains(gv.Hosts, hostname) {
144147
hostVars = CombineVariables(hostVars, Extension2Variables(gv.Vars))
145148
}
146149
}
147-
// Merge remote variables (variables collected from the actual host)
148-
hostVars = CombineVariables(hostVars, v.value.Hosts[hostname].RemoteVars)
149-
// Merge runtime variables (variables set during playbook execution)
150-
hostVars = CombineVariables(hostVars, v.value.Hosts[hostname].RuntimeVars)
151-
152-
// Merge host-specific variables from inventory (override group vars)
150+
// 5. Merge host-specific variables from inventory (host > groups)
153151
hostVars = CombineVariables(hostVars, Extension2Variables(v.value.Inventory.Spec.Hosts[hostname]))
154-
// Merge configuration variables
152+
// 6. Merge configuration variables (highest priority)
155153
hostVars = CombineVariables(hostVars, Extension2Variables(v.value.Config.Spec))
154+
156155
// Set default variables for localhost
157156
defaultHostVariable(hostname, hostVars)
158157
globalHosts[hostname] = hostVars

0 commit comments

Comments
 (0)