Minor Tweak for Ansible on Windows

I’m trying to get Ansible up and running on a windows environment, which I know is not technically supported. However, using the following guide, http://dhorbach.blogspot.com/2013/08/working-with-ansible-in-eclipse-and.html, which took about 15 - 20 minutes, I was able to start running playbooks from my windows machine.

But, I also need to manage windows environments as well, not just run playbooks from a windows machine. After getting SSHD up and running on a target system, I tried executing the following command:

$ ansible -c ssh WindowsTestServer -a “/usr/bin/whoami.exe”

But, it threw an error:

msaltzmanw7.bbbb.net | FAILED >> {
“failed”: true,
“msg”: "/usr/bin/python: can’t open file ‘/home/msaltzman/.ansible/tmp/ansible-tmp-1392324304.91-246447835237283/command’,
“parsed”: false
}

I tracked it down to the fact that the tmp directories that the modules, playbooks, etc. use on the remote machine weren’t getting created prior to trying to move the required modules and code into said temp directory. I’m not sure if this is the best way to fix it, but I altered ansible/runner/connection_plugins/ssh.py to create the directory first using sftp:

def put_file(self, in_path, out_path, out_dir):
‘’’ transfer a file from local to remote ‘’’
vvv(“PUT %s TO %s” % (in_path, out_path), host=self.host)
if not os.path.exists(in_path):
raise errors.AnsibleFileNotFound(“file or module does not exist: %s” % in_path)
cmd = self._password_cmd()

host = self.host
if self.ipv6:
host = ‘[%s]’ % host

if C.DEFAULT_SCP_IF_SSH:
cmd += [“scp”] + self.common_args
cmd += [in_path,host + “:” + pipes.quote(out_path)]
indata = None
else:
cmd += [“sftp”] + self.common_args + [host]
mkdir = “mkdir %s” % ( out_dir )
indata = “put %s %s\n” % (pipes.quote(in_path), pipes.quote(out_path))

p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self._send_password()
stdout, stderr = p.communicate( mkdir )
p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self._send_password()
stdout, stderr = p.communicate(indata)

if p.returncode != 0:
raise errors.AnsibleError(“failed to transfer file to %s:\n%s\n%s” % (out_path, stdout, stderr))

I wanted to check if this was a good way to solve the problem before I submitted a pull request for it. What do you think?

Ok so let me state the ideas here as these threads tend to get linked to :slight_smile:

Basically, we don’t want to take any pull requests regarding getting Ansible to run on a Windows server as this is something we don’t want to maintain long term or be committed to keep working – more is actually required in some cases, and in many cases it makes things more complex.

For instance, supporting Windows would mean we couldn’t rely on certain modules that didn’t work on Windows, or don’t exist for Windows, which ties our hands with what we can do there.

We are hoping to include Windows node management (from Linux) as a future feature, but I’m not able to give dates (I suggested some in the past but I don’t want to name dates and be off). This will likely be done using a different “connection” type in Ansible and using powershell/DSC.

Using a virtual machine or a Mac are both good options!

As for the issue you raise, “I tracked it down to the fact that the tmp directories that the modules, playbooks, etc. use on the remote machine weren’t getting created prior to trying to move the required modules and code into said temp directory”

I am a little curious why a mkdir wouldn’t work but did want to point you to the “sftp_if_ssh” settings in ansible.cfg – in this case you can choose to just use SFP the first time. (i.e. it might be you have a Linux problem and this isn’t related to it being run from Windows?)

Thanks Michael. I don’t think I was clear enough in my first post. When I ran into this issue, I was running from a Windows environment (which with the configurations I mentioned work without issue against Linux machines) against a remote Windows host. I am also proposing the following changes:

  • Add the out_dir parameter to put_file
  • Add a mkdir command to put_file

Currently, put_file only has 2 parameters, and no mkdir command. Sorry if that wasn’t clear.

Right, we’ll cross this bridge when we come to it.

Our future Windows code, when it happens, will likely involve a new connection type.

Thanks for the information Michael. When you get to that point, let me know if I can be of assistance.