Add back ENV variable support

This commit is contained in:
Dino Paškvan 2022-06-14 12:58:52 +02:00
parent 32cc67164e
commit 6ff9b34c10
2 changed files with 36 additions and 5 deletions

View File

@ -15,7 +15,7 @@ type AnsiblePlaybook struct {
Logger Logger
}
func (p AnsiblePlaybook) makeCmd(command string, args []string) *exec.Cmd {
func (p AnsiblePlaybook) makeCmd(command string, args []string, environmentVars *[]string) *exec.Cmd {
cmd := exec.Command(command, args...) //nolint: gas
cmd.Dir = p.GetFullPath()
@ -24,18 +24,21 @@ func (p AnsiblePlaybook) makeCmd(command string, args []string) *exec.Cmd {
cmd.Env = append(cmd.Env, fmt.Sprintf("PWD=%s", cmd.Dir))
cmd.Env = append(cmd.Env, "PYTHONUNBUFFERED=1")
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=True")
if environmentVars != nil {
cmd.Env = append(cmd.Env, *environmentVars...)
}
return cmd
}
func (p AnsiblePlaybook) runCmd(command string, args []string) error {
cmd := p.makeCmd(command, args)
cmd := p.makeCmd(command, args, nil)
p.Logger.LogCmd(cmd)
return cmd.Run()
}
func (p AnsiblePlaybook) RunPlaybook(args []string, cb func(*os.Process)) error {
cmd := p.makeCmd("ansible-playbook", args)
func (p AnsiblePlaybook) RunPlaybook(args []string, environmentVars *[]string, cb func(*os.Process)) error {
cmd := p.makeCmd("ansible-playbook", args, environmentVars)
p.Logger.LogCmd(cmd)
cmd.Stdin = strings.NewReader("")
err := cmd.Start()

View File

@ -559,11 +559,38 @@ func (t *TaskRunner) runPlaybook() (err error) {
return
}
environmentVariables, err := t.getEnvironmentENV()
if err != nil {
return
}
return lib.AnsiblePlaybook{
Logger: t,
TemplateID: t.template.ID,
Repository: t.repository,
}.RunPlaybook(args, func(p *os.Process) { t.process = p })
}.RunPlaybook(args, &environmentVariables, func(p *os.Process) { t.process = p })
}
func (t *TaskRunner) getEnvironmentENV() (arr []string, err error) {
extraVars := make(map[string]interface{})
if t.environment.JSON != "" {
err = json.Unmarshal([]byte(t.environment.JSON), &extraVars)
if err != nil {
return
}
}
if cfg, ok := extraVars["ENV"]; ok {
switch v := cfg.(type) {
case map[string]interface{}:
for key, val := range v {
arr = append(arr, fmt.Sprintf("%s=%s", key, val))
}
}
}
return
}
func (t *TaskRunner) getEnvironmentExtraVars() (str string, err error) {
@ -602,6 +629,7 @@ func (t *TaskRunner) getEnvironmentExtraVars() (str string, err error) {
}
vars := make(map[string]interface{})
delete(extraVars, "ENV")
vars["task_details"] = taskDetails
extraVars["semaphore_vars"] = vars