Skip to content

Commit

Permalink
Support ansible provision mode for remote playbook
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Apr 15, 2024
1 parent f8fbae4 commit 830dd18
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
})
case limayaml.ProvisionModeBoot:
continue
case limayaml.ProvisionModeAnsible:
continue
default:
return fmt.Errorf("unknown provision mode %q", f.Mode)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ const (
ProvisionModeUser ProvisionMode = "user"
ProvisionModeBoot ProvisionMode = "boot"
ProvisionModeDependency ProvisionMode = "dependency"
ProvisionModeAnsible ProvisionMode = "ansible"
)

type Provision struct {
Mode ProvisionMode `yaml:"mode" json:"mode"` // default: "system"
SkipDefaultDependencyResolution *bool `yaml:"skipDefaultDependencyResolution,omitempty" json:"skipDefaultDependencyResolution,omitempty"`
Script string `yaml:"script" json:"script"`
Playbook string `yaml:"playbook,omitempty" json:"playbook,omitempty"`
}

type Containerd struct {
Expand Down
5 changes: 3 additions & 2 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ func Validate(y LimaYAML, warn bool) error {
i, ProvisionModeDependency)
}
case ProvisionModeDependency:
case ProvisionModeAnsible:
default:
return fmt.Errorf("field `provision[%d].mode` must one of %q, %q, %q, or %q",
i, ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot, ProvisionModeDependency)
return fmt.Errorf("field `provision[%d].mode` must one of %q, %q, %q, %q, or %q",
i, ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot, ProvisionModeDependency, ProvisionModeAnsible)
}
if strings.Contains(p.Script, "LIMA_CIDATA") {
logrus.Warn("provisioning scripts should not reference the LIMA_CIDATA variables")
Expand Down
65 changes: 65 additions & 0 deletions pkg/start/ansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package start

import (
"context"
"os"
"os/exec"
"path/filepath"

"github.com/goccy/go-yaml"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store"
"github.com/sirupsen/logrus"
)

func runAnsibleProvision(ctx context.Context, inst *store.Instance) error {
y, err := inst.LoadYAML()
if err != nil {
return err
}
for _, f := range y.Provision {
if f.Mode == limayaml.ProvisionModeAnsible {
logrus.Infof("Waiting for ansible playbook %q", f.Playbook)
if err := runAnsiblePlaybook(ctx, inst, f.Playbook); err != nil {
return err
}
}
}
return nil
}

func runAnsiblePlaybook(ctx context.Context, inst *store.Instance, playbook string) error {
inventory, err := createInventory(inst)
if err != nil {
return err
}
logrus.Debugf("ansible-playbook -i %q %q", inventory, playbook)
args := []string{"-i", inventory, playbook}
cmd := exec.CommandContext(ctx, "ansible-playbook", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func createInventory(inst *store.Instance) (string, error) {
vars := map[string]interface{}{
"ansible_connection": "ssh",
"ansible_host": "lima-" + inst.Name,
"ansible_ssh_common_args": "-F " + inst.SSHConfigFile,
}
hosts := map[string]interface{}{
inst.Name: vars,
}
group := "lima"
data := map[string]interface{}{
group: map[string]interface{}{
"hosts": hosts,
},
}
bytes, err := yaml.Marshal(data)
if err != nil {
return "", err
}
inventory := filepath.Join(inst.Dir, "inventory.yaml")
return inventory, os.WriteFile(inventory, bytes, 0o644)
}
4 changes: 4 additions & 0 deletions pkg/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPat
return true
}

if xerr := runAnsibleProvision(ctx, inst); xerr != nil {
err = xerr
return true
}
if *inst.Config.Plain {
logrus.Infof("READY. Run `ssh -F %q lima-%s` to open the shell.", inst.SSHConfigFile, inst.Name)
} else {
Expand Down

0 comments on commit 830dd18

Please sign in to comment.