2020-11-01 05:26:20 +00:00
|
|
|
[CmdletBinding()]param(
|
|
|
|
[string[]]$Command,
|
|
|
|
[string[]]$Remotes = 'all',
|
|
|
|
[string[]]$MoreArgs,
|
|
|
|
[switch]$NotPowerShell,
|
2021-05-05 21:57:09 +00:00
|
|
|
[switch]$RawOutput
|
2020-11-01 05:26:20 +00:00
|
|
|
)
|
2020-11-01 04:55:38 +00:00
|
|
|
|
|
|
|
$script:ansible_cli=@('ansible')
|
|
|
|
if( (Test-Path './ansible.cfg') -or (Test-Path '/opt/ansible/hosts') -and (Test-Path '/opt/ansible/ssh_key_for_ansible') ) {
|
|
|
|
$ansible_cli += '-i', '/opt/ansible/hosts', '--private-key=/opt/ansible/ssh_key_for_ansible'
|
|
|
|
}
|
|
|
|
if( $Command ) {
|
2020-11-04 15:58:04 +00:00
|
|
|
$ansible_cli += '-a'
|
|
|
|
$ansible_cli += 'ANSIBLE_PYTHON_INTERPRETER=auto_silent '
|
2020-11-01 04:55:38 +00:00
|
|
|
$ansible_cli += '-a'
|
|
|
|
if( $NotPowerShell ) {
|
|
|
|
$ansible_cli += $(($Command -join '; ').Replace("'","\`$(printf '\x27')").Replace('"',"\`$(printf '\x22')"))
|
|
|
|
} else {
|
|
|
|
$ansible_cli += "pwsh -encodedcommand $(ConvertTo-Base64 $($Command -join '; '))"
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 21:57:09 +00:00
|
|
|
if( -not $RawOutput ) {
|
2020-11-01 05:26:20 +00:00
|
|
|
$ansible_cli += '--one-line'
|
|
|
|
}
|
2020-11-01 04:55:38 +00:00
|
|
|
$ansible_cli += $Remotes
|
2020-11-01 05:26:20 +00:00
|
|
|
$ansible_cli += $MoreArgs
|
2020-11-01 04:55:38 +00:00
|
|
|
|
|
|
|
$local:expr = $('& "'+$($ansible_cli -join '" "')+'"')
|
|
|
|
Write-Verbose $expr
|
2020-11-01 05:26:20 +00:00
|
|
|
$local:SaveConsoleColor = [Console]::ForegroundColor
|
2020-11-04 15:58:04 +00:00
|
|
|
$env:ANSIBLE_DEPRECATION_WARNINGS='false'
|
2021-05-05 21:57:09 +00:00
|
|
|
$local:sw = [System.Diagnostics.Stopwatch]::StartNew()
|
2020-11-01 05:26:20 +00:00
|
|
|
Invoke-Expression $expr | ForEach-Object {
|
2021-05-05 21:57:09 +00:00
|
|
|
if( $RawOutput ) { return $_ };
|
2020-12-02 05:30:57 +00:00
|
|
|
$local:res = $_.Split('|') + @('', '', '', '')
|
2020-11-01 05:26:20 +00:00
|
|
|
$res = [PSCustomObject]([ordered]@{
|
|
|
|
Machine = $res[0].Trim();
|
2021-05-05 21:57:09 +00:00
|
|
|
Status = @($res[1].Trim(), "$($res[2])".Trim())
|
|
|
|
RunTime = $sw.Elapsed
|
2021-05-08 21:46:33 +00:00
|
|
|
Output = "$($res[3])".Trim() -split '\\n';
|
2020-11-01 05:26:20 +00:00
|
|
|
})
|
2021-05-10 03:45:23 +00:00
|
|
|
$res.PSObject.TypeNames.Insert(0,"AnsibleOutput")
|
2020-11-01 05:26:20 +00:00
|
|
|
if( $res.Status -match '^\w+!:' ) {
|
|
|
|
[Console]::ForegroundColor = [ConsoleColor]::Red
|
|
|
|
} else {
|
|
|
|
[Console]::ForegroundColor = [ConsoleColor]::Yellow
|
|
|
|
}
|
2021-05-10 03:45:23 +00:00
|
|
|
|
2020-11-01 05:26:20 +00:00
|
|
|
$res
|
|
|
|
}
|
2021-05-10 03:45:23 +00:00
|
|
|
|
2020-11-01 05:26:20 +00:00
|
|
|
[Console]::ForegroundColor = $SaveConsoleColor
|