If you’re interested in learning config management, or looking to try a new tool, Ansible is a great way to start managing servers. With any new tool comes the overhead of installing and configuring it. This article explains a simple way to setup Ansible with Python virtualenv.
The Ansible docs goes over a variety of ways to install Ansible ranging from apt/yum installs to compiling from source. In my own experience I’ve found that create a virtualenv is the easiest way to work with Ansible, especially when using more than one project.
apt install python-virtualenv
yum install python-virtualenv
If you’re having trouble installing, maybe try using pip to install virtualenv
pip install pip --upgrade
pip install virtualenv
Using virtualenv to install Ansible
Virtualenv allows you to import Python modules into a project folder, rather than installing them for every user on the system. Now that you have virtualenv installed, you can create an environment just for Ansible:
pip install ansible==2.6.4
That’s the basic setup, you now have access to Ansible! From here you should be able to run ansible –version
You can exit the environment by running:
Now start a new virtualenv with the latest version of Ansible:
pip install ansible=2.6.3
Verify which version of Ansible is available:
Upgrade to the latest version of Ansible:
pip install ansible --upgrade
Switching back to a different version of Ansible is simple, just source in a different virtualenv:
Create a Bash alias for Ansible and virtualenv
Finally, we’ll create an alias under your user account to source in a virtualenv with a more simple command. Edit your .bashrc as follows:
alias ansenv='source ~/ansible-latest/bin/activate'
Try it out! you’ll need to source in your .bashrc, or logout/login to pick up the changes:
Virtualenv gives you the convenience of pip without modifying python packages and modules system-wide. It’s like having your Ansible versions in separate containers! This is my preffered way to setup Ansible. We’ll look into other ways of managing Ansible in the future.
3 Replies to “Setup Ansible with Python Virtualenv”
Comments are closed.