From c8abab3034bfecfd91ba6c5534818efcf1dcbb07 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 14:42:04 +0000 Subject: [PATCH] feat(ansible): unify template file rendering Co-Authored-By: Virgil --- executor.go | 47 +------------------------------------------- modules_file_test.go | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/executor.go b/executor.go index b4815d4..bce4507 100644 --- a/executor.go +++ b/executor.go @@ -15,7 +15,6 @@ import ( "strconv" "strings" "sync" - "text/template" "time" coreio "dappco.re/go/core/io" @@ -3631,49 +3630,5 @@ func (e *Executor) TemplateFile(src, host string, task *Task) (string, error) { return "", err } - // Convert Jinja2 to Go template syntax (basic conversion) - tmplContent := content - tmplContent = replaceAll(tmplContent, "{{", "{{ .") - tmplContent = replaceAll(tmplContent, "{%", "{{") - tmplContent = replaceAll(tmplContent, "%}", "}}") - - tmpl, err := template.New("template").Parse(tmplContent) - if err != nil { - // Fall back to simple replacement - return e.templateString(content, host, task), nil - } - - // Build context map - context := make(map[string]any) - for k, v := range e.vars { - context[k] = v - } - // Add host vars - if e.inventory != nil { - hostVars := GetHostVars(e.inventory, host) - for k, v := range hostVars { - context[k] = v - } - } - // Add facts - if facts, ok := e.facts[host]; ok { - context["ansible_facts"] = factsToMap(facts) - context["ansible_hostname"] = facts.Hostname - context["ansible_fqdn"] = facts.FQDN - context["ansible_os_family"] = facts.OS - context["ansible_memtotal_mb"] = facts.Memory - context["ansible_processor_vcpus"] = facts.CPUs - context["ansible_default_ipv4_address"] = facts.IPv4 - context["ansible_distribution"] = facts.Distribution - context["ansible_distribution_version"] = facts.Version - context["ansible_architecture"] = facts.Architecture - context["ansible_kernel"] = facts.Kernel - } - - buf := newBuilder() - if err := tmpl.Execute(buf, context); err != nil { - return e.templateString(content, host, task), nil - } - - return buf.String(), nil + return e.templateString(content, host, task), nil } diff --git a/modules_file_test.go b/modules_file_test.go index 2825227..d6f3731 100644 --- a/modules_file_test.go +++ b/modules_file_test.go @@ -905,6 +905,32 @@ func TestModulesFile_ModuleTemplate_Good_AnsibleFactsMapTemplate(t *testing.T) { assert.Contains(t, string(up.Content), "host=web01") } +func TestModulesFile_ModuleTemplate_Good_TaskVarsAndHostMagicVars(t *testing.T) { + tmpDir := t.TempDir() + srcPath := joinPath(tmpDir, "context.conf.j2") + require.NoError(t, writeTestFile(srcPath, []byte("short={{ inventory_hostname_short }} local={{ local_value }}"), 0644)) + + e, mock := newTestExecutorWithMock("web01.example.com") + task := &Task{ + Vars: map[string]any{ + "local_value": "from-task", + }, + } + + result, err := moduleTemplateWithClient(e, mock, map[string]any{ + "src": srcPath, + "dest": "/etc/app/context.conf", + }, "web01.example.com", task) + + require.NoError(t, err) + assert.True(t, result.Changed) + + up := mock.lastUpload() + require.NotNil(t, up) + assert.Contains(t, string(up.Content), "short=web01") + assert.Contains(t, string(up.Content), "local=from-task") +} + func TestModulesFile_ModuleTemplate_Good_CustomMode(t *testing.T) { tmpDir := t.TempDir() srcPath := joinPath(tmpDir, "script.sh.j2")