forked from drone-plugins/drone-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
243 lines (234 loc) · 6.32 KB
/
main.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"log"
"os"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var (
version = "unknown"
)
func main() {
app := cli.NewApp()
app.Name = "ansible plugin"
app.Usage = "ansible plugin"
app.Action = run
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "requirements",
Usage: "path to python requirements",
EnvVar: "PLUGIN_REQUIREMENTS",
},
cli.StringFlag{
Name: "galaxy",
Usage: "path to galaxy requirements",
EnvVar: "PLUGIN_GALAXY",
},
cli.StringSliceFlag{
Name: "inventory",
Usage: "specify inventory host path",
EnvVar: "PLUGIN_INVENTORY,PLUGIN_INVENTORIES",
},
cli.StringSliceFlag{
Name: "playbook",
Usage: "list of playbooks to apply",
EnvVar: "PLUGIN_PLAYBOOK,PLUGIN_PLAYBOOKS",
},
cli.StringFlag{
Name: "limit",
Usage: "further limit selected hosts to an additional pattern",
EnvVar: "PLUGIN_LIMIT",
},
cli.StringFlag{
Name: "skip-tags",
Usage: "only run plays and tasks whose tags do not match",
EnvVar: "PLUGIN_SKIP_TAGS",
},
cli.StringFlag{
Name: "start-at-task",
Usage: "start the playbook at the task matching this name",
EnvVar: "PLUGIN_START_AT_TASK",
},
cli.StringFlag{
Name: "tags",
Usage: "only run plays and tasks tagged with these values",
EnvVar: "PLUGIN_TAGS",
},
cli.StringSliceFlag{
Name: "extra-vars",
Usage: "set additional variables as key=value",
EnvVar: "PLUGIN_EXTRA_VARS,ANSIBLE_EXTRA_VARS",
},
cli.StringSliceFlag{
Name: "module-path",
Usage: "prepend paths to module library",
EnvVar: "PLUGIN_MODULE_PATH",
},
cli.BoolFlag{
Name: "check",
Usage: "run a check, do not apply any changes",
EnvVar: "PLUGIN_CHECK",
},
cli.BoolFlag{
Name: "diff",
Usage: "show the differences, may print secrets",
EnvVar: "PLUGIN_DIFF",
},
cli.BoolFlag{
Name: "flush-cache",
Usage: "clear the fact cache for every host in inventory",
EnvVar: "PLUGIN_FLUSH_CACHE",
},
cli.BoolFlag{
Name: "force-handlers",
Usage: "run handlers even if a task fails",
EnvVar: "PLUGIN_FORCE_HANDLERS",
},
cli.BoolFlag{
Name: "list-hosts",
Usage: "outputs a list of matching hosts",
EnvVar: "PLUGIN_LIST_HOSTS",
},
cli.BoolFlag{
Name: "list-tags",
Usage: "list all available tags",
EnvVar: "PLUGIN_LIST_TAGS",
},
cli.BoolFlag{
Name: "list-tasks",
Usage: "list all tasks that would be executed",
EnvVar: "PLUGIN_LIST_TASKS",
},
cli.BoolFlag{
Name: "syntax-check",
Usage: "perform a syntax check on the playbook",
EnvVar: "PLUGIN_SYNTAX_CHECK",
},
cli.IntFlag{
Name: "forks",
Usage: "specify number of parallel processes to use",
EnvVar: "PLUGIN_FORKS",
Value: 5,
},
cli.StringFlag{
Name: "vault-id",
Usage: "the vault identity to use",
EnvVar: "PLUGIN_VAULT_ID,ANSIBLE_VAULT_ID",
},
cli.StringFlag{
Name: "vault-password",
Usage: "the vault password to use",
EnvVar: "PLUGIN_VAULT_PASSWORD,ANSIBLE_VAULT_PASSWORD",
},
cli.IntFlag{
Name: "verbose",
Usage: "level of verbosity, 0 up to 4",
EnvVar: "PLUGIN_VERBOSE",
},
cli.StringFlag{
Name: "private-key",
Usage: "use this key to authenticate the connection",
EnvVar: "PLUGIN_PRIVATE_KEY,ANSIBLE_PRIVATE_KEY",
},
cli.StringFlag{
Name: "user",
Usage: "connect as this user",
EnvVar: "PLUGIN_USER,ANSIBLE_USER",
},
cli.StringFlag{
Name: "connection",
Usage: "connection type to use",
EnvVar: "PLUGIN_CONNECTION",
},
cli.IntFlag{
Name: "timeout",
Usage: "override the connection timeout in seconds",
EnvVar: "PLUGIN_TIMEOUT",
},
cli.StringFlag{
Name: "ssh-common-args",
Usage: "specify common arguments to pass to sftp/scp/ssh",
EnvVar: "PLUGIN_SSH_COMMON_ARGS",
},
cli.StringFlag{
Name: "sftp-extra-args",
Usage: "specify extra arguments to pass to sftp only",
EnvVar: "PLUGIN_SFTP_EXTRA_ARGS",
},
cli.StringFlag{
Name: "scp-extra-args",
Usage: "specify extra arguments to pass to scp only",
EnvVar: "PLUGIN_SCP_EXTRA_ARGS",
},
cli.StringFlag{
Name: "ssh-extra-args",
Usage: "specify extra arguments to pass to ssh only",
EnvVar: "PLUGIN_SSH_EXTRA_ARGS",
},
cli.BoolFlag{
Name: "become",
Usage: "run operations with become",
EnvVar: "PLUGIN_BECOME",
},
cli.StringFlag{
Name: "become-method",
Usage: "privilege escalation method to use",
EnvVar: "PLUGIN_BECOME_METHOD,ANSIBLE_BECOME_METHOD",
},
cli.StringFlag{
Name: "become-user",
Usage: "run operations as this user",
EnvVar: "PLUGIN_BECOME_USER,ANSIBLE_BECOME_USER",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
plugin := Plugin{
Config: Config{
Requirements: c.String("requirements"),
Galaxy: c.String("galaxy"),
Inventories: c.StringSlice("inventory"),
Playbooks: c.StringSlice("playbook"),
Limit: c.String("limit"),
SkipTags: c.String("skip-tags"),
StartAtTask: c.String("start-at-task"),
Tags: c.String("tags"),
ExtraVars: c.StringSlice("extra-vars"),
ModulePath: c.StringSlice("module-path"),
Check: c.Bool("check"),
Diff: c.Bool("diff"),
FlushCache: c.Bool("flush-cache"),
ForceHandlers: c.Bool("force-handlers"),
ListHosts: c.Bool("list-hosts"),
ListTags: c.Bool("list-tags"),
ListTasks: c.Bool("list-tasks"),
SyntaxCheck: c.Bool("syntax-check"),
Forks: c.Int("forks"),
VaultID: c.String("vailt-id"),
VaultPassword: c.String("vault-password"),
Verbose: c.Int("verbose"),
PrivateKey: c.String("private-key"),
User: c.String("user"),
Connection: c.String("connection"),
Timeout: c.Int("timeout"),
SSHCommonArgs: c.String("ssh-common-args"),
SFTPExtraArgs: c.String("sftp-extra-args"),
SCPExtraArgs: c.String("scp-extra-args"),
SSHExtraArgs: c.String("ssh-extra-args"),
Become: c.Bool("become"),
BecomeMethod: c.String("become-method"),
BecomeUser: c.String("become-user"),
},
}
if len(plugin.Config.Playbooks) == 0 {
return errors.New("you must provide a playbook")
}
if len(plugin.Config.Inventories) == 0 {
return errors.New("you must provide an inventory")
}
return plugin.Exec()
}