feat(ansible): support vars lookup
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
35014b52fc
commit
e6be1e5f5a
2 changed files with 19 additions and 6 deletions
|
|
@ -2440,7 +2440,7 @@ func (e *Executor) resolveExpr(expr string, host string, task *Task) string {
|
|||
|
||||
// Handle lookups
|
||||
if corexHasPrefix(expr, "lookup(") {
|
||||
return e.handleLookup(expr)
|
||||
return e.handleLookup(expr, host, task)
|
||||
}
|
||||
|
||||
// Handle registered vars
|
||||
|
|
@ -2753,7 +2753,7 @@ func isUnresolvedTemplateValue(value string) bool {
|
|||
}
|
||||
|
||||
// handleLookup handles lookup() expressions.
|
||||
func (e *Executor) handleLookup(expr string) string {
|
||||
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*`)
|
||||
match := re.FindStringSubmatch(expr)
|
||||
|
|
@ -2771,6 +2771,10 @@ func (e *Executor) handleLookup(expr string) string {
|
|||
if data, err := coreio.Local.Read(e.resolveLocalPath(arg)); err == nil {
|
||||
return data
|
||||
}
|
||||
case "vars":
|
||||
if value, ok := e.lookupConditionValue(arg, host, task, nil); ok {
|
||||
return sprintf("%v", value)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
|
|
|
|||
|
|
@ -504,13 +504,13 @@ func TestExecutorExtra_HandleLookup_Good_EnvVar(t *testing.T) {
|
|||
e := NewExecutor("/tmp")
|
||||
t.Setenv("TEST_ANSIBLE_LOOKUP", "found_it")
|
||||
|
||||
result := e.handleLookup("lookup('env', 'TEST_ANSIBLE_LOOKUP')")
|
||||
result := e.handleLookup("lookup('env', 'TEST_ANSIBLE_LOOKUP')", "", nil)
|
||||
assert.Equal(t, "found_it", result)
|
||||
}
|
||||
|
||||
func TestExecutorExtra_HandleLookup_Good_EnvVarMissing(t *testing.T) {
|
||||
e := NewExecutor("/tmp")
|
||||
result := e.handleLookup("lookup('env', 'NONEXISTENT_VAR_12345')")
|
||||
result := e.handleLookup("lookup('env', 'NONEXISTENT_VAR_12345')", "", nil)
|
||||
assert.Equal(t, "", result)
|
||||
}
|
||||
|
||||
|
|
@ -519,14 +519,23 @@ func TestExecutorExtra_HandleLookup_Good_FileLookupResolvesBasePath(t *testing.T
|
|||
require.NoError(t, writeTestFile(joinPath(dir, "vars.txt"), []byte("from base path"), 0644))
|
||||
|
||||
e := NewExecutor(dir)
|
||||
result := e.handleLookup("lookup('file', 'vars.txt')")
|
||||
result := e.handleLookup("lookup('file', 'vars.txt')", "", nil)
|
||||
|
||||
assert.Equal(t, "from base path", result)
|
||||
}
|
||||
|
||||
func TestExecutorExtra_HandleLookup_Good_VarsLookup(t *testing.T) {
|
||||
e := NewExecutor("/tmp")
|
||||
e.SetVar("lookup_value", "resolved from vars")
|
||||
|
||||
result := e.handleLookup("lookup('vars', 'lookup_value')", "", nil)
|
||||
|
||||
assert.Equal(t, "resolved from vars", result)
|
||||
}
|
||||
|
||||
func TestExecutorExtra_HandleLookup_Bad_InvalidSyntax(t *testing.T) {
|
||||
e := NewExecutor("/tmp")
|
||||
result := e.handleLookup("lookup(invalid)")
|
||||
result := e.handleLookup("lookup(invalid)", "", nil)
|
||||
assert.Equal(t, "", result)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue