Passed
Push — master ( ac7040...ce8f56 )
by Marc
01:14
created

AnsiblePlaybook::syntaxCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
1 ignored issue
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $callback is correct as it would always require null to be passed?
Loading history...
31
     * @return integer|string
32
     */
33 1
    public function execute($callback = null)
34
    {
35 1
        $this->checkInventory();
36
37 1
        return $this->runProcess($callback);
38
    }
39
40
    /**
41
     * The play to be executed.
42
     *
43
     * @param string $playbook
44
     * @return AnsiblePlaybookInterface
45
     */
46 30
    public function play(string $playbook): AnsiblePlaybookInterface
47
    {
48 30
        $this->addBaseoption($playbook);
49
50 30
        return $this;
51
    }
52
53
    /**
54
     * Ask for SSH password.
55
     *
56
     * @return AnsiblePlaybookInterface
57
     */
58 1
    public function askPass(): AnsiblePlaybookInterface
59
    {
60 1
        $this->addParameter('--ask-pass');
61
62 1
        return $this;
63
    }
64
65
    /**
66
     * Ask for su password.
67
     *
68
     * @return AnsiblePlaybookInterface
69
     */
70 1
    public function askSuPass(): AnsiblePlaybookInterface
71
    {
72 1
        $this->addParameter('--ask-su-pass');
73
74 1
        return $this;
75
    }
76
77
    /**
78
     * Ask for sudo password.
79
     *
80
     * @return AnsiblePlaybookInterface
81
     */
82 1
    public function askBecomePass(): AnsiblePlaybookInterface
83
    {
84 1
        $this->addParameter('--ask-become-pass');
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * Ask for vault password.
91
     *
92
     * @return AnsiblePlaybookInterface
93
     */
94 1
    public function askVaultPass(): AnsiblePlaybookInterface
95
    {
96 1
        $this->addParameter('--ask-vault-pass');
97
98 1
        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 1
    public function check(): AnsiblePlaybookInterface
107
    {
108 1
        $this->addParameter('--check');
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * Connection type to use (default=smart).
115
     *
116
     * @param string $connection
117
     * @return AnsiblePlaybookInterface
118
     */
119 1
    public function connection($connection = 'smart'): AnsiblePlaybookInterface
120
    {
121 1
        $this->addOption('--connection', $connection);
122
123 1
        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 1
    public function diff(): AnsiblePlaybookInterface
133
    {
134 1
        $this->addParameter('--diff');
135
136 1
        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 1
    public function extraVars($extraVars = ''): AnsiblePlaybookInterface
146
    {
147 1
        $extraVars = $this->checkParam($extraVars, ' ');
148 1
        $this->addOption('--extra-vars', $extraVars);
149
150 1
        return $this;
151
    }
152
153
    /**
154
     * Run handlers even if a task fails.
155
     *
156
     * @return AnsiblePlaybookInterface
157
     */
158 1
    public function forceHandlers(): AnsiblePlaybookInterface
159
    {
160 1
        $this->addParameter('--force-handlers');
161
162 1
        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 1
    public function forks($forks = 5): AnsiblePlaybookInterface
172
    {
173 1
        $this->addOption('--forks', $forks);
174
175 1
        return $this;
176
    }
177
178
    /**
179
     * Show help message and exit.
180
     *
181
     * @return AnsiblePlaybookInterface
182
     */
183 1
    public function help(): AnsiblePlaybookInterface
184
    {
185 1
        $this->addParameter('--help');
186
187 1
        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 1
    public function inventoryFile($inventory = '/etc/ansible/hosts'): AnsiblePlaybookInterface
197
    {
198 1
        $this->addOption('--inventory-file', $inventory);
199 1
        $this->hasInventory = true;
200
201 1
        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 2
    public function limit($subset = ''): AnsiblePlaybookInterface
211
    {
212 2
        $subset = $this->checkParam($subset, ',');
213
214 2
        $this->addOption('--limit', $subset);
215
216 2
        return $this;
217
    }
218
219
    /**
220
     * Outputs a list of matching hosts; does not execute anything else.
221
     *
222
     * @return AnsiblePlaybookInterface
223
     */
224 1
    public function listHosts(): AnsiblePlaybookInterface
225
    {
226 1
        $this->addParameter('--list-hosts');
227
228 1
        return $this;
229
    }
230
231
    /**
232
     * List all tasks that would be executed.
233
     *
234
     * @return AnsiblePlaybookInterface
235
     */
236 1
    public function listTasks(): AnsiblePlaybookInterface
237
    {
238 1
        $this->addParameter('--list-tasks');
239
240 1
        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 1
    public function modulePath($path = ['/usr/share/ansible/']): AnsiblePlaybookInterface
250
    {
251 1
        $this->addOption('--module-path', implode(',', $path));
252
253 1
        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 exist on Asm\Ansible\Process\ProcessBuilderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
        $this->processBuilder->/** @scrutinizer ignore-call */ 
265
                               setEnv('ANSIBLE_NOCOWS', 1);

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 1
    public function colors(bool $colors = true): AnsiblePlaybookInterface
276
    {
277 1
        $this->processBuilder->setEnv('ANSIBLE_FORCE_COLOR', $colors);
278
    }
279 1
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 1
        $this->addOption('--private-key', $file);
289
290 1
        return $this;
291
    }
292 1
293
    /**
294 1
     * 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 1
        $this->addOption('--skip-tags', $tags);
304
305 1
        return $this;
306
    }
307 1
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 1
    {
316
        $this->addOption('--start-at-task', $task);
317 1
318
        return $this;
319 1
    }
320
321
    /**
322
     * One-step-at-a-time: confirm each task before running.
323
     *
324
     * @return AnsiblePlaybookInterface
325
     */
326
    public function step(): AnsiblePlaybookInterface
327 1
    {
328
        $this->addParameter('--step');
329 1
330
        return $this;
331 1
    }
332
333
    /**
334
     * Run operations with su.
335
     *
336
     * @return AnsiblePlaybookInterface
337
     */
338
    public function su(): AnsiblePlaybookInterface
339
    {
340 1
        $this->addParameter('--su');
341
342 1
        return $this;
343
    }
344 1
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 1
        $this->addOption('--su-user', $user);
354
355 1
        return $this;
356
    }
357 1
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 1
        $this->addParameter('--become');
367
368 1
        return $this;
369
    }
370 1
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 1
    {
379
        $this->addOption('--become-user', $user);
380 1
381
        return $this;
382 1
    }
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 1
        $this->addParameter('--syntax-check');
392
393 1
        return $this;
394
    }
395 1
396
    /**
397 1
     * 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 1
        $this->addOption('--tags', $tags);
407
408 1
        return $this;
409
    }
410 1
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 2
        $this->addOption('--timeout', $timeout);
420
421 2
        return $this;
422
    }
423 2
424
    /**
425
     * Connect as this user.
426
     *
427
     * @param string $user
428
     * @return AnsiblePlaybookInterface
429
     */
430
    public function user(string $user): AnsiblePlaybookInterface
431
    {
432 1
        $this->addOption('--user', $user);
433
434 1
        return $this;
435
    }
436 1
437
    /**
438
     * Vault password file.
439
     *
440
     * @param string $file
441
     * @return AnsiblePlaybookInterface
442
     */
443
    public function vaultPasswordFile(string $file): AnsiblePlaybookInterface
444
    {
445 1
        $this->addoption('--vault-password-file', $file);
446
447 1
        return $this;
448
    }
449 1
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 30
        $this->addParameter('-' . $verbose);
459
460 30
        return $this;
461
    }
462 30
463
    /**
464
     * Show program's version number and exit.
465
     *
466
     * @return AnsiblePlaybookInterface
467
     */
468
    public function version(): AnsiblePlaybookInterface
469
    {
470 1
        $this->addParameter('--version');
471
472 1
        return $this;
473
    }
474 1
475
    /**
476
     * clear the fact cache
477
     *
478
     * @return AnsiblePlaybookInterface
479
     */
480 31
    public function flushCache(): AnsiblePlaybookInterface
481
    {
482 31
         $this->addParameter('--flush-cache');
483 1
484 1
         return $this;
485 1
    }
486 31
487
    /**
488
     * the new vault identity to use for rekey
489
     *
490
     * @param string $vaultId
491
     * @return AnsiblePlaybookInterface
492
     */
493
    public function newVaultId(string $vaultId): AnsiblePlaybookInterface
494
    {
495
        $this->addOption('--new-vault-id', $vaultId);
496
497
        return $this;
498
    }
499
500
    /**
501
     * new vault password file for rekey
502
     *
503
     * @param string $passwordFile
504
     * @return AnsiblePlaybookInterface
505
     */
506
    public function newVaultPasswordFile(string $passwordFile): AnsiblePlaybookInterface
507
    {
508
        $this->addOption('--new-vault-password-file', $passwordFile);
509
510
        return $this;
511
    }
512
513
    /**
514
     * specify extra arguments to pass to scp only (e.g. -l)
515
     *
516
     * @param string $scpExtraArgs
517
     * @return AnsiblePlaybookInterface
518
     */
519
    public function scpExtraArgs(string $scpExtraArgs): AnsiblePlaybookInterface
520
    {
521
        $this->addOption('--scp-extra-args', $scpExtraArgs);
522
523
        return $this;
524
    }
525
526
    /**
527
     * specify extra arguments to pass to sftp only (e.g. -f, -l)
528
     *
529
     * @param string $sftpExtraArgs
530
     * @return AnsiblePlaybookInterface
531
     */
532
    public function sftpExtraArgs(string $sftpExtraArgs): AnsiblePlaybookInterface
533
    {
534
        $this->addOption('--sftp-extra-args', $sftpExtraArgs);
535
536
        return $this;
537
    }
538
539
    /**
540
     * specify common arguments to pass to sftp/scp/ssh (e.g. ProxyCommand)
541
     *
542
     * @param string $sshArgs
543
     * @return AnsiblePlaybookInterface
544
     */
545
    public function sshCommonArgs(string $sshArgs): AnsiblePlaybookInterface
546
    {
547
        $this->addOption('--ssh-common-args', $sshArgs);
548
549
        return $this;
550
    }
551
552
    /**
553
     * specify extra arguments to pass to ssh only (e.g. -R)
554
     *
555
     * @param string $extraArgs
556
     * @return AnsiblePlaybookInterface
557
     */
558
    public function sshExtraArgs(string $extraArgs): AnsiblePlaybookInterface
559
    {
560
        $this->addOption('--ssh-extra-args', $extraArgs);
561
562
        return $this;
563
    }
564
565
    /**
566
     * the vault identity to use
567
     *
568
     * @param string $vaultId
569
     * @return AnsiblePlaybookInterface
570
     */
571
    public function vaultId(string $vaultId): AnsiblePlaybookInterface
572
    {
573
        $this->addOption('--vault-id', $vaultId);
574
575
        return $this;
576
    }
577
578
    /**
579
     * Get parameter string which will be used to call ansible.
580
     *
581
     * @param bool $asArray
582
     * @return string|array
583
     */
584
    public function getCommandlineArguments(bool $asArray = true)
585
    {
586
        $this->checkInventory();
587
588
        return $this->prepareArguments($asArray);
589
    }
590
591
    /**
592
     * If no inventory file is given, assume
593
     */
594
    private function checkInventory(): void
595
    {
596
        if (!$this->hasInventory) {
597
            $inventory = str_replace('.yml', '', $this->getBaseOptions());
598
            $this->inventoryFile($inventory);
599
        }
600
    }
601
}
602