Skip to content

Commit

Permalink
Make sure that ansible params check the playbook
Browse files Browse the repository at this point in the history
The ansible provisioning supports using a separate yaml playbook,
so check this file (but only the top playbook) for any parameters...

The `ansible-playbook` command does not run remotely so it does not
use the param.env, which means that the env is set on the command.

Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Oct 2, 2024
1 parent 16fd466 commit 3ff7933
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hack/ansible-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
tasks:
- name: Create test file
file:
path: /tmp/ansible
path: "/tmp/{{ lookup('ansible.builtin.env', 'PARAM_ANSIBLE') }}"
state: touch
1 change: 1 addition & 0 deletions hack/test-templates/test-misc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mounts:
writable: true

param:
ANSIBLE: ansible
BOOT: boot
DEPENDENCY: dependency
PROBE: probe
Expand Down
13 changes: 13 additions & 0 deletions pkg/instance/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package instance

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -33,6 +34,7 @@ func runAnsiblePlaybook(ctx context.Context, inst *store.Instance, playbook stri
logrus.Debugf("ansible-playbook -i %q %q", inventory, playbook)
args := []string{"-i", inventory, playbook}
cmd := exec.CommandContext(ctx, "ansible-playbook", args...)
cmd.Env = getAnsibleEnvironment(inst)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
Expand Down Expand Up @@ -60,3 +62,14 @@ func createAnsibleInventory(inst *store.Instance) (string, error) {
inventory := filepath.Join(inst.Dir, filenames.AnsibleInventoryYAML)
return inventory, os.WriteFile(inventory, bytes, 0o644)
}

func getAnsibleEnvironment(inst *store.Instance) []string {
env := []string{}
for _, e := range os.Environ() {

Check failure on line 68 in pkg/instance/ansible.go

View workflow job for this annotation

GitHub Actions / Lints

S1011: should replace loop with `env = append(env, os.Environ()...)` (gosimple)
env = append(env, e)
}
for key, val := range inst.Config.Param {
env = append(env, fmt.Sprintf("PARAM_%s=%s", key, val))
}
return env
}
10 changes: 10 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ func ValidateParamIsUsed(y *LimaYAML) error {
keyIsUsed = true
break
}
if p.Playbook != "" {
playbook, err := os.ReadFile(p.Playbook)
if err != nil {
return err
}
if re.Match(playbook) {
keyIsUsed = true
break
}
}
}
for _, p := range y.Probes {
if re.MatchString(p.Script) {
Expand Down

0 comments on commit 3ff7933

Please sign in to comment.