Running PowerShell 7 Commands Directly on Ansible Localhost

In a previous post I have written about Running PowerShell Core Commands Directly on Ansible Localhost using the version of PowerShell Core available at that time 6.2.3. With the recent release of PowerShell 7 I will demonstrate how to achieve the same outcome with this newer version and also an example taking advantage of one of the new features.

My awx_task container is based on Centos 8 and installation for PowerShell 7 is the same process as for 6.2.3. I already had the Microsoft RedHat repository registered from installing 6.2.3, so it was simply:

sudo yum install -y powershell

We can see that PowerShell 7 has been installed:

To illustrate functionality we’ll use the following Ansible playbook:

In the first half of the playbook we’ll execute the below PowerShell script stored locally on the Ansible host.

On line 1 we purposefully run a command that will generate an error. We then use a new cmdlet in PowerShell 7, Get-Error, to return some specific error details.

When this section of the playbook is executed in AWX we observe the selected info returned from Get-Error that we wish to debug via Ansible.

In the second half of the playbook we use Invoke-RestMethod to make an API call against a test URL.

Observe the debug log from the Invoke-RestMethod call confirms that we are using PowerShell 7:


This post is part of the #PS7Now series of blog posts to accompany the release of PowerShell 7. You can find posts from other contributors at the following sites: