-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ansible provision mode for remote playbook
Signed-off-by: Anders F Björklund <[email protected]>
- Loading branch information
1 parent
f8fbae4
commit 830dd18
Showing
5 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters