running shell commands on Windows

194 views
Skip to first unread message

fanvalt

unread,
Sep 13, 2016, 3:53:43 AM9/13/16
to Ansible Project
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,

J Hawkesworth

unread,
Sep 14, 2016, 3:22:13 AM9/14/16
to Ansible Project
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

J Hawkesworth

unread,
Sep 15, 2016, 8:26:18 AM9/15/16
to Ansible Project
By the way, this is useful if you are looking for ways to convert linux 'toolbox' commands to powershell equivalents:


Jon
Reply all
Reply to author
Forward
0 new messages