Skip to content

Commit

Permalink
add conditional output for playbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
maschmann committed Sep 14, 2024
1 parent af16769 commit c7dd13b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Asm/Ansible/Command/AbstractAnsibleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ abstract class AbstractAnsibleCommand

private array $baseOptions;

private bool $useStdoutForError;

/**
* @param ProcessBuilderInterface $processBuilder
* @param LoggerInterface|null $logger
Expand All @@ -44,6 +46,7 @@ public function __construct(ProcessBuilderInterface $processBuilder, LoggerInter
$this->options = [];
$this->parameters = [];
$this->baseOptions = [];
$this->useStdoutForError = false;
$this->setLogger($logger ?? new NullLogger());
}

Expand Down Expand Up @@ -183,7 +186,7 @@ protected function runProcess(?callable $callback): int|string

// if no callback is set, and the process is not successful, we return the output
if (false === $process->isSuccessful()) {
return $process->getErrorOutput();
return $this->useStdoutForError ? $process->getOutput() : $process->getErrorOutput();
}

// if no callback is set, and the process is successful, we return the output
Expand All @@ -210,4 +213,14 @@ protected function getProcessCommandline(Process $process): string

return sprintf('%s %s', implode(' ', $vars), $commandline);
}

/**
* in case ansible explicitly is in json mode, this will be set to be able to get error outputs

Check failure on line 218 in Asm/Ansible/Command/AbstractAnsibleCommand.php

View workflow job for this annotation

GitHub Actions / build (8.3, 11, phpunit.xml.dist)

Whitespace found at end of line
*
* @return void
*/
protected function useStdoutForError(): void
{
$this->useStdoutForError = true;
}
}
1 change: 1 addition & 0 deletions Asm/Ansible/Command/AnsiblePlaybook.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public function colors(bool $colors = true): AnsiblePlaybookInterface
public function json(): AnsiblePlaybookInterface
{
$this->processBuilder->setEnv('ANSIBLE_STDOUT_CALLBACK', 'json');
$this->useStdoutForError();

return $this;
}
Expand Down
32 changes: 32 additions & 0 deletions Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,38 @@ public function testReturnsErrorOutputIfProcessWasNotSuccessful(): void
self::assertEquals('error output', $playbook->execute());
}

public function testReturnsErrorOutputIfProcessWasNotSuccessfulInJsonMode(): void
{
$builder = $this->createMock(ProcessBuilderInterface::class);
$builder
->expects(self::once())
->method('setArguments')
->willReturnSelf();
$builder
->expects(self::once())
->method('getProcess')
->willReturn($process = $this->createMock(Process::class));
$process
->expects(self::once())
->method('run');
$process
->expects(self::once())
->method('isSuccessful')
->willReturn(false);
$process
->expects(self::once())
->method('getOutput')
->willReturn('{ "error": "error output" }');
$process
->expects(self::never())
->method('getErrorOutput');

$playbook = new AnsiblePlaybook($builder);
$playbook->json();

self::assertEquals('{ "error": "error output" }', $playbook->execute());
}

public function testReturnsNormalOutputIfProcessWasSuccessful(): void
{
$builder = $this->createMock(ProcessBuilderInterface::class);
Expand Down

0 comments on commit c7dd13b

Please sign in to comment.