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

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

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?

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.

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]

I've run through this before and it worked. However, today I ran into a problem during the pip install ansible (while compiling pynacl).
...
config.status: creating test/Makefile
config.status: executing depfiles commands
config.status: executing libtool commands
error: [Errno 2] No such file or directory

----------------------------------------
Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-bUPjuA/pynacl/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-59yTyM-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-bUPjuA/pynacl/
...
Based on https://github.com/pyca/pynacl/issues/298, this relates to missing 'make'

I added 'make' and the pip install ansible completed successfully.

Thanks for documenting this!
Billy

This way of installing ansible with pip worked like a charm on my Windows 7 after of course installing all the above package dependencies in advance. Thanks for the great thread.

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.

Now that cygwin installs jinja2, I presume that would be the preferred installation method. I see it now installs something called python2-yaml: Python LibYAML bindings and python3-yaml: Python LibYAML bindings -- should we choose python2-yaml since we installed python 2.xx? Or python3-yaml? Or neither? Please and thank you in advance, I feel much better doing this after talking to the redhat people.

You should probably use python2-yaml for now, just because all the defaults in Ansible are still set for Python 2. (Though I don't think installing python3-yaml would break anything either).

Thanks Jeff for this awesome hack ... now I can have my ansible playbooks run from my Windows Desktop.

Instead of cloning git repo and checking for stable version, you can follow first 4 steps for ansible as well by doing curl -O http://releases.ansible.com/ansible/ansible-2.5.0.tar.gz and do untar and go with execute python setup.py install.......if you want to work on latest release...you can curl relative compressed tar.

On your cURL statements add an uppercase L, I tried the URLs today and they are both 301's but with -L curl will follow the redirect and finish the downloads properly for Jinja and PyYAML