DebugFactor.com | Forum

How to read or view the contents of a file in windows server using Ansible

I would like to read or copy the contents of a file in windows server to a variable for further tasks.
I tried as below, but it runs for unlimited time and not giving any output to the screen.

Could someone tell me if there is any method for this?

- name: Read from file
  win_command: notepad C:\temp\filename.xml
  register: file_contents
- name: Tasks
  debug:
    msg: "success"
  when: '"success" in file_contents.stdout'

The command which you have provided (notepad C:\temp\filename.xml) opens a file in a notepad. That is the reason why it hangs.

To read the contents of a file in Windows you can execute the command cat C:\temp\filename.xml in Powershell.
To achieve the same with Ansible, use the win_shell module as below.

- name: Read from file
  win_shell: cat C:\temp\filename.xml
  register: file_contents
- name: Tasks
  debug:
    msg: "success"
  when: '"success" in file_contents.stdout'

Hope this helps.