Running Ansible within Windows

2016 Update: If you are using Windows 10 or later, check out my newer instructions for Using Ansible through Windows 10's Subsystem for Linux.

Ansible is a simple and powerful infrastructure and configuration management tool that Server Check.in uses to manage it's infrastructure. Installing and using Ansible on Mac OS X or Linux workstations is incredibly easy, and takes all of 30 seconds to set up.

Running Ansible via Cygwin on Windows 7

Running Ansible commands from within Windows is unsupported at the time of this writing. According to the CTO of AnsibleWorks, only windows servers as endpoints (not as hosts for controlling other servers via Ansible) are on the roadmap for future support... That said, if the idea of running a Linux VM on your Windows workstation (with something like the free VirtualBox app) just to run Ansible is unsettling, you can still use Ansible within Windows, if you run it within Cygwin.

As an aside, this blog post was written while researching for a book on Ansible: Ansible for DevOps. You can purchase Ansible for DevOps on LeanPub, Amazon, or iTunes.

Here are steps to getting Ansible (and it's related commands, like ansible-playbook) running on Windows:

  1. Download and install Cygwin, with at least the following packages selected (you can select the packages during the install process):

    • curl
    • python (2.7.x)
    • python-jinja
    • python-crypto
    • python-openssl
    • python-setuptools
    • git (1.7.x)
    • vim
    • openssh
    • openssl
    • openssl-devel
  2. If you are working behind a proxy (as is the case in many corporate networks), edit the .bash_profile used by Cygwin either using vim (open Cygwin and enter vim .bash_profile), or with whatever editor you'd like, and add in lines like the following:

    export http_proxy=http://username:password@proxy-address-here:80/
    export https_proxy=https://username:password@proxy-address-here:80/
  3. Download and install separately PyYAML and Jinja2 separately, as they're not available via Cygwin's installer:

    1. Open Cygwin
    2. Download PyYAML:
      curl -O https://pypi.python.org/packages/source/P/PyYAML/PyYAML-3.10.tar.gz
    3. Download Jinja2:
      curl -O https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.6.tar.gz
    4. Untar both downloads:
      tar -xvf PyYAML-3.10.tar.gz && tar -xvf Jinja2-2.6.tar.gz
    5. Change directory into each of the expanded folders and run python setup.py install to install each package.
    6. Generate an SSH key for use later: ssh-keygen, then hit enter to skip adding a password until you get back to the command prompt.
    7. Clone ansible from its repository on GitHub:
      git clone https://github.com/ansible/ansible /opt/ansible
    8. If you'd like to work from a particular Ansible version (like 2.0.1, current as of this writing), change directory into /opt/ansible and checkout the correct tag: git checkout v2.0.1 (some users have also reported success with the tag `v2_final`).
    9. Add the following lines into your Cygwin .bash_profile (like you did the proxy settings—if you're behind one—in step 2):

      # Ansible settings
      ANSIBLE=/opt/ansible
      export PATH=$PATH:$ANSIBLE/bin
      export PYTHONPATH=$ANSIBLE/lib
      export ANSIBLE_LIBRARY=$ANSIBLE/library

    10. At this point, you should be able to run ansible commands via Cygwin (once you restart, or enter source ~/.bash_profile to pick up the settings you just added). Try ansible --version to display Ansible's version.

If you would like to use Ansible as a provisioner for Vagrant, you can try, but after a day's worth of frustration (to the point of trying to add custom .bat files, changing Windows' and Linux' %PATH%/$PATH multiple times, and doing a hundred other things besides), I would recommend using Linux or a Mac to use Vagrant + Ansible together, or you could also use something like my JJG-Ansible-Windows shell provisioning script to run Ansible from within the VM itself.

Note: It may also be possible to run Ansible more easily using a bootstrap like Ansible Babun Bootstrap, but the experience of using Ansible on Windows will still be sub-par (but at least easier!).

I have written much more on how to use Ansible, whether you use Windows, Linux, or Mac OS X, in much greater detail, in Ansible for DevOps!

Comments

Thanks a lot !
Works well for me on my Win 10 (ansible 1.9.1)

Great article, only problem I had was having to run: python setup.py install within the checked out ansible code as the .bash_profile stuff didnt work for me.

Thanks

I also had that issue but worked fine after running the install command in the ansible dir.

Awesome article, helped a lot. Couple of notes from my install today.

Jinja is available on Cygwin Now.

Ran into a few issues with the default branch for ansible
recommend : git checkout v2_final

Thanks for the heads-up. v2_final works great for me.

Thanks! I've updated the post with these notes, and will do some testing on my Windows 10 laptop as well. This is great news!

this does't work for me. i use password to login the root server. It always said:

FAILED! => {'msg': u'ERROR! SSH encountered an unknown error. The output was:\n', 'failed': True}

Do you know how to fix it?

Hi,

I am getting this error:
ERROR! Unexpected Exception: 'module' object has no attribute '_vendor'

any idea?

I too get this error.
I found this about it via Google but python-setuptools was already installed and reinstalling did not help.
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg...

Finally got a working version...
git ls-remote https://github.com/ansible/ansible | grep stable
git checkout stable-2.1

Why not simply `pip install ansible`?

Does that work? Honestly, the last time I tried this out was about 1 year ago, and I couldn't get pip to work in any Windows environment.

Yes it works and it needs updated C++ lib.

I tried to installed in windows. but, could not able to even get the start point.
Can some give me the walkthough steps to install ansible on windows ?
I am brand new to ansible.
Kindly share your email id, so that I can send email if I have additional questions

I tried Jeff's method, but the only version I could get to work through that was v1.9.6-1 (using "git checkout v1.9.6-1" -- the git checkout instructions in the article are a bit out of date). Any newer release tags were completely broken for me using this method (it would yield a python error to run any Ansible commands). I'd been using Jeff's method for a while with an older install of cygwin, but I just upgraded to Windows 10 and needed to do a reinstall.

The short version:
-- Install Cygwin (or Babun)
-- Install the dependencies listed in Jeff's guide (manually during the cygwin install process; after installing apt-cyg [https://github.com/transcode-open/apt-cyg]; or through Babun's pact package manager, etc)
-- Install pip: python /usr/lib/python2.7/site-packages/easy_install.py pip
-- Install ansible: pip install ansible

Using pip will get you set up with a full version of 2.1.0.0. You won't need the extra ~/.bash_rc lines to get it to work out of the box, but you will likely have to do some standard configuring afterwards depending on your Ansible setup.

I've noticed if you don't have admin access on your system, you'll have to run this process on cygwin terminal open as "Run as administrator". It will work just fine on non-admin terminals afterwards, though.

This worked for me with the following addendums:
- installed gcc to allow pip to build a few other dependencies during 'pip install ansible'
- additional cygwin dep: libffi-devel

Great thread, guys, thanks for sharing!

I've "managed" to install ansible using pip as suggested above, but when running `ansible` I get "The 'pycrypto>=2.6' distribution was not found and is required by ansible". However, `pip list` shows that pycrypto is, indeed, installed in version 2.6.1.

I'd appreciate some help with this but will keep digging in the meanwhile.

I got a solution for that, tested multiple times. Here you go.

  1. Install cygwin
  2. Install the following:
    • curl
    • python
    • python-setuptools
    • python-crypto
    • openssl
    • openssl-devel
    • libffi-devel
    • gcc-g++
    • vim
    • git
    • wget
  3. Install pip using easy_install-2.7 pip
  4. Install Ansible using pip install ansible

[Edit: added by Jeff G after spam filter removed it—see: How to Install Ansible on Windows]

Thanks for the steps. Worked very well on windows 7

I got the following error on my Windows 7:

$ ansible --version
Traceback (most recent call last):
  File "/opt/ansible/bin/ansible", line 45, in <module>
    from ansible.utils.display import Display
  File "C:\opt\ansible\lib\ansible\utils\display.py", line 21, in <module>
    import fcntl
ImportError: No module named fcntl

Thank you for this great post.
I had to do few tweaks around it to work on my specific environment.(Cygwin on Windows 7 unfortunatelyt)
Ansible check out from GIT is currently pointing to the tag - v2.1.3.0-1. So, when I tried to do a "ansible --version", it was failing with a "ImportError: No module named runner". So, I check out from a lower version tag (v1.9.2-0.2.rc2). After that "ansible --version" was throwing some git sub-modules related error ($ ansible --version
ansible 1.9.2 (detached HEAD 9bf06bfe84) last updated 2016/11/18 15:58:20 (GMT +650)
lib/ansible/modules/core: not found - use git submodule update --init lib/ansible/modules/core
lib/ansible/modules/extras: not found - use git submodule update --init lib/ansible/modules/extras
v2/ansible/modules/core: not found - use git submodule update --init v2/ansible/modules/core
v2/ansible/modules/extras: not found - use git submodule update --init v2/ansible/modules/extras
configured module search path = /opt/ansible/library). After downloading those sub modules, all errors from "ansible --version" are gone.

I install almost everything via pip in Cygwin environment. Things get interesting when you want to pair ansible with vagrant on windows. I wrote a little go wrapper that can be used to tie this all together nicely. https://github.com/bertramn/ansible-win-wrapper