The web content provides a comprehensive guide on automating Docker deployments of Apache Airflow using Ansible, emphasizing the benefits of automation and the simplicity of Ansible's YAML configurations.
Abstract
The article "Automate Docker Deployments with Ansible" delves into the process of deploying an Apache Airflow container on a cloud-based virtual machine using Ansible for automation. It builds upon a previous blog post that discussed dockerizing Airflow orchestrations. The author, from Lynx, highlights Ansible's role in bridging the gap between manual deployment steps and full automation. The tutorial covers Ansible's key components, its open-source nature, ease of use with YAML, and its agentless operation. The benefits of using Ansible include its flexibility across different environments and its ability to handle complex workflows. The tutorial walks through setting up Ansible, creating an inventory file, defining group variables, and executing roles for installing Docker and deploying the Airflow application. The process involves building a Docker image, copying DAGs, and running docker-compose up. The article concludes by reinforcing Ansible's effectiveness in automating application deployments, especially when paired with Docker.
Opinions
The author advocates for the use of Ansible in automating infrastructure and application deployments, citing its simplicity and the use of YAML for configurations.
Ansible is praised for being open-source and agentless, which simplifies the setup and maintenance of deployment processes.
The article suggests that Ansible provides a robust solution for consistent and scalable deployments across various environments, including servers, VMs, and cloud platforms.
The author's positive stance on Ansible is evident, as it is described as a tool that can cater to complex workflows and orchestrations without requiring advanced coding skills.
The tutorial format of the article implies that the author believes in the practicality and accessibility of Ansible for DevOps tasks, making it a valuable tool for organizations looking to automate their deployment pipelines.
Docker solves big problems by providing continuous development and testing environments with rapid deployment cycles. It helps you package your application code with dependencies as an image to provide consistent conditions that never break.
I have covered how a data engineer can leverage dockerizing custom airflow orchestrations with ease in the previous blog post.
However, there is still a missing link. What if you want to avoid doing any manual steps and automate this process of building and deploying your airflow container on a VM on the cloud. We use Ansible, an IT automation tool at Lynx, to achieve this last mile automation.
This article will cover a tutorial on how you can automate the deployment of an Airflow container using Ansible.
Introducing Ansible
Ansible is an IT automation tool that allows you to automate infrastructure and application deployments. It has 6 key components used for automation in their respective domain.
Screenshot from Ansible website
Automation is quite important these days, to build robust applications organizations prefer infrastructure and applications to be managed and scaled as per need.
Benefits of using Ansible
Free and open source
Provides a simple setup that uses YAML configurations, no special coding skills are required
Cater for complex workflows, including deployment on multiple servers, clusters, and orchestrations
Ansible is agentless, you don’t need to install separate software on hosts.
Provides flexibility to deploy on servers, VMs, or cloud with the same YAML scripts
Now that we have looked into the benefits, let’s look into an actual tutorial deploying docker applications using Ansible.
Tutorial on using Ansible for docker deployments
Codebase
If you are interested in directly looking into the source code you can find it here
Project structure
This project has 4 key components in terms of structure:
ansible: This is where automation code using ansible playbooks live
dags: Airflow dags
docker: Docker deployments of airflow copied from a previous blog post
setup: Setup shell scripts for docker lives here
Docker deployment from the previous blog post
In the previous blog post, I covered a full tutorial on how to deploy airflow using docker. I will be reusing the same component as it is and bringing the whole folder to this new project.
Please make sure in your localhost Ansible PIP package is installed, if not you can first create a virtual environment, activate the virtualenv, and then install ansible using pip
This is where you provide variables to be used by the Ansible playbooks:
| — all.yml
ansible_user: <user_name>
ansible_ssh_private_key_file: ~/.ssh/id_rsa
We are using this to provide a path to the private key and name of the ansible user
inventory
Using this to provide the IP address of the host under [webservers]. The ansible script picks up this webserver IP address as a host where the automation is run
| — hosts.ini
[webservers]10.2.3.1
roles
Within roles/ folder, you can see two key components of the Ansible deployments
Screenshot by the author
Setup Docker: This is where the script in the setup folder is used to install docker on the host system. This is one of the ansible-playbook under install-docker → tasks → main.yml.
SERVICE_USER=syal
sudo su
usermod - aG wheel ${SERVICE_USER}
sed -i 's/%sudo\s\+ALL=(ALL:ALL)\s\+ALL/%sudo ALL=(ALL:ALL) NOPASSWD: ALL/' /etc/sudoers
Deploy Docker: This is where we will build the image, copy the dags and code, and docker-compose up. This ansible-playbook covers the following steps:
- Creating directories
- Building Docker image on localhost
- Copying and load docker image
- Copy the Dags folder
- Docker Compose up
If you are bounded by the infrastructure and services provided, Ansible can still help you automate your application deployments. Especially when combined with Docker, it provides a generic solution for deployments with ease.