簡體   English   中英

使用Ansible從Windows命令提示符輸出中提取特定列

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

我想獲得第3和第5列輸出。 我在我的劇本中使用了以下命令。

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

但我輸出如下。

        "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.",]

當我通過Windows提示符執行命令時,我得到了預期的輸出。 請幫忙。

你可以在Ansible中執行列提取而不是使用awk 例如,假設您已在名為result的變量中注冊了win_shell任務的輸出,則可以執行以下操作:

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

這將顯示每行輸出的第3和第5列(數組為零索引)。 也許你想把它放在一個列表而不是只顯示它:

- 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