0

I wanted to get the 3rd and 5th column output. I used the below command in my playbook.

win_shell: dir | awk '{print $3,$5}'

But I get output as below.

        "awk : The term 'awk' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the ",
        "spelling of the name, or if a path was included, verify that the path is correct and try again.",]

When I execute the command via the windows prompt I get the expected output. Please help.

Smily
  • 1,990
  • 2
  • 10
  • 29
  • the win_shell module uses powershell as default shell. Since cmd is working you can use win_command. Also it would be worth to check the same execution via power shell – error404 Apr 30 '19 at 13:53
  • when using win_command i still get error as "Failed to process input: The parameter 'awk' must begin with a / or - (HRESULT=80070057)." – Smily Apr 30 '19 at 14:33
  • https://stackoverflow.com/questions/21927944/how-to-run-an-awk-commands-in-windows – error404 Apr 30 '19 at 14:36

1 Answers1

0

You could perform you column extraction in Ansible rather than using awk. For example, assuming you've registered the output of your win_shell task in a variable called result, you could do this:

- debug:
    var: item.split()|json_query('[[2], [4]]')
  loop: "{{ output.stdout_lines }}"

This would display the 3rd and 5th column (arrays are zero-indexed) from each line of output. Maybe you want to put this in a list instead of just displaying it:

- set_fact:
    data: "{{ data|default([]) + [item.split()|json_query('[[2], [4]]')] }}"
  loop: "{{ output.stdout_lines }}"
larsks
  • 228,688
  • 37
  • 348
  • 338