简体   繁体   English

使用Ansible从Windows命令提示符输出中提取特定列

[英]Extract a specific column from the windows command prompt output using Ansible

I wanted to get the 3rd and 5th column output. 我想获得第3和第5列输出。 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. 当我通过Windows提示符执行命令时,我得到了预期的输出。 Please help. 请帮忙。

You could perform you column extraction in Ansible rather than using awk . 你可以在Ansible中执行列提取而不是使用awk For example, assuming you've registered the output of your win_shell task in a variable called result , you could do this: 例如,假设您已在名为result的变量中注册了win_shell任务的输出,则可以执行以下操作:

- 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. 这将显示每行输出的第3和第5列(数组为零索引)。 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 }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM