Running PowerShell Core Commands in a Linux Target from Ansible
Ansible has modules shell, for executing commands against Linux targets, and win_shell for executing commands against Windows targets - typically using PowerShell. So I was curious to see if it was possible to execute commands in a Linux target using PowerShell Core and if so, how to do it.
For my test I have an Ubuntu 18.04 VM with PowerShell Core 6.2.2 installed:
The shell module has an executable parameter described as “Change the shell used to execute the command. This expects an absolute path to the executable. “ . So I figured I would start there and put together the following basic playbook:
Notice how the executable parameter has been set to the path for PowerShell Core, /usr/bin/pwsh . Running a simple command Get-Process and using ConvertTo-Json to make the returned data easier to consume in Ansible.
That basic test was successful, so I then updated the playbook to process the returned data:
- Use the from_json filter to convert the returned JSON text to an object
- Search through that object to find the process with the name cron
- Debug out some of the properties of the returned cron object
The resulting playbook run gives us the cron object:
and the log message we were hoping for: