2

I would like to enrich my Ansible logs with info from the Verbose stream. Using Ansible 2.9.25 with Powershell 5.1

So I am trying to tie verbose output to standard out:

- win_shell: Expand-Archive jinja2_files.zip -DestinationPath . -Force -Verbose 4>&1
  args:
    chdir: e:\temp\Unzip

Expand-Archive is one of the Powershell cmdlet that supports -Verbose but the behavior is exactly the same with other cmdlets.

No matter what I do my stdout is blank in the task output log:

"stdout": "",

I could redirect to a temp file, read it and discard it but hopefully there is a better way.

Confirmed Ansible is able to write to both sdout and sderr.

  • Try wrapping the preceding statement in a block: `&{Expand-Archive jinja2_files.zip -DestinationPath . -Force -Verbose} 4>&1` – Mathias R. Jessen Feb 08 '22 at 13:55
  • Tried both '&' and Invoke-Command - using a block in both cases with no luck – Alex Korobchevsky Feb 08 '22 at 14:19
  • 1
    Just to rule out other factors: does the command actually succeed? (if Ansible captures stderr separately, you wouldn't see error messages in the stdout output). Also, note that the PowerShell CLI (`powershell.exe`) by default sends _all_ output streams to stdout, so `4>&1` shouldn't even be necessary. – mklement0 Feb 08 '22 at 14:41
  • @mklement0 - yes the command succeeds - both directly called and out of Ansible – Alex Korobchevsky Feb 08 '22 at 14:54
  • 2
    I reversed my close vote. I realized you are already redirecting correctly but still not getting output. Does this happen if you use [ansible.windows.win_powershell](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_powershell_module.html) instead? – codewario Feb 08 '22 at 15:45
  • Good point @BendertheGreatest - unfortunately I don't have win_powershell in my corporate environment. – Alex Korobchevsky Feb 08 '22 at 17:55
  • Is _any_ stdout captured? If you prepand, say, `'hi'; ` to your command, is `hi` captured? – mklement0 Feb 08 '22 at 19:38
  • 'hi' and the following command both go to stderr @mklement0. Nothing goes to stdout – Alex Korobchevsky Feb 08 '22 at 20:29
  • Did you actively award the bounty because the answer solved your problem? If so, I suggest also accepting the answer. However, did the use of `$(...)` really made a difference (normally, it should not)? – mklement0 Feb 17 '22 at 15:10

1 Answers1

1

You can use expressions to capture Verbose and Standard output into variables.

Save both Output and Verbose into single variable

$VerboseAndOutput = $(Expand-Archive -Path "C:\2019_OfficeContent_Symbols.zip" -Verbose) 4>&1

Save Output and Verbose into separate variables

$VerboseOnly = $($OutputOnly= .{
    Expand-Archive -Path "C:\2019_OfficeContent_Symbols.zip" -Verbose
}) 4>&1

So, something like the following should work for you:

- win_shell: $(Expand-Archive jinja2_files.zip -DestinationPath . -Force -Verbose) 4>&1
  args:
    chdir: e:\temp\Unzip

If you also want to capture warnings:

- win_shell: $(Expand-Archive jinja2_files.zip -DestinationPath . -Force -Verbose) 4>&13>&1
  args:
    chdir: e:\temp\Unzip
MikeSh
  • 352
  • 1
  • 5
  • You don't need a subexpression (`$(...)`) in order to apply redirections to a command or capture its success / stdout output; try `$captured = Write-Verbose -Verbose hi 4>&1`, for instance. – mklement0 Feb 16 '22 at 14:38
  • To put it differently: there's nothing wrong with the PowerShell code in the question. The problem must lie in how Ansible invokes it and captures its output. – mklement0 Feb 16 '22 at 14:44