From f2db5140107886b1e7a7e76b6627a3944f840112 Mon Sep 17 00:00:00 2001 From: TJ Hoplock Date: Tue, 16 Jul 2024 01:14:27 -0400 Subject: [PATCH] feat(manager): add support for host/group/role template files 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 --- internal/manager/manager.go | 9 ++++++--- internal/manager/module.go | 7 ++++--- test/mockup/inventory/groups/default/templates/group.tpl | 3 +++ .../inventory/hosts/testbox-arch/templates/host.tpl | 3 +++ .../inventory/hosts/testbox-ubuntu/templates/host.tpl | 3 +++ .../inventory/modules/test-template/templates/foo.tpl | 6 ++++-- test/mockup/inventory/roles/common/templates/role.tpl | 3 +++ 7 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 test/mockup/inventory/groups/default/templates/group.tpl create mode 100644 test/mockup/inventory/hosts/testbox-arch/templates/host.tpl create mode 100644 test/mockup/inventory/hosts/testbox-ubuntu/templates/host.tpl create mode 100644 test/mockup/inventory/roles/common/templates/role.tpl diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 7b35fd4..3657be5 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -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 @@ -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, diff --git a/internal/manager/module.go b/internal/manager/module.go index 5387fc1..6b8ac9a 100644 --- a/internal/manager/module.go +++ b/internal/manager/module.go @@ -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") } @@ -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 == "" { @@ -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) } @@ -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) } diff --git a/test/mockup/inventory/groups/default/templates/group.tpl b/test/mockup/inventory/groups/default/templates/group.tpl new file mode 100644 index 0000000..c41ce3b --- /dev/null +++ b/test/mockup/inventory/groups/default/templates/group.tpl @@ -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 }} diff --git a/test/mockup/inventory/hosts/testbox-arch/templates/host.tpl b/test/mockup/inventory/hosts/testbox-arch/templates/host.tpl new file mode 100644 index 0000000..80cc2d3 --- /dev/null +++ b/test/mockup/inventory/hosts/testbox-arch/templates/host.tpl @@ -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 }} diff --git a/test/mockup/inventory/hosts/testbox-ubuntu/templates/host.tpl b/test/mockup/inventory/hosts/testbox-ubuntu/templates/host.tpl new file mode 100644 index 0000000..f1c101c --- /dev/null +++ b/test/mockup/inventory/hosts/testbox-ubuntu/templates/host.tpl @@ -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 }} diff --git a/test/mockup/inventory/modules/test-template/templates/foo.tpl b/test/mockup/inventory/modules/test-template/templates/foo.tpl index f6c02b8..b345367 100644 --- a/test/mockup/inventory/modules/test-template/templates/foo.tpl +++ b/test/mockup/inventory/modules/test-template/templates/foo.tpl @@ -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" }} diff --git a/test/mockup/inventory/roles/common/templates/role.tpl b/test/mockup/inventory/roles/common/templates/role.tpl new file mode 100644 index 0000000..5727d04 --- /dev/null +++ b/test/mockup/inventory/roles/common/templates/role.tpl @@ -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 }}