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:
pip install 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[staging]superlists-staging.ottg.eu[local]localhost 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:allsudo:yestasks:-name:make sure required packages are installedapt:pkg=nginx,git,python3,python3-pip state=present-name:make sure virtualenv is installedcommand:pip3 install virtualenv-name:allow long hostnames in nginxlineinfile:dest=/etc/nginx/nginx.confregexp='(\s+)#? ?server_names_hash_bucket_size'backrefs=yesline='\1server_names_hash_bucket_size ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access