Skip to content

Commit 329d851

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 <anders.f.bjorklund@gmail.com>
1 parent 0625d0b commit 329d851

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
@@ -54,7 +54,6 @@ declare -A CHECKS=(
5454
["disk"]=""
5555
["user-v2"]=""
5656
["mount-path-with-spaces"]=""
57-
["provision-ansible"]=""
5857
["provision-data"]=""
5958
["param-env-variables"]=""
6059
["set-user"]=""
@@ -82,7 +81,6 @@ case "$NAME" in
8281
CHECKS["snapshot-online"]="1"
8382
CHECKS["snapshot-offline"]="1"
8483
CHECKS["mount-path-with-spaces"]="1"
85-
CHECKS["provision-ansible"]="1"
8684
CHECKS["provision-data"]="1"
8785
CHECKS["param-env-variables"]="1"
8886
CHECKS["set-user"]="1"
@@ -188,18 +186,14 @@ if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
188186
[ "$(limactl shell "$NAME" cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
189187
fi
190188

191-
if [[ -n ${CHECKS["provision-ansible"]} ]]; then
192-
INFO 'Testing that /tmp/ansible was created successfully on provision'
193-
limactl shell "$NAME" test -e /tmp/ansible
194-
fi
195-
196189
if [[ -n ${CHECKS["provision-data"]} ]]; then
197190
INFO 'Testing that /etc/sysctl.d/99-inotify.conf was created successfully on provision'
198191
limactl shell "$NAME" grep -q fs.inotify.max_user_watches /etc/sysctl.d/99-inotify.conf
199192
fi
200193

201194
if [[ -n ${CHECKS["param-env-variables"]} ]]; then
202195
INFO 'Testing that PARAM env variables are exported to all types of provisioning scripts and probes'
196+
limactl shell "$NAME" test -e /tmp/param-ansible
203197
limactl shell "$NAME" test -e /tmp/param-boot
204198
limactl shell "$NAME" test -e /tmp/param-dependency
205199
limactl shell "$NAME" test -e /tmp/param-probe

hack/test-templates/test-misc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mounts:
1414
writable: true
1515

1616
param:
17+
ANSIBLE: ansible
1718
BOOT: boot
1819
DEPENDENCY: dependency
1920
PROBE: probe

pkg/instance/ansible.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package instance
55

66
import (
77
"context"
8+
"fmt"
89
"os"
910
"os/exec"
1011
"path/filepath"
@@ -36,6 +37,7 @@ func runAnsiblePlaybook(ctx context.Context, inst *store.Instance, playbook stri
3637
logrus.Debugf("ansible-playbook -i %q %q", inventory, playbook)
3738
args := []string{"-i", inventory, playbook}
3839
cmd := exec.CommandContext(ctx, "ansible-playbook", args...)
40+
cmd.Env = getAnsibleEnvironment(inst)
3941
cmd.Stdout = os.Stdout
4042
cmd.Stderr = os.Stderr
4143
return cmd.Run()
@@ -63,3 +65,11 @@ func createAnsibleInventory(inst *store.Instance) (string, error) {
6365
inventory := filepath.Join(inst.Dir, filenames.AnsibleInventoryYAML)
6466
return inventory, os.WriteFile(inventory, bytes, 0o644)
6567
}
68+
69+
func getAnsibleEnvironment(inst *store.Instance) []string {
70+
env := os.Environ()
71+
for key, val := range inst.Config.Param {
72+
env = append(env, fmt.Sprintf("PARAM_%s=%s", key, val))
73+
}
74+
return env
75+
}

pkg/limayaml/validate.go

+10
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,16 @@ func ValidateParamIsUsed(y *LimaYAML) error {
524524
keyIsUsed = true
525525
break
526526
}
527+
if p.Playbook != "" {
528+
playbook, err := os.ReadFile(p.Playbook)
529+
if err != nil {
530+
return err
531+
}
532+
if re.Match(playbook) {
533+
keyIsUsed = true
534+
break
535+
}
536+
}
527537
}
528538
for _, p := range y.Probes {
529539
if re.MatchString(p.Script) {

0 commit comments

Comments
 (0)