Ansible, Windows and PowerShell: the Basics – Part 6, Displaying Output from PowerShell Code

In Part 6 of this series we’ll continue our journey with Ansible, Windows and PowerShell and look at how we can display output from code in PowerShell scripts back in Ansible to help track the progress of a task.

The following example will demonstrate how to send log info from a PowerShell script back to Ansible.

Our job template in AWX is _5_log-output

The contents of _5_log-output.yml are are follows:

The first task copies the PowerShell script demonstrating how to output log text to the target and the second task executes that script, and most importantly registers the result in the variable named result. The third task debugs the stdout_lines property of the result which contains what we’ll be looking for. This Ansible documentation page details what is available to you when returning data and registering into a variable.

The contents of test-output.ps1 are as follows:

Running a job from the _5_log-output job template produces the following result:

The script was successfully copied and executed. The information from the Write-Host commands was not displayed by default in the Run a script task, however by adding a debug task we are able to grab the log info from the stdout_lines property.

For a more advanced look at returning data from PowerShell scripts, take a look at this post I made outside of this series.