Skip to content

Commit d1d8189

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 996b340 commit d1d8189

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-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
)
4140

@@ -64,7 +63,6 @@ case "$NAME" in
6463
CHECKS["snapshot-online"]="1"
6564
CHECKS["snapshot-offline"]="1"
6665
CHECKS["mount-path-with-spaces"]="1"
67-
CHECKS["provision-ansible"]="1"
6866
CHECKS["param-env-variables"]="1"
6967
;;
7068
"net-user-v2")
@@ -149,13 +147,9 @@ if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
149147
[ "$(limactl shell "$NAME" cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
150148
fi
151149

152-
if [[ -n ${CHECKS["provision-ansible"]} ]]; then
153-
INFO 'Testing that /tmp/ansible was created successfully on provision'
154-
limactl shell "$NAME" test -e /tmp/ansible
155-
fi
156-
157150
if [[ -n ${CHECKS["param-env-variables"]} ]]; then
158151
INFO 'Testing that PARAM env variables are exported to all types of provisioning scripts and probes'
152+
limactl shell "$NAME" test -e /tmp/param-ansible
159153
limactl shell "$NAME" test -e /tmp/param-boot
160154
limactl shell "$NAME" test -e /tmp/param-dependency
161155
limactl shell "$NAME" test -e /tmp/param-probe

hack/test-templates/test-misc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mounts:
2727
writable: true
2828

2929
param:
30+
ANSIBLE: ansible
3031
BOOT: boot
3132
DEPENDENCY: dependency
3233
PROBE: probe

pkg/instance/ansible.go

+11
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,12 @@ 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 := []string{}
68+
env = append(env, os.Environ()...)
69+
for key, val := range inst.Config.Param {
70+
env = append(env, fmt.Sprintf("PARAM_%s=%s", key, val))
71+
}
72+
return env
73+
}

pkg/limayaml/validate.go

+10
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,16 @@ func ValidateParamIsUsed(y *LimaYAML) error {
445445
keyIsUsed = true
446446
break
447447
}
448+
if p.Playbook != "" {
449+
playbook, err := os.ReadFile(p.Playbook)
450+
if err != nil {
451+
return err
452+
}
453+
if re.Match(playbook) {
454+
keyIsUsed = true
455+
break
456+
}
457+
}
448458
}
449459
for _, p := range y.Probes {
450460
if re.MatchString(p.Script) {

0 commit comments

Comments
 (0)