-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmain.go
387 lines (378 loc) · 11.7 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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: "mode",
Usage: "Mode of the functionality",
EnvVar: "PLUGIN_MODE",
},
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.BoolTFlag{
Name: "galaxy-force",
Usage: "force overwriting an existing role or collection",
EnvVar: "PLUGIN_GALAXY_FORCE",
},
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",
},
cli.BoolFlag{
Name: "disable-host-key-checking",
Usage: "Disable validation of the host's SSH server keys",
EnvVar: "PLUGIN_DISABLE_HOST_KEY_CHECKING",
},
cli.BoolFlag{
Name: "host-key-checking",
Usage: "Enable validation of the host's SSH server keys",
EnvVar: "PLUGIN_HOST_KEY_CHECKING",
},
cli.StringFlag{
Name: "installation",
Usage: "Specify the path to Ansible installation",
EnvVar: "PLUGIN_INSTALLATION",
},
cli.StringFlag{
Name: "inventory-content",
Usage: "Inline inventory content as a string",
EnvVar: "PLUGIN_INVENTORY_CONTENT",
},
cli.BoolFlag{
Name: "sudo",
Usage: "Use sudo for operations",
EnvVar: "PLUGIN_SUDO",
},
cli.StringFlag{
Name: "sudo-user",
Usage: "Specify the sudo user (default: root)",
EnvVar: "PLUGIN_SUDO_USER",
},
cli.StringFlag{
Name: "vault-tmp-path",
Usage: "Temporary path for generated vault files",
EnvVar: "PLUGIN_VAULT_TMP_PATH",
},
// Ad-Hoc Specific Flags
cli.StringFlag{
Name: "hosts",
Usage: "Target hosts for ad-hoc command",
EnvVar: "PLUGIN_HOSTS",
},
cli.StringFlag{
Name: "module",
Usage: "Module name for ad-hoc execution",
EnvVar: "PLUGIN_MODULE",
},
cli.StringFlag{
Name: "module-arguments",
Usage: "Arguments for the specified module",
EnvVar: "PLUGIN_MODULE_ARGUMENTS",
},
cli.BoolFlag{
Name: "dynamic-inventory",
Usage: "Enable dynamic inventory",
EnvVar: "PLUGIN_DYNAMIC_INVENTORY",
},
cli.StringFlag{
Name: "extras",
Usage: "Additional options for ad-hoc execution",
EnvVar: "PLUGIN_EXTRAS",
},
cli.StringFlag{
Name: "vault-credentials-key",
Usage: "Vault credentials ID for encrypted files",
EnvVar: "PLUGIN_VAULT_CREDENTIALS_KEY",
},
// Vault Specific Flags
cli.StringFlag{
Name: "action",
Usage: "Action for ansible-vault (e.g., encrypt, decrypt, view)",
EnvVar: "PLUGIN_ACTION",
},
cli.StringFlag{
Name: "content",
Usage: "Content to encrypt or decrypt",
EnvVar: "PLUGIN_CONTENT",
},
cli.StringFlag{
Name: "input",
Usage: "Input file for the vault operation",
EnvVar: "PLUGIN_INPUT",
},
cli.StringFlag{
Name: "output",
Usage: "Output file for the vault operation",
EnvVar: "PLUGIN_OUTPUT",
},
cli.StringFlag{
Name: "new-vault-credentials-key",
Usage: "New Vault Credentials Key for rekeying",
EnvVar: "PLUGIN_NEW_VAULT_CREDENTIALS_KEY",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
plugin := Plugin{
Config: Config{
Mode: c.String("mode"),
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"),
GalaxyForce: c.Bool("galaxy-force"),
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"),
DisableHostKeyChecking: c.Bool("disable-host-key-checking"), // Disable SSH host key checking
HostKeyChecking: c.Bool("host-key-checking"), // Enable SSH host key validation
Installation: c.String("installation"), // Path to the Ansible executable or installation
InventoryContent: c.String("inventory-content"), // Inline inventory content
Sudo: c.Bool("sudo"), // Use sudo for operations
SudoUser: c.String("sudo-user"), // Sudo user for operations
VaultTmpPath: c.String("vault-tmp-path"), // Temporary path for vault password files and others
// Ad-Hoc Parameters
Hosts: c.String("hosts"), // Target hosts for ad-hoc command
Module: c.String("module"), // Module name for ad-hoc command
ModuleArguments: c.String("module-arguments"), // Module arguments for ad-hoc command
DynamicInventory: c.Bool("dynamic-inventory"), // Enable dynamic inventory
Extras: c.String("extras"), // Additional options for ad-hoc execution
VaultCredentialsKey: c.String("vault-credentials-key"), // Vault credentials ID for encrypted files
// Vault Parameters
Action: c.String("action"),
Content: c.String("content"),
Input: c.String("input"),
Output: c.String("output"),
NewVaultCredentialsKey: c.String("new-vault-credentials-key"),
},
}
// Set default mode to "playbook" if not explicitly provided
if plugin.Config.Mode == "" {
plugin.Config.Mode = "playbook"
}
// Validate mode and required parameters based on the mode
switch plugin.Config.Mode {
case "playbook":
if len(plugin.Config.Playbooks) == 0 {
return errors.New("you must provide a playbook in playbook mode")
}
if len(plugin.Config.Inventories) == 0 && plugin.Config.InventoryContent == "" {
return errors.New("you must provide an inventory or inventory content in playbook mode")
}
case "adhoc":
if plugin.Config.Hosts == "" {
return errors.New("you must provide hosts for adhoc mode")
}
// Module is optional; defaults to "command" if not provided
case "vault":
if plugin.Config.VaultCredentialsKey == "" {
return errors.New("VaultCredentialsKey is mandatory for vault mode")
}
// Action, Content, Input, and Output are optional
default:
return errors.New("invalid mode: specify 'playbook', 'adhoc', or 'vault'")
}
return plugin.Exec()
}