Ansible is a scripting language mostly used to automate installing applications, deploying apps etc..
You may think why to learn a new language if I can do the same using shell scripts, well that's ok but in my opinion ansible doesn't take much time to learn and shell scripts could become complex as the projects scale up. You can read further details over here(https://news.ycombinator.com/item?id=6431552).
Before we start it's important that you have an overview of uwsgi and Nginx configurations to deploy a project. You can look over this(http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html) beautiful and simple docs.
1. Install ansible:
Recomended: pip install ansible
Archlinux: pacman -S ansible
Note: archlinux uses python3 as default so you need to create a file /etc/ansible/group_vars/all and paste:
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
2. Check if it's working:
Create /etc/ansible/hosts file with
and execute: ansible all -m ping
you will get a response as pong. The above command executes ping over all servers to specify any group(specific servers) then execute: ansible server1 -m ping
3. Example ansible playbook.
Note: Here I assume Ubuntu 14.04 server and ssh permission to it.
Let's install vim over the server.
First provide the IP address in /etc/ansible/hosts file
Now create a task file. Ansible uses yml language.
create script.yml and paste the following
Now create a playbook which runs above created role over specified hosts:
create playbook.yml with the following
The above script installs latest vim package with apt-get package manager.
4. Update your ubuntu server:
First, before updating we have to create grub file as some packages require it. So create update_ubuntu.yml file with
Now as before creating a playbook with required configs:
5. Create Django project:
Create file django_project.yml and paste:
Note: project_name is provided in the playbook.yml file.
Now create playbook.yml file:
run ansible-playbook playbook.yml
That's it, your django project is created.
6. Create templates for uwsgi, Nginx and supervisor
The supervisor is just a process control system like it starts or stops an instance. Here we control uwsgi service using su
Things that we need here are the configuration files, ansible uses jinja2 template for configurations.
First create uwsgi template:
create uwsgi.j2 file and paste:
For running uwsgi in emperor mode we use supervisor.
Create configuration for supervisor, so create uwsgi_server.j2 jinja2 template and paste:
Now create nginx templates:
In nginx.j2 paste:
In step 7 we create roles using these templates.
7. uWSGI and Nginx roles:
Create uwsgi_django_setup.yml file and include:
Nginx Django setup role:
create file nginx_django_setup.yml and paste:
Finally, create a playbook.yml:
Run ansible-playbook playbook.yml, your project will be deployed.
Note: We have hosted these scripts over ansible galaxy. You can pull using:
ansible-galaxy pull micropyramid.django