feat(ansible): support pipe lookups
Some checks are pending
CI / auto-merge (push) Waiting to run
CI / test (push) Waiting to run
CI / auto-fix (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 14:00:44 +00:00
parent 541d16b5a6
commit 470592e253
2 changed files with 20 additions and 2 deletions

View file

@ -3570,14 +3570,19 @@ func isUnresolvedTemplateValue(value string) bool {
// handleLookup handles lookup() expressions.
func (e *Executor) handleLookup(expr string, host string, task *Task) string {
// Parse lookup('type', 'arg')
re := regexp.MustCompile(`lookup\s*\(\s*['"](\w+)['"]\s*,\s*['"]([^'"]+)['"]\s*`)
re := regexp.MustCompile(`lookup\s*\(\s*['"](\w+)['"]\s*,\s*(.+?)\s*\)`)
match := re.FindStringSubmatch(expr)
if len(match) < 3 {
return ""
}
lookupType := match[1]
arg := match[2]
arg := strings.TrimSpace(match[2])
if len(arg) >= 2 {
if (arg[0] == '\'' && arg[len(arg)-1] == '\'') || (arg[0] == '"' && arg[len(arg)-1] == '"') {
arg = arg[1 : len(arg)-1]
}
}
switch lookupType {
case "env":
@ -3586,6 +3591,11 @@ func (e *Executor) handleLookup(expr string, host string, task *Task) string {
if data, err := coreio.Local.Read(e.resolveLocalPath(arg)); err == nil {
return data
}
case "pipe":
stdout, _, rc, err := runLocalShell(context.Background(), arg, "")
if err == nil && rc == 0 {
return strings.TrimRight(stdout, "\r\n")
}
case "vars":
if value, ok := e.lookupConditionValue(arg, host, task, nil); ok {
return sprintf("%v", value)

View file

@ -888,6 +888,14 @@ func TestExecutorExtra_HandleLookup_Good_VarsLookup(t *testing.T) {
assert.Equal(t, "resolved from vars", result)
}
func TestExecutorExtra_HandleLookup_Good_PipeLookup(t *testing.T) {
e := NewExecutor("/tmp")
result := e.handleLookup("lookup('pipe', 'printf pipe-value')", "", nil)
assert.Equal(t, "pipe-value", result)
}
func TestExecutorExtra_HandleLookup_Bad_InvalidSyntax(t *testing.T) {
e := NewExecutor("/tmp")
result := e.handleLookup("lookup(invalid)", "", nil)