Completed
Branch development (67b918)
by Marc
02:48
created

AnsiblePlaybook::privateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/*
3
 * This file is part of the php-ansible package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Asm\Ansible\Command;
12
13
/**
14
 * Class AnsiblePlaybook
15
 *
16
 * @package Asm\Ansible\Command
17
 * @author Marc Aschmann <[email protected]>
18
 */
19
final class AnsiblePlaybook extends AbstractAnsibleCommand implements AnsiblePlaybookInterface
20
{
21
    /**
22
     * @var boolean
23
     */
24
    private $hasInventory = false;
25
26
    /**
27
     * Executes a command process.
28
     * Returns either exitcode or string output if no callback is given.
29
     *
30
     * @param null $callback
31
     * @return integer|string
32
     */
33
    public function execute($callback = null)
34
    {
35
        $this->checkInventory();
36
37
        return $this->runProcess($callback);
38
    }
39
40
    /**
41
     * The play to be executed.
42
     *
43
     * @param string $playbook
44
     * @return AnsiblePlaybookInterface
45
     */
46
    public function play(string $playbook): AnsiblePlaybookInterface
47
    {
48
        $this->addBaseoption($playbook);
49
50
        return $this;
51
    }
52
53
    /**
54
     * Ask for SSH password.
55
     *
56
     * @return AnsiblePlaybookInterface
57
     */
58
    public function askPass(): AnsiblePlaybookInterface
59
    {
60
        $this->addParameter('--ask-pass');
61
62
        return $this;
63
    }
64
65
    /**
66
     * Ask for su password.
67
     *
68
     * @return AnsiblePlaybookInterface
69
     */
70
    public function askSuPass(): AnsiblePlaybookInterface
71
    {
72
        $this->addParameter('--ask-su-pass');
73
74
        return $this;
75
    }
76
77
    /**
78
     * Ask for sudo password.
79
     *
80
     * @return AnsiblePlaybookInterface
81
     */
82
    public function askBecomePass(): AnsiblePlaybookInterface
83
    {
84
        $this->addParameter('--ask-become-pass');
85
86
        return $this;
87
    }
88
89
    /**
90
     * Ask for vault password.
91
     *
92
     * @return AnsiblePlaybookInterface
93
     */
94
    public function askVaultPass(): AnsiblePlaybookInterface
95
    {
96
        $this->addParameter('--ask-vault-pass');
97
98
        return $this;
99
    }
100
101
    /**
102
     * Don't make any changes; instead, try to predict some of the changes that may occur.
103
     *
104
     * @return AnsiblePlaybookInterface
105
     */
106
    public function check(): AnsiblePlaybookInterface
107
    {
108
        $this->addParameter('--check');
109
110
        return $this;
111
    }
112
113
    /**
114
     * Connection type to use (default=smart).
115
     *
116
     * @param string $connection
117
     * @return AnsiblePlaybookInterface
118
     */
119
    public function connection($connection = 'smart'): AnsiblePlaybookInterface
120
    {
121
        $this->addOption('--connection', $connection);
122
123
        return $this;
124
    }
125
126
    /**
127
     * When changing (small) files and templates, show the
128
     * differences in those files; works great with --check.
129
     *
130
     * @return AnsiblePlaybookInterface
131
     */
132
    public function diff(): AnsiblePlaybookInterface
133
    {
134
        $this->addParameter('--diff');
135
136
        return $this;
137
    }
138
139
    /**
140
     * Set additional variables as array [ 'key' => 'value' ] or string.
141
     *
142
     * @param string|array $extraVars
143
     * @return AnsiblePlaybookInterface
144
     */
145
    public function extraVars($extraVars = ''): AnsiblePlaybookInterface
146
    {
147
        $extraVars = $this->checkParam($extraVars, ' ');
148
        $this->addOption('--extra-vars', $extraVars);
149
150
        return $this;
151
    }
152
153
    /**
154
     * Run handlers even if a task fails.
155
     *
156
     * @return AnsiblePlaybookInterface
157
     */
158
    public function forceHandlers(): AnsiblePlaybookInterface
159
    {
160
        $this->addParameter('--force-handlers');
161
162
        return $this;
163
    }
164
165
    /**
166
     * Specify number of parallel processes to use (default=5).
167
     *
168
     * @param int $forks
169
     * @return AnsiblePlaybookInterface
170
     */
171
    public function forks($forks = 5): AnsiblePlaybookInterface
172
    {
173
        $this->addOption('--forks', $forks);
174
175
        return $this;
176
    }
177
178
    /**
179
     * Show help message and exit.
180
     *
181
     * @return AnsiblePlaybookInterface
182
     */
183
    public function help(): AnsiblePlaybookInterface
184
    {
185
        $this->addParameter('--help');
186
187
        return $this;
188
    }
189
190
    /**
191
     * Specify inventory host file (default=/etc/ansible/hosts).
192
     *
193
     * @param string $inventory filename for hosts file
194
     * @return AnsiblePlaybookInterface
195
     */
196
    public function inventoryFile($inventory = '/etc/ansible/hosts'): AnsiblePlaybookInterface
197
    {
198
        $this->addOption('--inventory-file', $inventory);
199
        $this->hasInventory = true;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Further limit selected hosts to an additional pattern.
206
     *
207
     * @param array|string $subset list of hosts
208
     * @return AnsiblePlaybookInterface
209
     */
210
    public function limit($subset = ''): AnsiblePlaybookInterface
211
    {
212
        $subset = $this->checkParam($subset, ',');
213
214
        $this->addOption('--limit', $subset);
215
216
        return $this;
217
    }
218
219
    /**
220
     * Outputs a list of matching hosts; does not execute anything else.
221
     *
222
     * @return AnsiblePlaybookInterface
223
     */
224
    public function listHosts(): AnsiblePlaybookInterface
225
    {
226
        $this->addParameter('--list-hosts');
227
228
        return $this;
229
    }
230
231
    /**
232
     * List all tasks that would be executed.
233
     *
234
     * @return AnsiblePlaybookInterface
235
     */
236
    public function listTasks(): AnsiblePlaybookInterface
237
    {
238
        $this->addParameter('--list-tasks');
239
240
        return $this;
241
    }
242
243
    /**
244
     * Specify path(s) to module library (default=/usr/share/ansible/).
245
     *
246
     * @param array $path list of paths for modules
247
     * @return AnsiblePlaybookInterface
248
     */
249
    public function modulePath($path = ['/usr/share/ansible/']): AnsiblePlaybookInterface
250
    {
251
        $this->addOption('--module-path', implode(',', $path));
252
253
        return $this;
254
    }
255
256
    /**
257
     * Disable cowsay
258
     *
259
     * @codeCoverageIgnore
260
     * @return AnsiblePlaybookInterface
261
     */
262
    public function noCows(): AnsiblePlaybookInterface
263
    {
264
        $this->processBuilder->setEnv('ANSIBLE_NOCOWS', 1);
0 ignored issues
show
Bug introduced by
The method setEnv() does not seem to exist on object<Asm\Ansible\Process\ProcessBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
265
266
        return $this;
267
    }
268
269
    /**
270
     * Enable/Disable Colors
271
     *
272
     * @param bool $colors
273
     * @return AnsiblePlaybookInterface
274
     */
275
    public function colors(bool $colors = true): AnsiblePlaybookInterface
276
    {
277
        $this->processBuilder->setEnv('ANSIBLE_FORCE_COLOR', $colors);
0 ignored issues
show
Bug introduced by
The method setEnv() does not seem to exist on object<Asm\Ansible\Process\ProcessBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
278
    }
279
280
    /**
281
     * Use this file to authenticate the connection.
282
     *
283
     * @param string $file private key file
284
     * @return AnsiblePlaybookInterface
285
     */
286
    public function privateKey(string $file): AnsiblePlaybookInterface
287
    {
288
        $this->addOption('--private-key', $file);
289
290
        return $this;
291
    }
292
293
    /**
294
     * Only run plays and tasks whose tags do not match these values.
295
     *
296
     * @param array|string $tags list of tags to skip
297
     * @return AnsiblePlaybookInterface
298
     */
299
    public function skipTags($tags = ''): AnsiblePlaybookInterface
300
    {
301
        $tags = $this->checkParam($tags, ',');
302
303
        $this->addOption('--skip-tags', $tags);
304
305
        return $this;
306
    }
307
308
    /**
309
     * Start the playbook at the task matching this name.
310
     *
311
     * @param string $task name of task
312
     * @return AnsiblePlaybookInterface
313
     */
314
    public function startAtTask(string $task): AnsiblePlaybookInterface
315
    {
316
        $this->addOption('--start-at-task', $task);
317
318
        return $this;
319
    }
320
321
    /**
322
     * One-step-at-a-time: confirm each task before running.
323
     *
324
     * @return AnsiblePlaybookInterface
325
     */
326
    public function step(): AnsiblePlaybookInterface
327
    {
328
        $this->addParameter('--step');
329
330
        return $this;
331
    }
332
333
    /**
334
     * Run operations with su.
335
     *
336
     * @return AnsiblePlaybookInterface
337
     */
338
    public function su(): AnsiblePlaybookInterface
339
    {
340
        $this->addParameter('--su');
341
342
        return $this;
343
    }
344
345
    /**
346
     * Run operations with su as this user (default=root).
347
     *
348
     * @param string $user
349
     * @return AnsiblePlaybookInterface
350
     */
351
    public function suUser(string $user = 'root'): AnsiblePlaybookInterface
352
    {
353
        $this->addOption('--su-user', $user);
354
355
        return $this;
356
    }
357
358
    /**
359
     * Enable privilege escalation
360
     *
361
     * @return AnsiblePlaybookInterface
362
     * @see http://docs.ansible.com/ansible/become.html
363
     */
364
    public function become(): AnsiblePlaybookInterface
365
    {
366
        $this->addParameter('--become');
367
368
        return $this;
369
    }
370
371
    /**
372
     * Desired sudo user (default=root).
373
     *
374
     * @param string $user
375
     * @return AnsiblePlaybookInterface
376
     */
377
    public function becomeUser(string $user = 'root'): AnsiblePlaybookInterface
378
    {
379
        $this->addOption('--become-user', $user);
380
381
        return $this;
382
    }
383
384
    /**
385
     * Perform a syntax check on the playbook, but do not execute it.
386
     *
387
     * @return AnsiblePlaybookInterface
388
     */
389
    public function syntaxCheck(): AnsiblePlaybookInterface
390
    {
391
        $this->addParameter('--syntax-check');
392
393
        return $this;
394
    }
395
396
    /**
397
     * Only run plays and tasks tagged with these values.
398
     *
399
     * @param string|array $tags list of tags
400
     * @return AnsiblePlaybookInterface
401
     */
402
    public function tags($tags): AnsiblePlaybookInterface
403
    {
404
        $tags = $this->checkParam($tags, ',');
405
406
        $this->addOption('--tags', $tags);
407
408
        return $this;
409
    }
410
411
    /**
412
     * Override the SSH timeout in seconds (default=10).
413
     *
414
     * @param int $timeout
415
     * @return AnsiblePlaybookInterface
416
     */
417
    public function timeout(int $timeout = 10): AnsiblePlaybookInterface
418
    {
419
        $this->addOption('--timeout', $timeout);
420
421
        return $this;
422
    }
423
424
    /**
425
     * Connect as this user.
426
     *
427
     * @param string $user
428
     * @return AnsiblePlaybookInterface
429
     */
430
    public function user(string $user): AnsiblePlaybookInterface
431
    {
432
        $this->addOption('--user', $user);
433
434
        return $this;
435
    }
436
437
    /**
438
     * Vault password file.
439
     *
440
     * @param string $file
441
     * @return AnsiblePlaybookInterface
442
     */
443
    public function vaultPasswordFile(string $file): AnsiblePlaybookInterface
444
    {
445
        $this->addoption('--vault-password-file', $file);
446
447
        return $this;
448
    }
449
450
    /**
451
     * Verbose mode (vvv for more, vvvv to enable connection debugging).
452
     *
453
     * @param string $verbose
454
     * @return AnsiblePlaybookInterface
455
     */
456
    public function verbose(string $verbose = 'v'): AnsiblePlaybookInterface
457
    {
458
        $this->addParameter('-' . $verbose);
459
460
        return $this;
461
    }
462
463
    /**
464
     * Get parameter string which will be used to call ansible.
465
     *
466
     * @param bool $asArray
467
     * @return string|array
468
     */
469
    public function getCommandlineArguments(bool $asArray = true)
470
    {
471
        $this->checkInventory();
472
473
        return $this->prepareArguments($asArray);
474
    }
475
476
    /**
477
     * Show program's version number and exit.
478
     *
479
     * @return AnsiblePlaybookInterface
480
     */
481
    public function version(): AnsiblePlaybookInterface
482
    {
483
        $this->addParameter('--version');
484
485
        return $this;
486
    }
487
488
    /**
489
     * If no inventory file is given, assume
490
     */
491
    private function checkInventory(): void
492
    {
493
        if (!$this->hasInventory) {
494
            $inventory = str_replace('.yml', '', $this->getBaseOptions());
495
            $this->inventoryFile($inventory);
496
        }
497
    }
498
}
499