Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win_powershell: script: filename.ps1 #245

Closed
ghost opened this issue Jul 9, 2021 · 3 comments
Closed

win_powershell: script: filename.ps1 #245

ghost opened this issue Jul 9, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 9, 2021

SUMMARY

I would like to pass a file on the ansible control node to win_powershell's script: parameter.

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

win_powershell

ADDITIONAL INFORMATION

With the ansible.builtin.script module, we can provide a script file to execute on a target and specify the executable and additional arguments to use. Making a script Idempotent, and telling Ansible if changes occured or not, can be difficult though.

With ansible.windows.win_powershell, one of the new features you introduced was the $Ansible object that we can set the changed and failed statuses on, however, you cannot pass a file to the script parameter of win_powershell like you can with the builtin script module. I find it very difficult to read powershell in a yaml syntax/highlighted file in vscode, and having to transpose powershell code from a ps1 file into yaml can be very tedious if you need to troubleshoot.

Right now, win_powershell is pretty great for small code executions and easy enough to work with, but it would definitely be much nicer to work with if ps1 files could be passed to the module instead of raw yaml code (this would especially lend itself well to roles and collections)

- ansible.windows.win_powershell:
    script: foobar.ps1
    executable: pwsh.exe
    arguments:
    - -ExecutionPolicy
    - ByPass
 
@jborean93
Copy link
Collaborator

This can be done today by using the file lookup

- ansible.windows.win_powershell:
    script: '{{ lookup("file", "foobar.ps1") }}'
    executable: pwsh.exe
    arguments:
    - -ExecutionPolicy
    - ByPass

Except for win_powershell being able to run it in pwsh.exe I cannot think of any other scenarios where this might be beneficialcial over using the controller based file as a module. The latter has a few more benefits such as:

  • Simpler, no need to mess with runspaces and serializing output
  • Access to existing, and custom, module utils that can be shipped with Ansible
  • Better documentation standards and tools are available for modules
  • Can be callable by name outside the role/collection, win_powershell will still need be called with the proper options to replicate the same behaviour

@ghost
Copy link
Author

ghost commented Jul 9, 2021

Hmmm..... as long as relative pathing works within a role, that might work just fine. I haven't actually used the file lookup plugin with relative ansible paths like this before, so hadn't occurred to me to try that.

Thank you.

@jborean93
Copy link
Collaborator

jborean93 commented Jul 9, 2021

Relative pathing definitely works, it's the same as if you did something like win_copy: src=file.ps1 .... There are a few locations it tries to find the file. This would be another reason why having it as a module is better as trying to refer to a file inside a role but called outside will be tricky whereas a module is just by the name it is defined as.

Yaish25491 pushed a commit to Yaish25491/ansible.windows that referenced this issue Dec 4, 2024
jborean93 added a commit that referenced this issue Dec 8, 2024
* Initial commit

* Fix tests - ci_complete ci_coverage

* removing chocolatey as they have moved to chocolatey.chocolatey (#65)

* removing chocolatey as they have moved to chocolatey.chocolatey

* more chocolatey removals

* fix sanity error

* fixing one last time I hope

* Fix up docs after migration (#95)

* Fix up docs after migration

* Fix up sanity errors

* fix up sanity ignores (#97)

* fix up sanity ignores

* Bump ansible-windows dep

* Fix bad change for win_region

* Rebalance the test targets (#128)

* Rebalance the test targets

* Make sure IIS test removes the service so our httptester works

* Fix new santiy check and stabilise external downloads (#171)

* Mark win_hotfix as unstable (#245)

* Fix devel sanity checks - ci_complete (#331)

* Update win_hotfix.ps1 (#386)

* Update win_hotfix.ps1

* Fix up docs and return values

Co-authored-by: Jordan Borean <jborean93@gmail.com>

---------

Co-authored-by: Jordan Borean <jborean93@gmail.com>
Co-authored-by: Jake Jackson <jljacks93@gmail.com>
Co-authored-by: fsteve800 <91268425+fsteve800@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant