Ansible fails in windows servers when using vmware_tools connection and a username not logged in before

226 Views Asked by At

I'm trying to create a directory in a Windows machine and after that copy a file inside the directory.

The modules used are win_file and win_copy. This is the code:

- name: "win_winrm | main.yml | Create temporary dir 'tmp_ansible' on C:Sistemas unit"
  win_file:
    path: C:\Sistemas\tmp_ansible
    state: directory

- name: "win_winrm | main.yml | Copy ConfigureRemotingForAnsible.ps1 script into Windows machine"
  win_copy:
    src: files/ConfigureRemotingForAnsible.ps1
    dest: C:\Sistemas\tmp_ansible\ConfigureRemotingForAnsible.ps1

The win_file modules fails with a strange error that doesn't show any stdout.

This is the output:

TASK [win_winrm : win_winrm | main.yml | Create temporary dir 'tmp_ansible' on C:Sistemas unit] ***
[WARNING]: Error deleting remote temporary files (rc: 1, stderr: #< CLIXML
<Objs Version="1.1.0.1"
xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress"
RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>Sy
stem.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing
modules for first use.</AV><AI>0</AI><Nil
/><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><S
S="Error">Remove-Item : Cannot find path
'C:\Users\TEMP\AppData\Local\Temp\ansible-
tmp-1674134258.9689112-102-136885899008081' _x000D__x000A_</S><S
S="Error">because it does not exist._x000D__x000A_</S><S S="Error">At line:2
char:1_x000D__x000A_</S><S S="Error">+ Remove-Item
'C:\Users\TEMP\AppData\Local\Temp\ansible-tmp-1674134258.
..._x000D__x000A_</S><S S="Error">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~…
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: }
fatal: [wsy01iedi.rmasede.grma.net]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

Credentials used are tested manually and working and the user has admin permissions.

Just to discard it is not a problem related to the win_file module when trying to create the directory, I have tried to directly copy the file to an existing directory with the module win_copy. Same error.

In fact, the automation works in some servers, but in most of them, it doesn't.

As this is an automation for configuring WinRM in virtual machines hosted in VMWare vCenter, the ansible_connection method used is vmware_tools.

EDITED:

I found the issue. When ansible connects to a windows server using the vmware_tools method, the user profile is not created in the server, so ansible fails because it can not find the path of the user profile (C:\Users\USER).

If I try the same automation but using the WinRM connection method (obv in a server where WinRM is already configured), it works. When I log in to the server manually, I can see that the user profile path has been just created.

1

There are 1 best solutions below

0
On

My problem is that vmware_tools.py's function _fetch_file_from_vm() uses vmware_host as url address. This is fqdn address, that my ansible machine cannot resolve.