-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathansible.go
More file actions
159 lines (134 loc) · 4.49 KB
/
Copy pathansible.go
File metadata and controls
159 lines (134 loc) · 4.49 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
*
* Copyright 2021 The Vitess Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/
package ansible
import (
"context"
"errors"
"io"
"os"
"path"
"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
"github.com/otiai10/copy"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
ErrorPathUnknown = "path does not exist"
flagAnsibleRoot = "ansible-root-directory"
flagInventoryFile = "ansible-inventory-file"
flagPlaybookFile = "ansible-playbook-file"
)
type Config struct {
RootDir string
InventoryFile string
PlaybookFile string
stdout io.Writer
stderr io.Writer
// ExtraVars is a key value map that will be passed to Ansible
// as extra variable using --extra-vars.
// The corresponding keys are defined as constants in the `vars.go` file of this package.
// This map gets filled by the Executor before the execution of Ansible.
// It contains all the required information to run the Ansible roles and tasks properly.
ExtraVars map[string]interface{}
}
func NewConfig() Config {
return Config{
ExtraVars: map[string]interface{}{},
}
}
func (c *Config) AddExtraVar(key string, value interface{}) {
c.ExtraVars[key] = value
}
func (c *Config) AddToViper(v *viper.Viper) {
_ = v.UnmarshalKey(flagAnsibleRoot, &c.RootDir)
_ = v.UnmarshalKey(flagInventoryFile, &c.InventoryFile)
_ = v.UnmarshalKey(flagPlaybookFile, &c.PlaybookFile)
}
func (c *Config) AddToPersistentCommand(cmd *cobra.Command) {
cmd.Flags().StringVar(&c.RootDir, flagAnsibleRoot, "", "Root directory of Ansible")
cmd.Flags().StringVar(&c.InventoryFile, flagInventoryFile, "", "Inventory file used by Ansible")
cmd.Flags().StringVar(&c.PlaybookFile, flagPlaybookFile, "", "Playbook file used by Ansible")
_ = viper.BindPFlag(flagAnsibleRoot, cmd.Flags().Lookup(flagAnsibleRoot))
_ = viper.BindPFlag(flagInventoryFile, cmd.Flags().Lookup(flagInventoryFile))
_ = viper.BindPFlag(flagPlaybookFile, cmd.Flags().Lookup(flagPlaybookFile))
}
func applyRootToFiles(root string, file *string) {
if !path.IsAbs(*file) {
*file = path.Join(root, *file)
}
}
func Run(c *Config) error {
applyRootToFiles(c.RootDir, &c.PlaybookFile)
applyRootToFiles(c.RootDir, &c.InventoryFile)
ansiblePlaybookConnectionOptions := &options.AnsibleConnectionOptions{
User: "ubuntu",
SSHCommonArgs: "-o StrictHostKeyChecking=no",
}
ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Inventory: c.InventoryFile,
ExtraVars: c.ExtraVars,
}
ansiblePlaybookPrivilegeEscalationOptions := &options.AnsiblePrivilegeEscalationOptions{
Become: true,
}
plb := &playbook.AnsiblePlaybookCmd{
Playbooks: []string{c.PlaybookFile},
ConnectionOptions: ansiblePlaybookConnectionOptions,
PrivilegeEscalationOptions: ansiblePlaybookPrivilegeEscalationOptions,
Options: ansiblePlaybookOptions,
Exec: execute.NewDefaultExecute(
execute.WithShowDuration(),
execute.WithWrite(c.stdout),
execute.WithWriteError(c.stderr),
// ansible-playbook only discovers an ansible.cfg from the current
// working directory, which is the api process' cwd rather than the
// per-execution playbook directory. Point it explicitly at the
// ansible.cfg copied alongside the playbook so its callbacks (e.g.
// profile_tasks) are enabled.
execute.WithEnvVar("ANSIBLE_CONFIG", path.Join(c.RootDir, "ansible.cfg")),
),
}
err := plb.Run(context.TODO())
if err != nil {
return err
}
return nil
}
func (c *Config) CopyRootDirectory(directory string) error {
if _, err := os.Stat(directory); os.IsNotExist(err) {
return errors.New(ErrorPathUnknown)
}
err := copy.Copy(c.RootDir, directory)
if err != nil {
return err
}
c.RootDir = directory
return nil
}
func (c *Config) SetStdout(stdout *os.File) {
c.stdout = stdout
}
func (c *Config) SetStderr(stderr *os.File) {
c.stderr = stderr
}
func (c *Config) SetOutputs(stdout, stderr *os.File) {
c.stdout = stdout
c.stderr = stderr
}