Ansible is an open source configuration management tool. In simple terms, you can use ansible to provision an environment, configure it and deploy it. In this article, we will be discussing on how we use ansible to deploy your code onto the servers.

So what is deployment?

The basic steps to deploy code onto the infrastructure is as follows –

  • Install the servers such as web server, appserver on the Linux/Unix/whatever server you use.
  • Next, get your packages from your artifactory and deploy them on to the servers you’ve installed.
  • Now restart the servers.

These are the basic steps you have to accomplish to get your deployment done. Let’s get to the structure on how we accomplish this using ansible.

Terminology –

Playbook – Is an ansible file that runs the deployment or the tasks required for the deployment. Playbooks file extension is ‘.yaml’ or ‘yml’.

Ansible Control server – The server from which you run the ansible playbooks.

Target servers – The servers on which you deploy the code using ansible.

Ansible.cfg – This file is the config file for your ansible model. You can customize things here.

Group_vars – This is the directory that consists of files that has the key value variable pairs in here.

Inventory – This is the folder where you have files that consists of the server ip’s or names in it.

Roles – Roles is a main directory that has the tasks playbook, handlers, and templates in it. There are many other folders in it which I will not be mentioning as this is a basic article on ansible.

Tasks – This has a main.yaml file which has the deployment tasks in it.

Handlers – This folder has main.yaml file that has tasks which are kicked by tasks in tasks folder.

Templates – This has the template files in jinja2. These files are usually the config files.

The main playbook – This is the actual playbook that we run. This runs the tasks in the roles folder on the hosts as per the list in the inventory file.

Simple syntax for running a playbook – ansible-playbook main_playbook.yaml

Now you know the basic structure of ansible playbook. The next thing are the modules that ansible provide which you use in the tasks to perform a specific action. This could be a simple ping module. If you think that ansible doesn’t provide a specific module you need, you can always write a customized module.

Advantages of using ansible –

–         It is open source.

–         Easily understandable.

–         Light weight. You just need python installed on the server boxes.

–         No ansible installed required on the target server.

–         Quick execution.