Installing Ansible 2.x in a Python Virtualenv on OS X30 April 2016 · Filed in Tutorial
In this post, I’m going to walk you through the steps to install Ansible 2.x into a Python virtual environment (virtualenv) on OS X. There’s nothing terribly hard or unusual about this process, but I wanted to document it here for folks who might be new to the process (or who might be interested in why using this approach could be beneficial).
I’m stumbled into this process because I had been using Ansible 1.9.x and wanted to upgrade to Ansible 2.x so that I could use some of the new OpenStack-related modules. (These are modules that allow you to manipulate OpenStack-based resources, like instances or networks, using Ansible playbooks, and they were introduced with the release of Ansible 2.x.) The new modules had some additional Python dependencies, and installing these Python dependencies on OS X can be challenging at times (due to System Integrity Protection [SIP]). For example, installing the
shade module on my OS X El Capitan system ran afoul of SIP.
The answer is to use Python virtual environments (aka “virtualenvs”). Virtualenvs provide a mechanism whereby you can isolate Python dependencies between different Python-based projects. You create a Python virtualenv, then you install the Python module into that virtualenv. All other environments—including other virtualenvs and the system environment—are left untouched. Handy, right? Ansible, as a Python-based project, can be run inside a virtualenv so that you can isolate Ansible’s dependencies away from the rest of the system.
Here’s what the process looks like:
First, you’ll need to install support for Python virtualenvs. Although some folks have said not to use
sudowhen installing via Pip, I found it necessary:
sudo -H pip install virtualenv
Note that you might need to install Pip first, which—fortunately—is pretty straightforward (
Once you have virtualenv support installed, then create a virtualenv into which you’ll install the Ansible dependencies:
virtualenv --system-site-packages ansible
This will create a virtualenv called “ansible” in a directory named “ansible” in the current working directory. I used the
--system-site-packagesflag to allow this virtualenv to have access to the system site packages. I saw various recommendations both for and against this approach; my line of thinking was that this would help prevent some duplication of modules.
Activate the virtualenv by running
source ansible/bin/activate(if you used a name other than “ansible”, substitute that in the command). This will modify your prompt so that the name of the virtualenv is shown in parentheses before the prompt.
Now install Ansible using
pip install ansible, along with any other necessary dependencies. In my case, I wanted to use the OpenStack modules, so I also ran
pip install shade.
When you’re done with the virtualenv, run
deactivate. You’ll see your prompt revert to normal.
Now, anytime you want to run Ansible, activate the “ansible” virtualenv (see step #3). This will put you into the appropriate virtualenv with all the necessary modules and dependencies. When you’re done, simply deactivate the virtualenv (see step #5) and go on about your day. Easy as cake!Tags: Ansible · CLI · Macintosh · Python Previous Post: Prayer Time at the Austin Summit Next Post: Building a VMware-Formatted Cumulus VX Vagrant Box