-
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.
Merge pull request #2283 from afbjorklund/ansible
Support ansible provision mode for remote playbook
- Loading branch information
Showing
12 changed files
with
142 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- hosts: all | ||
tasks: | ||
- name: Create test file | ||
file: | ||
path: /tmp/ansible | ||
state: touch |
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
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,66 @@ | ||
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/lima-vm/lima/pkg/store/filenames" | ||
"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 := createAnsibleInventory(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 createAnsibleInventory(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, filenames.AnsibleInventoryYAML) | ||
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
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