Previously I’ve written about Running PowerShell Core Commands in a Linux Target from Ansible . In this article, we’ll look at a similar topic, but instead the PowerShell commands will be executed directly on the local Ansible host, not on a Linux based remote target.
In my lab I’m running AWX in containers.
The cross-platform version of PowerShell is not installed by default in this deployment method of AWX, so we need to get it installed there first. Connect into the awx_task container and observe that it is based on Centos 8:
Instructions for installing PowerShell on Centos can be found here.
Upon completion we have a version of 6.2.3 installed:
The AWX inventory that we will target is the Demo Inventory which contains the localhost:
Our playbook for testing:
- With the command module we specify pwsh, followed by the command we wish to run
- With the shell module the PowerShell command can be directly specified, however we need to supply the path to the shell we wish to use, i.e. /usr/bin/pwsh
Second note: the astute among you may have noticed the syntax to grab the User-Agent property from the returned JSON is not a simple dotted notation: apiOutput.headers[‘User-Agent’] . More info on how to get round a hypen in a name.
Either method can be used to achieve similar objectives, see below output of the above playbook (note the method documented here to return data from PowerShell to Ansible using JSON):