Appendix C. Provisioning with Ansible
We used Fabric to automate deploying new versions of the source code to our servers. But provisioning a fresh server, and updating the Nginx and Gunicorn config files, was all left as a manual process.
This is the kind of job that’s increasingly given to tools called “Configuration Management” or “Continuous Deployment” tools. Chef and Puppet were the first popular ones, and in the Python world there’s Salt and Ansible.
Of all of these, Ansible is the easiest to get started with. We can get it working with just two files:
pip2 install --user ansible # Python 2 sadly
An “inventory file” at deploy_tools/inventory.ansible defines what servers we can run against:
deploy_tools/inventory.ansible
[live]
superlists.ottg.eu ansible_become
=
yes ansible_ssh_user=elspeth
[staging]
superlists-staging.ottg.eu ansible_become
=
yes ansible_ssh_user=elspeth
[local]
localhost ansible_ssh_user
=
root ansible_ssh_port=6666 ansible_host=127.0.0.1
(The local entry is just an example, in my case a Virtualbox VM, with port forwarding for ports 22 and 80 set up.)
Installing System Packages and Nginx
Next the Ansible “playbook”, which defines what to do on the server. This uses a syntax called YAML:
deploy_tools/provision.ansible.yaml
---
-
hosts
:
all
vars
:
host
:
"{{
inventory_hostname
}}"
tasks
:
-
name
:
Deadsnakes PPA to get Python 3.6
apt_repository
:
repo='ppa:deadsnakes/ppa'
-
name
:
make sure required packages are installed
apt
:
pkg=nginx,git,python3.6,python3.6-venv ...
Get Test-Driven Development with Python, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.