Skip to content

Commit

Permalink
feat(manager): add support for host/group/role template files
Browse files Browse the repository at this point in the history
This expands the support for inventory defined template
(`templates/*.tpl`) files to also expose inventory templates that have
been defined in the group, role, and host components to the module
during templating
  • Loading branch information
tjhop committed Jul 16, 2024
1 parent 131237e commit f2db514
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 8 deletions.
9 changes: 6 additions & 3 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Manager struct {
directives []Directive
executedDirectives map[string]struct{} // stores the ID of the directive as key
hostVariables VariableSlice
hostTemplates []string
runLock sync.Mutex
funcMap template.FuncMap
tmplData templateData
Expand Down Expand Up @@ -127,18 +128,20 @@ func (mgr *Manager) Reload(ctx context.Context, logger *slog.Logger, inv invento
// triggered directly after a reload of data from inventory)
hostVarsPaths := inv.GetVariablesForSelf()
if len(hostVarsPaths) > 0 {
mgr.hostVariables = mgr.ReloadVariables(ctx, logger, hostVarsPaths, nil)
mgr.hostVariables = mgr.ReloadVariables(ctx, logger, hostVarsPaths, nil, nil)
} else {
logger.DebugContext(ctx, "No host variables")
}

mgr.hostTemplates = inv.GetTemplatesForSelf()
}

func (mgr *Manager) ReloadVariables(ctx context.Context, logger *slog.Logger, paths []string, hostVars VariableMap) VariableSlice {
func (mgr *Manager) ReloadVariables(ctx context.Context, logger *slog.Logger, paths []string, hostVars VariableMap, hostTemplates []string) VariableSlice {
var varMaps []VariableMap

for _, path := range paths {
allTemplateData := mgr.getTemplateData(ctx, path, hostVars, nil, hostVars)
renderedVars, err := templateScript(ctx, path, allTemplateData, mgr.funcMap)
renderedVars, err := templateScript(ctx, path, allTemplateData, mgr.funcMap, hostTemplates...)
if err != nil {
logger.LogAttrs(
ctx,
Expand Down
7 changes: 4 additions & 3 deletions internal/manager/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (mgr *Manager) ReloadModules(ctx context.Context, logger *slog.Logger) {
// if the module has a variables file set, source it and store
// the expanded variables
if mod.Variables != "" {
newMod.Variables = mgr.ReloadVariables(ctx, modLogger, []string{mod.Variables}, shell.MakeVariableMap(mgr.hostVariables))
newMod.Variables = mgr.ReloadVariables(ctx, modLogger, []string{mod.Variables}, shell.MakeVariableMap(mgr.hostVariables), mgr.hostTemplates)
} else {
modLogger.DebugContext(ctx, "No module variables")
}
Expand Down Expand Up @@ -126,6 +126,7 @@ func (mgr *Manager) RunModule(ctx context.Context, logger *slog.Logger, mod Modu
allVars := shell.MergeVariables(hostVarsMap, modVarsMap)
allVarsMap := shell.MakeVariableMap(allVars)
allTemplateData := mgr.getTemplateData(ctx, mod.String(), hostVarsMap, modVarsMap, allVarsMap)
allUserTemplateFiles := append(mgr.hostTemplates, mod.m.TemplateFiles...)

var testRC uint8
if mod.m.Test == "" {
Expand All @@ -139,7 +140,7 @@ func (mgr *Manager) RunModule(ctx context.Context, logger *slog.Logger, mod Modu
labels["script"] = "test"
metricManagerModuleRunTimestamp.With(labels).Set(float64(testStart.Unix()))

renderedTest, err := templateScript(ctx, mod.m.Test, allTemplateData, mgr.funcMap, mod.m.TemplateFiles...)
renderedTest, err := templateScript(ctx, mod.m.Test, allTemplateData, mgr.funcMap, allUserTemplateFiles...)
if err != nil {
return fmt.Errorf("Failed to template script: %s", err)
}
Expand Down Expand Up @@ -187,7 +188,7 @@ func (mgr *Manager) RunModule(ctx context.Context, logger *slog.Logger, mod Modu
labels["script"] = "apply"
metricManagerModuleRunTimestamp.With(labels).Set(float64(applyStart.Unix()))

renderedApply, err := templateScript(ctx, mod.m.Apply, allTemplateData, mgr.funcMap, mod.m.TemplateFiles...)
renderedApply, err := templateScript(ctx, mod.m.Apply, allTemplateData, mgr.funcMap, allUserTemplateFiles...)
if err != nil {
return fmt.Errorf("Failed to template script: %s", err)
}
Expand Down
3 changes: 3 additions & 0 deletions test/mockup/inventory/groups/default/templates/group.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- define "group_template" }}
This part of the template was defined among the common template files in the "default" group of the test inventory.
{{- end }}
3 changes: 3 additions & 0 deletions test/mockup/inventory/hosts/testbox-arch/templates/host.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- define "host_template" }}
This part of the template was defined among the common template files for the host {{ .Mango.Metadata.Hostname | quote }}.
{{- end }}
3 changes: 3 additions & 0 deletions test/mockup/inventory/hosts/testbox-ubuntu/templates/host.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- define "group_template" }}
This part of the template was defined among the common template files for the host {{ .Mango.Metadata.Hostname | quote }}.
{{- end }}
6 changes: 4 additions & 2 deletions test/mockup/inventory/modules/test-template/templates/foo.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{{- define "foo" }}
This part of the template was defined among the common templates in {{ list .Mango.Metadata.ModuleName "templates/*.tpl" | join "/" | quote }}

{{ template "inspirational_quote" . | toString }}
{{ template "group_template" . }}
{{ template "role_template" . }}
{{ template "host_template" . }}
{{ template "inspirational_quote" . }}
{{- end }}

{{- define "inspirational_quote" }}
Expand Down
3 changes: 3 additions & 0 deletions test/mockup/inventory/roles/common/templates/role.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- define "role_template" }}
This part of the template was defined among the common template files in the "common" role of the test inventory.
{{- end }}

0 comments on commit f2db514

Please sign in to comment.