Skip to content

Commit 3ca68d0

Browse files
committed
Make sure that ansible params check the playbook
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 <[email protected]>
1 parent 16ba927 commit 3ca68d0

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

hack/ansible-test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
tasks:
33
- name: Create test file
44
file:
5-
path: /tmp/ansible
5+
path: "/tmp/param-{{ lookup('ansible.builtin.env', 'PARAM_ANSIBLE') }}"
66
state: touch

hack/test-templates.sh

+1-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ declare -A CHECKS=(
3535
["disk"]=""
3636
["user-v2"]=""
3737
["mount-path-with-spaces"]=""
38-
["provision-ansible"]=""
3938
["param-env-variables"]=""
4039
["set-user"]=""
4140
)
@@ -62,7 +61,6 @@ case "$NAME" in
6261
CHECKS["snapshot-online"]="1"
6362
CHECKS["snapshot-offline"]="1"
6463
CHECKS["mount-path-with-spaces"]="1"
65-
CHECKS["provision-ansible"]="1"
6664
CHECKS["param-env-variables"]="1"
6765
CHECKS["set-user"]="1"
6866
;;
@@ -160,13 +158,9 @@ if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
160158
[ "$(limactl shell "$NAME" cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
161159
fi
162160

163-
if [[ -n ${CHECKS["provision-ansible"]} ]]; then
164-
INFO 'Testing that /tmp/ansible was created successfully on provision'
165-
limactl shell "$NAME" test -e /tmp/ansible
166-
fi
167-
168161
if [[ -n ${CHECKS["param-env-variables"]} ]]; then
169162
INFO 'Testing that PARAM env variables are exported to all types of provisioning scripts and probes'
163+
limactl shell "$NAME" test -e /tmp/param-ansible
170164
limactl shell "$NAME" test -e /tmp/param-boot
171165
limactl shell "$NAME" test -e /tmp/param-dependency
172166
limactl shell "$NAME" test -e /tmp/param-probe

hack/test-templates/test-misc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mounts:
3030
writable: true
3131

3232
param:
33+
ANSIBLE: ansible
3334
BOOT: boot
3435
DEPENDENCY: dependency
3536
PROBE: probe

pkg/instance/ansible.go

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package instance
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -33,6 +34,7 @@ func runAnsiblePlaybook(ctx context.Context, inst *store.Instance, playbook stri
3334
logrus.Debugf("ansible-playbook -i %q %q", inventory, playbook)
3435
args := []string{"-i", inventory, playbook}
3536
cmd := exec.CommandContext(ctx, "ansible-playbook", args...)
37+
cmd.Env = getAnsibleEnvironment(inst)
3638
cmd.Stdout = os.Stdout
3739
cmd.Stderr = os.Stderr
3840
return cmd.Run()
@@ -60,3 +62,11 @@ func createAnsibleInventory(inst *store.Instance) (string, error) {
6062
inventory := filepath.Join(inst.Dir, filenames.AnsibleInventoryYAML)
6163
return inventory, os.WriteFile(inventory, bytes, 0o644)
6264
}
65+
66+
func getAnsibleEnvironment(inst *store.Instance) []string {
67+
env := os.Environ()
68+
for key, val := range inst.Config.Param {
69+
env = append(env, fmt.Sprintf("PARAM_%s=%s", key, val))
70+
}
71+
return env
72+
}

pkg/limayaml/validate.go

+10
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,16 @@ func ValidateParamIsUsed(y *LimaYAML) error {
457457
keyIsUsed = true
458458
break
459459
}
460+
if p.Playbook != "" {
461+
playbook, err := os.ReadFile(p.Playbook)
462+
if err != nil {
463+
return err
464+
}
465+
if re.Match(playbook) {
466+
keyIsUsed = true
467+
break
468+
}
469+
}
460470
}
461471
for _, p := range y.Probes {
462472
if re.MatchString(p.Script) {

0 commit comments

Comments
 (0)