Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that ansible params check the playbook #2677

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/param-{{ lookup('ansible.builtin.env', 'PARAM_ANSIBLE') }}"
state: touch
8 changes: 1 addition & 7 deletions hack/test-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ declare -A CHECKS=(
["disk"]=""
["user-v2"]=""
["mount-path-with-spaces"]=""
["provision-ansible"]=""
["param-env-variables"]=""
["set-user"]=""
)
Expand All @@ -62,7 +61,6 @@ case "$NAME" in
CHECKS["snapshot-online"]="1"
CHECKS["snapshot-offline"]="1"
CHECKS["mount-path-with-spaces"]="1"
CHECKS["provision-ansible"]="1"
CHECKS["param-env-variables"]="1"
CHECKS["set-user"]="1"
;;
Expand Down Expand Up @@ -160,13 +158,9 @@ if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
[ "$(limactl shell "$NAME" cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
fi

if [[ -n ${CHECKS["provision-ansible"]} ]]; then
INFO 'Testing that /tmp/ansible was created successfully on provision'
limactl shell "$NAME" test -e /tmp/ansible
fi

if [[ -n ${CHECKS["param-env-variables"]} ]]; then
INFO 'Testing that PARAM env variables are exported to all types of provisioning scripts and probes'
limactl shell "$NAME" test -e /tmp/param-ansible
limactl shell "$NAME" test -e /tmp/param-boot
limactl shell "$NAME" test -e /tmp/param-dependency
limactl shell "$NAME" test -e /tmp/param-probe
Expand Down
1 change: 1 addition & 0 deletions hack/test-templates/test-misc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mounts:
writable: true

param:
ANSIBLE: ansible
BOOT: boot
DEPENDENCY: dependency
PROBE: probe
Expand Down
10 changes: 10 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,11 @@ 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 := os.Environ()
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 @@ -457,6 +457,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
Loading