Last active
February 19, 2021 01:17
-
-
Save sinri/fc551e2898c0307d29140fb8e52a9f96 to your computer and use it in GitHub Desktop.
Ansible Output Glass, usage `ansible-playbook -vvv ... | php glass.php`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright 2017 Sirni Edogawa | |
* usage `ansible-playbook -vvv ... | php glass.php` | |
*/ | |
echo "[Ansible Glass Version Alpha Online]".PHP_EOL; | |
$handle = fopen("php://stdin", "r"); | |
if ($handle) { | |
while (($buffer = fgets($handle)) !== false) { | |
process_line($buffer); | |
} | |
if (!feof($handle)) { | |
echo "[Ansible Glass]Error: unexpected fgets() fail\n"; | |
} | |
fclose($handle); | |
} | |
function process_line($line) | |
{ | |
echo $line.PHP_EOL; | |
if (strpos($line, 'changed:')===0) { | |
//std format | |
$machine='?'; | |
if (preg_match('/^changed: \[([A-Za-z0-9_]+)\] =>/', $line, $match)) { | |
$machine=$match[1]; | |
} | |
$k=strpos($line, '=>'); | |
$json=substr($line, $k+3); | |
$info=json_decode($json, true); | |
echo "> changed: [{$machine}] ".($info['changed']?'TRUE':'FALSE').PHP_EOL; | |
echo "> start: {$info['start']}".PHP_EOL; | |
echo "> end: {$info['end']}".PHP_EOL; | |
echo "> delta: {$info['delta']}".PHP_EOL; | |
echo "$ {$info['cmd']} ".PHP_EOL; | |
foreach ($info['stdout_lines'] as $l) { | |
echo '> '.$l.PHP_EOL; | |
} | |
echo '! '.$info['stderr'].PHP_EOL; | |
} elseif (strpos($line, 'fatal:')===0) { | |
$machine='?'; | |
if (preg_match('/^fatal: \[([A-Za-z0-9_]+)\]: FAILED! =>/', $line, $match)) { | |
$machine=$match[1]; | |
} | |
$k=strpos($line, '=>'); | |
$json=substr($line, $k+3); | |
$info=json_decode($json, true); | |
$k=strpos($line, '=>'); | |
$json=substr($line, $k+3); | |
$info=json_decode($json, true); | |
echo "! changed: [{$machine}] ".($info['changed']?'TRUE':'FALSE').PHP_EOL; | |
echo "! failed: ".($info['failed']?'TRUE':'FALSE').PHP_EOL; | |
echo "! start: {$info['start']}".PHP_EOL; | |
echo "! end: {$info['end']}".PHP_EOL; | |
echo "! delta: {$info['delta']}".PHP_EOL; | |
echo "$ {$info['cmd']} ".PHP_EOL; | |
foreach ($info['stdout_lines'] as $l) { | |
echo '! '.$l.PHP_EOL; | |
} | |
echo '! '.$info['stderr'].PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment