Continuous Deployment (CD) using Ansible

Continuous Deployment (CD) using Ansible

ยท

3 min read

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:

ansible_imple_img (4).jpg

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!

ย