-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible.go
57 lines (49 loc) · 1.59 KB
/
ansible.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"bytes"
"fmt"
"html/template"
"os"
)
const ansiblesh = `
apt && apt update && apt install -y ansible
yum && yum install -y ansible
`
const ansibleHostsTemplate = `{{ $ssh := .SSH }}
default ansible_host={{ $ssh.Host }} ansible_user={{ $ssh.User }} ansible_port={{ $ssh.Port }} ansible_ssh_private_key_file={{ $ssh.IdentityFile }} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
{{ if .Groups }}
{{ range .Groups -}}
[{{ . }}]
default ansible_host={{ $ssh.Host }} ansible_user={{ $ssh.User }} ansible_port={{ $ssh.Port }} ansible_ssh_private_key_file={{ $ssh.IdentityFile }} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
{{ end }}
{{ end }}
`
type ansibleHost struct {
SSH sshItems
Groups []string
}
// generates ansible hosts file for node <vagrantdir>/ansiblehosts_<nodename>
func generateAnsibleHosts(nodeName string, provisioner Provisioner, vagrantDir string) (err error) {
if _, statErr := os.Stat(fmt.Sprintf("%s/ansiblehosts_%s", vagrantDir, nodeName)); os.IsNotExist(statErr) {
sshConn, _ := getVagrantSSHDetails(vagrantDir, nodeName)
if err != nil {
return err
}
var ansibleHost ansibleHost
ansibleHost.SSH = sshConn
ansibleHost.Groups = provisioner.Groups
var tpl bytes.Buffer
vc, err := template.New("hosts").Parse(ansibleHostsTemplate)
if err != nil {
fmt.Println("Error:", err)
}
err = vc.Execute(&tpl, ansibleHost)
ansibleHostsFileContent := tpl.String()
os.Chdir(vagrantDir)
if err := writeFile(fmt.Sprintf("ansiblehosts_%s", nodeName), ansibleHostsFileContent); err != nil {
return err
}
os.Chdir("..")
}
return
}