feat(ansible): support pipe lookups
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
541d16b5a6
commit
470592e253
2 changed files with 20 additions and 2 deletions
14
executor.go
14
executor.go
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue