Python virtualenv management with Ansible
Using Ansible to replicate the virtual environment of a Django web application.
In a previous post we described how we can use Ansible to manage cloud services, for example, to replicate a server instance living in AWS and use it for development - testing purposes.
Of cource this was an example that can present you some options but heavy using of cloud provider proprietary services can lead you to expensive and limited solutions.
Ideally we want to use only portable environments that can move from server to server and from a cloud provider to another easily with minimal infrastructure or operating system configuration.
Following this mentality along with the persistence to do every configuration with a configuration management tool like Ansible can help our application to be more portable, scalable, easy to replicate for testing, development etc.
This post is about a real working python virtualenv containing a Django application with all its dependencies. The consept is the creation of an isolated test environment running on the same linux server. In addition to the application we need to replicate the database because our testing plan may be include schema changes or other data migration.
So we need to automate this scenario:
- Connect to the production server.
- Compress the application in a package.
- Backup the application package in our local server.
- Extract the package to the remote server in the test workspace.
- Create a new python virtualenv.
- Install the python packages that our application depends on, in our new virtual env using a "requirements" file.
- Configure the application for test by changing debug status and database name in settings.py.
- Create a new "staging" database in the same instance as a production replica (you may need to become the instance owner to do this).
- Check that everything is ok via "manage.py check" command.
The scenario is reflected to the following ansible playbook:
Of cource, our source code may be managed by a git repository. So instead of copying files, we can hit our repository server every time we want to get a version of our code.
This is an example Ansible playbook that implements version HEAD checkout from a remote repository -via SSH- to a local repository:
A working example like that can give you an idea about the processes we can automate and run easily again and again or run against multiple servers with no effort using Ansible.
- Posted by Kostas Koutsogiannopoulos · April 26, 2016