You've probably heard ๐ข about Ansible if you've gone through Continuous Deployment.
Ansible is one of the easiest ways to automate the time-consuming ๐ and exhausting process of manual deployment. The ansible documentation contains a detailed description of its usage scenarios.
This blog is primarily concerned with its implementation ๐.
Before I begin, I'd like to thank realpython.com/automating-django-deployment.., which I primarily used to construct my ansible project, and therefore, the folder/file structures may appear identical.
Let's not waste any time and get right to the implementation ๐.
My folder structure ๐ธ is as follows:
For each file and folder, I shall provide a brief description.
group_vars ๐-> This folder contains all ansible attributes.
- all -> In this file, I've declared all ansible variables.
roles ๐-> On this folder, we may define all of the roles that must be fulfilled.
common -> This is the role that must be completed.
tasks โ -> Here you'll find all of the required tasks that must be completed on the server.
01_server.yml -> All of the server's requirements are installed here with root privileges. The following is installed:
- python3.8 as the default (you can install any language and make it as default)
- nginx, nginx-extras, and git
- server dependency packages (such as pip, db client, etc..)
- gunicorn and supervisor
- create an app directory ๐ and any additional directories if required
02_git.yml -> I used git to clone the latest code
- 03_dependencies.yml -> Install application dependency packages and copy - environment and configuration files to the appropriate folder
- 04_migrations.yml -> Configure the environment and upgrade the database โฌ๏ธ using migrations.
- 05_nginx.yml -> Set up nginx, SSL certificates, and the nginx configuration file. I've taken the nginx configuration file from the app structure and loaded it.
- 06_gunicorn.yml -> gunicorn should be configured and supervisor should be restarted ๐.
- 07_systemd.yml -> Restart nginx and app using supervisor. Assign the necessary permissions to log files.
- 08_sanity_check.yml -> Conduct a sanity check to ensure that the application has launched and is functioning properly.
- main.yml -> This is the key YML file โ, which contains all the above eight steps. From here, we can remove or add tasks. If necessary, we can even do a conditional step.
templates ๐-> Templates and configuration files are stored in this folder.
- nginx_conf -> Where we can maintain nginx configuration
- your_app_name.conf -> supervisor file for the corresponding application
deploy.yml ๐-> This is the primary file that will be used to run ansible-playbook. We will need roles, hostnames, and users to deploy it, and they are all mentioned here.
hosts ๐ง -> This file merely lists the hosts for the various environments in which the application must be deployed.
Please see Ansible Implementation โจ in my GitHub account for the detailed ansible implementation.
Thanks for reading ๐, I hope you now have a basic understanding of ansible implementation!