Ansible Powershell Usage

145 views
Skip to first unread message

Shawn Singh

unread,
Mar 14, 2023, 3:28:04 PM3/14/23
to Ansible Project
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

Shawn Singh

unread,
Mar 14, 2023, 3:59:35 PM3/14/23
to Ansible Project
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...


Rowe, Walter P. (Fed)

unread,
Mar 15, 2023, 7:50:17 AM3/15/23
to ansible...@googlegroups.com
If you are truly managing Windows resources look at win_dsc.

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

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f3a8ab7d-ca56-44dd-a783-f54d5b816246n%40googlegroups.com.

Wei-Yen Tan

unread,
Mar 15, 2023, 11:17:36 AM3/15/23
to ansible...@googlegroups.com
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.  


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



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. 

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. 
Sent from Outlook for iOS

From: 'Rowe, Walter P. (Fed)' via Ansible Project <ansible...@googlegroups.com>
Sent: Thursday, March 16, 2023 12:50 AM
To: ansible...@googlegroups.com <ansible...@googlegroups.com>
Subject: Re: [ansible-project] Ansible Powershell Usage
 

Shawn Singh

unread,
Mar 15, 2023, 1:50:47 PM3/15/23
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages