running shell commands on Windows

Hello,

I did write an Ansible playbook to run on Linux, and now I would like to port it to run on Windows.

I would like to list the subdirectories to save it but because I don’t know the full name, on Linux did write this play:

  • name: Lister l installation existante
    shell: chdir=/{{ Directoryname }} ls -d SHR_4YOU-{{product}}-*
    register: shr4you_inst
    ignore_errors: True
    when: repertoire.stat.isdir is defined and repertoire.stat.isdir

How can I port this line on Windows ? shell: chdir=/{{ Directoryname }} ls -d SHR_4YOU-{{product}}-*

Thanks for your help.
Regards,

Hi,

Windows doesn’t have the same kind of shells as linux, so right now, your best choise is to write powershell and execute it from ansible using the raw or script modules. Powershell is a little different from bash or ksh. The main big difference is that you pipe objects not text.

Here’s a intro for someone coming from a linux background https://developer.rackspace.com/blog/powershell-101-from-a-linux-guy/

I like to use ad-hoc ansible to work out commands.

ansible win10 -m raw -a “gci c:\”

There are lot of aliases for command names that roughly approximate to linux equivalents, but because you are dealing with objects, the (default) output formats are very different from what you are used to. There’s a bunch of built in formatting commands which can help and you can pick out just the attributes from objects that you are interested in.

ansible 10t -m raw -a “gci c:\|format-list”

ansible win10 -m raw -a “gci c:\|convertto-csv”

ansible 10t -m raw -a “gci c:\|select Name, CreationTime |convertto-json”

Hope this gives you an idea of what is possible.

Jon

By the way, this is useful if you are looking for ways to convert linux ‘toolbox’ commands to powershell equivalents:

https://www.gitbook.com/book/devopscollective/a-unix-person-s-guide-to-powershell/details

Jon