首页 > 解决方案 > 在 Ansible 中读取 Windows 主机上的文件

问题描述

与 PowerShell中的命令类似Get-Content,我正在寻找一种方法来读取 Windows 目标上的文件并将文件内容保存到 Ansible 中的变量中。文档说该ansible.builtin.file模块能够获取文件内容,但该功能似乎不适用于该win_file模块。

以下伪代码应该更好地解释我正在尝试做的事情:

- name: save file contents to variable
  win_get_content:
    path: C:\somefile.txt
  register: file_contents

标签: ansibleansible-2.x

解决方案


如果"{{lookup('file', 'C:\somefile.txt') }}"在窗口中不起作用,您可以尝试:

 - name: get content of file
   win_shell: 'type C:\somefile.txt'  
   register: file_contents

 - name: display content
   debug:  
     msg: "{{ file_contents.stdout_lines }} "

推荐阅读