Skip to content

Commit

Permalink
Add support for updating registered variables for the following tasks.
Browse files Browse the repository at this point in the history
This update adds the function 'UpdateRegisteredVars' to update environment variables with values from a list of registered variables passed on by the caller. This ensures registered variables can be used across tasks are updated promptly.
  • Loading branch information
umputun committed Apr 27, 2024
1 parent c17efaf commit 11dff02
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cmd/spot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ func runTaskForTarget(ctx context.Context, r *runner.Process, taskName, targetNa
}
log.Printf("[INFO] completed: hosts:%d, commands:%d in %v\n",
res.Hosts, res.Commands, time.Since(st).Truncate(100*time.Millisecond))
r.Playbook.UpdateTasksTargets(res.Vars) // for dynamic targets
r.Playbook.UpdateTasksTargets(res.Vars) // for dynamic targets
r.Playbook.UpdateRegisteredVars(res.Registered) // for registered vars, cross-task
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/go-pkgz/stringutils"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -212,15 +213,21 @@ func (cmd *Cmd) scriptFile(inp string, register []string) (r io.Reader) {
}

// each exported variable is printed as a setvar command to be captured by the caller
exported := []string{} // collect all exported variables to avoid duplicates in setvar output
if len(exports) > 0 {
for _, v := range exports {
buf.WriteString(fmt.Sprintf("echo setvar %s=${%s}\n", v, v))
exported = append(exported, v)
}
}

// each register variable is printed as a setvar command to be captured by the caller
if len(register) > 0 {
for _, v := range register {
if stringutils.Contains(v, exported) {
// if already exported, we don't need to print it again
continue
}
buf.WriteString(fmt.Sprintf("echo setvar %s=${%s}\n", v, v))
}
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/config/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,30 @@ func (p *PlayBook) UpdateTasksTargets(vars map[string]string) {
}
}

// UpdateRegisteredVars takes the list of registered vars from the caller handling task execution
// and updates the environment variables of all commands in the playbook with the values from the list.
func (p *PlayBook) UpdateRegisteredVars(vars map[string]string) {
if len(vars) == 0 {
return
}
log.Printf("[DEBUG] update registered vars %+v", vars)
for k, v := range vars {
for _, tsk := range p.Tasks {
for i, c := range tsk.Commands {
env := c.Environment
if env == nil {
env = make(map[string]string)
}
if _, ok := env[k]; ok { // don't allow override already set vars. TODO: not sure if this is correct
continue
}
env[k] = v
tsk.Commands[i].Environment = env
}
}
}
}

// loadInventory loads the inventory data from the specified location (file or URL) and returns it as an InventoryData struct.
// The inventory data is parsed as either YAML or TOML, depending on the file extension.
// The method also performs some additional processing on the inventory data:
Expand Down
54 changes: 49 additions & 5 deletions pkg/runner/mocks/playbook.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Playbook interface {
TargetHosts(name string) ([]config.Destination, error)
AllSecretValues() []string
UpdateTasksTargets(vars map[string]string)
UpdateRegisteredVars(vars map[string]string)
}

// ProcResp holds the information about processed commands and hosts.
Expand Down

0 comments on commit 11dff02

Please sign in to comment.