Ansible Powershell Usage

I’m trying to use Powershell from Ansible.

This is the code:

  • name: Log into Azure

ansible.windows.win_powershell:

script: |

[CmdletBinding()]

param (

[String]

$TenantID,

[String]

$AccountID,

[SecureString]

$Secret

)

Connect-AzAccount -ServicePrincipal -TenantId $TenantID -Credential $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AccountID, $Secret)

parameters:

TenantID: “{{ tenant }}”

AccountID: “{{ sp }}”

Secret: “{{ secret }}”

The Connect-AzAccount command works, when executed from my machine (running OS X) under pwsh; however, it blows up pretty ugly when executed using ansible-playbook…

fatal: [localhost]: FAILED! => {

“changed”: false,

“module_stderr”: “\u001b[31;1mParserError: \u001b[0m/Users/j8683/.ansible/tmp/ansible-tmp-1678819559.972683-82400-269117048488035/AnsiballZ_win_powershell.ps1:159\u001b[0m\n\u001b[31;1m\u001b[0m\u001b[36;1mLine |\u001b[0m\n\u001b[31;1m\u001b[0m\u001b[36;1m\u001b[36;1m 159 | \u001b[0m \u001b[36;1m\u0000\u001b[0m\u0000\u0000\u0000{"module_entry": "IyFwb3dlcnNoZWxsCgojIENvcHlyaWdodDogKGMpIDIwMjE …\u001b[0m\n\u001b[31;1m\u001b[0m\u001b[36;1m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[0m\u001b[36;1m | \u001b[31;1m ~\u001b[0m\n\u001b[31;1m\u001b[0m\u001b[36;1m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[31;1m\u001b[31;1m\u001b[36;1m | \u001b[31;1munexpected token ‘\u0000’, expected ‘begin’, ‘process’, ‘end’, ‘clean’, or\u001b[0m\n\u001b[31;1m\u001b[0m\u001b[36;1m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[31;1m\u001b[31;1m\u001b[36;1m\u001b[31;1m\u001b[36;1m | \u001b[31;1m’dynamicparam’.\u001b[0m\n”,

“module_stdout”: “”,

“msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”,

“rc”: 1

}

Any ideas on what the win_powershell module might not like?

This is the ansible version I’m using:

ansible [core 2.14.0]

python version = 3.9.6 (default, Sep 26 2022, 11:37:49) [Clang 14.0.0 (clang-1400.0.29.202)] (/Library/Developer/CommandLineTools/usr/bin/python3)

jinja version = 3.1.2

Thanks,

Shawn

Also, I noticed this discussion: Ansible powershell module to be run on remote powershell on Linux machine. (https://groups.google.com/g/ansible-project/c/YZzYNEevzro)

Where Matt suggests:
That it [the module] is meant to be used with a Windows machine / over WinRM:

Matt Davis
unread,
Oct 21, 2016, 1:24:06 PM

to Ansible Project

Some aspects of Ansible’s Powershell support are currently built under the assumption that it would only ever run on Windows / over WinRM. There are a few things that would need to be moved around in order to allow “real” Ansible Powershell modules to work on Linux. By “real”, I mean so that the module generation stuff works correctly whether the WinRM connection plugin runs it or something else, and that you can use our Powershell module API.

So maybe that’s my issue… ie not meant to run against Azure…

If you are truly managing Windows resources look at win_dsc.

https://docs.ansible.com/ansible/latest/os_guide/windows_dsc.html

Walter

On a sidenote.
Actually I have used powershell on linux. I managed to get it working over ssh, and Refactored the helper functions for powershell task development.

https://www.powershellgallery.com/packages/PSAnsibleHelperFunctions4Linux/0.0.1

I also wrote a proof of concept collection to do ms sql managememt through the local execution environment.

https://github.com/weiyentan/community.dbatools

Though I really only see the use case for powershell as “controller type of acttivity” where the controller is reaching out. Ie. Managing a sql database. Or application. There is room to customise and with the advent of execution environments it allows for this. I have an execution environment that has pwsh installed so I can do all of this.

This was an older execution environment. A newer one exists.
https://github.com/weiyentan/awx-ee_containers/blob/master/execution_environments/powershell_standard_dbatools/execution-environment.yml

Managing linux servers, there is already tasks to do most things anyway to bother going through the effort of converting things to powershell imho. That’s another discussion.
If you are truly managing Windows resources look at win_dsc. – You received this message because you are subscribed to the Google Groups “Ansible Project” group. To unsubscribe from this group and stop receiving emails from it, send an email to . To view this discussion on the web visit .

Thanks for the feedback.

Wholeheartedly agree with your last statement :).
This was an ask and given that I see az cli as something that can do everything the PoSH modules can do, I’m not convinced it’s worth the effort.
That said, I’m trying to give it a fair shot. If it works, great, if not…

I came across a post (from reading some of the other conversations) where some is just using the shell module, specifying pwsh as the executable and putting the PoSH inline. I might give that a try; again, just to give this a shot.

If it doesn’t work for technical reasons, at least the feedback will be objective.

Shawn