Introduction to Ansible

An introduction and tutorial to the Ansible technology

Introduction to Ansible

What is Ansible?

Ansible is a tool invented for making depoyment of applications and environments effefortless and reliable. The tool is developed and maintained by Red Hat, making it dependent on a Rat Hat distribution of Linux.

From my experience developers have gotten familiar with the idea of having an application within a Docker container. This makes the deployment of both the environment and the application simple and easy.Β 

Although Docker containers are a step in the right direction, it is still a hassel to deploy multiple containers over multiple nodes as the same time. From my perspective, this is a normal scenario for a micro-service architecture.Β 

This is where Ansible comes into the picture. Ansible is able to manage the deployment of various application and containers through YAML configuration file (playbook) and a predefined file structure (we will see an example later…).

Ansible is essentially making a SSH connection to the desired node for deployment, transfering the necessary content to the node (SCP), and finally setting up the environment and application to match the playbook configration.

In many scenarioes the playbook configuration may be reused for different environments (e.g. customers). It will only be necessary to change the content of the Ansible file strcuture. Thereby changing the content (e.g application configurations) which will be deployed, but not the process which it will follow.Β Β 

It is also worth mentioning, that other tools like Ansible also exists (e.g. Salt). However, Ansible has the benefit of not having any requirements for preinstalled packages on the nodes to be deployed on. If Ansible can SSH to the node, it can deploy to it.Β 

INTEGU - What is Ansible

Use cases

In terms of uses cases, Ansible can be used for both static and dynamic environment deployments. In this context, a static deployment references to a fixed set of deployment steps (e.g. technologies to deploy with, order of steps etc.) and deployment files (e.g. software components and configuration files). This use case could ideally be used to re-establish the same state in a test environment.

If, on the other hand, the deployment files are changed between deployments, it can be considered a dynamic deployment. Changing deployment files would for example allow for new software components (e.g. Docker containers, jars, AppImages, and rpm) or different configuration files (e.g. JSON and YAML) to be deployed. A dynamic deployment can be used, but not limited, to manage deployment of environments in a continues development (CI/CD). Β 

Tutorial

In the section below, I will provide a tutorial for how to setup an Ansible deployment for deployment of two docker containers.Β 

I have put an emphasis on providing the prerequists and and necessary directory structure to execute the example.

Prerequisites:

Before you begin, make sure you have the following prerequisites installed on your system:

  1. Ansible: You can install Ansible by following the official documentation for your OS: Ansible Installation Guide.

  2. Docker: Install Docker by following the official documentation for your OS: Docker Installation Guide.

Directory Structure:

				
					ansible-project/
β”‚
β”œβ”€β”€ inventory/
β”‚   └── hosts
β”‚
β”œβ”€β”€ playbooks/
β”‚   β”œβ”€β”€ deploy_container1.yml
β”‚   └── deploy_container2.yml
β”‚
β”œβ”€β”€ roles/
β”‚   β”œβ”€β”€ common/
β”‚   └── containers/
β”‚       β”œβ”€β”€ tasks/
β”‚       β”‚   β”œβ”€β”€ container1.yml
β”‚       β”‚   └── container2.yml
β”‚       └── templates/
β”‚           β”œβ”€β”€ Dockerfile_container1.j2
β”‚           └── Dockerfile_container2.j2
β”‚
└── ansible.cfg

				
			

Here’s a brief explanation of the directory structure:

  1. inventory/: Store your Ansible inventory (host information) in the hosts file.
  2. playbooks/: Create separate YAML files for deploying each Docker container.
  3. roles/: Organize your Ansible roles. We’ll have two roles for each container, containing tasks and Dockerfile templates.
  4. /:Β Ansible configuration files.
1. Inventory File (hosts):

Create an inventory/hosts file with the following content:

				
					[containers]
server1 ansible_ssh_host=your_server_ip

				
			

Replace your_server_ip with the IP address of the server where you want to deploy the containers.

2. Playbooks:

Create two Ansible playbooks to deploy each container.

playbooks/deploy_container1.yml:

				
					---
- name: Deploy Container 1
  hosts: containers
  roles:
    - role: containers
      vars:
        container1_image: "your_container1_image:tag"

				
			
3. Ansible Roles:

Now, let’s create two Ansible roles for deploying the Docker containers.

The code snippet below is written in yaml. The two role-files are nearly identical. Therefore I will assume that reader will be able work from one role-file.

				
					---
- name: Deploy Container 1
  docker_container:
    name: container1
    image: "{{ container1_image }}"
    state: started
  vars:
    container1_image: "your_container1_image:tag"

				
			

Replace your_server_ip with the IP address of the server where you want to deploy the containers.

4. Ansible Configuration (ansible.cfg):

Create an ansible.cfg file in the root directory of your project:

				
					[defaults]
inventory = ./inventory/hosts

				
			

This configuration specifies the inventory file location.

Deployment

Now, you can deploy the containers using the following commands:

				
					ansible-playbook playbooks/deploy_container1.yml

				
			

Replace "your_container1_image:tag"Β with your actual Docker image names and tags.

That’s it! You’ve set up an Ansible project to deploy a Docker containers with the specified directory structure and code examples. You can expand this setup to include more containers and customize it according to your requirements.

Hopefully, this quick introduction to Ansible provided you with enough knowlegde to start using it yourself.Β 

Recommended Reading

Books

  • Ansible: Up and Running, 2nd Edition – Lorin Hochstein, Rene Moser – Link

Articles

  • Ansible – Link
  • Salt Project – Link

Β 

About

Hi, I'm the Author

My name is Daniel H. Jacobsen and I’m a dedicated and highly motivated software developer with a masters engineering degree within the field of ICT.Β 

I have through many years of constantly learning and adapting to new challenges, gained a well-rounded understanding of what it takes to stay up to date with new technologies, tools and utilities.Β 

The purpose of this blog is to share both my learnings and knowledge with other likeminded developers as well as illustrating how these topics can be taught in a different and alternative manner.

If you like the idea of that, I would encourage you to sign up for the newsletter.

Cheers! 🍺

Didn't Find What You Were Looking For?

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Scroll to Top
INTEGU - Cookie-consent

INTEGU uses cookies to personalize your experience and provide traceability for affiliate links. By using the website, you agree to these terms and conditions. To learn more see the privacy policy page.