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.
- Ansible, Windows and PowerShell: the Basics – Introduction
- Ansible, Windows and PowerShell: the Basics - Part 1, Windows Services
- Ansible, Windows and PowerShell: the Basics - Part 2, Install PowerShell Modules
- Ansible, Windows and PowerShell: the Basics – Part 3, Windows Roles and Features
- Ansible, Windows and PowerShell: the Basics – Part 4, Invoking PowerShell Code
- Ansible, Windows and PowerShell: the Basics – Part 5, Example PowerShell Error Handling
- Ansible, Windows and PowerShell: the Basics – Part 6, Displaying Output from PowerShell Code
- Ansible, Windows and PowerShell: the Basics – Part 7, Utilising PowerShell DSC
- Ansible, Windows and PowerShell: the Basics – Part 8, Rebooting & Waiting
- Ansible, Windows and PowerShell: the Basics – Part 9, Disk Creation
- Ansible, Windows and PowerShell: the Basics – Part 10, Local Users
- Ansible, Windows and PowerShell: the Basics – Part 11, Local Groups
- Ansible, Windows and PowerShell: the Basics – Part 12, Set TimeZone
- Ansible, Windows and PowerShell: the Basics – Part 13, Environment Variables
- Ansible, Windows and PowerShell: the Basics – Part 14, Registry Entries
- Ansible, Windows and PowerShell: the Basics – Part 15, Install Chocolatey Packages