Brief Introduction
I will be discussing ansible as an automation tool, some of its benefits and features, how ansible achieves automation, Key Ansible components, how ansible is being used in different stages of a CI/CD Pipeline, ansible roles, ansible collections, ansible-galaxy, and ansible automation hub.
Ansible Definition
Ansible is an open-source software tool that is used for provisioning, configuration management, and application-deployment tool. It is also a pushed-based automation tool because the configurations changes are pushed from the central server to the node servers, unlike chef that is a pull-based automation tool. It also enables infrastructure as code. Infrastructure as code is a way infrastructures can be provisioned and configured via code.
scenario: If you declare in your script that the state of a web server must be started, ansible ensures the state is maintained anytime the server crashes - ansible will restart the server.
Some of the Features of ansible
Ansible scripts are developed using YAML and YAML is easy to learn. Ansible is designed as a desired state engine i.e if the target state of infrastructure is defined, ansible ensures that the state is maintained. Ansible lets you write tasks in individual files and reuse them in different playbooks. Ansible uses an agentless architecture i.e we do not need to install an agent in the node servers because the central server pushes the configurations to the node servers.
How Ansible achieve automation
It all starts by installing ansible in the central server or host server and identifying the nodes that ansible will control. But you will need to have an SSH connection if you have a linux server or a winRM connection for a windows server. The next step is to create the inventory file that holds the IP addresses of these node servers. You can group all your destination servers and categorize them as app servers, web servers, or database servers.
The next step is to run the playbook which comprises of multiple plays and each play has multiple tasks and each task has multiple modules. These modules will be pushed to the node servers and executed at the destination location and the required software is installed. Once the installation is completed the module is removed.
Key Ansible components
Playbook - The playbook is written in YAML and executed from top to bottom. It holds multiple plays which hold multiple tasks that contain ansible modules, which you run in a playbook. Modules - It's the module that gets executed in the node servers. When you install ansible, it comes prebuilt with a set of core modules like database modules, file modules, storage modules, etc and ansible let you build custom modules too. Inventory file - list the IP addresses of all the node servers i.e host machines of which these modules need to be executed.
Note that there is a list of ansible modules and how to use the parameters provided for each module on the ansible documentation.
An example of such a module is the source control module, Ansible provides modules for major source control systems like git, mercury, sub-version, and bitbucket. To download a git repository and push code changes automatically, we will use the git module provided by ansible. Also, ansible shell commands let you execute any shell command like running the playbook from the command line.
Next, let's talk about how ansible achieves automation in the different stages of a CI/CD pipeline.
- DEVELOPMENT PHASE - During development, ansible can be used in installing different software and software packages used by the developers.
Using ansible let's install a few software packages in a Linux machine that the developers will be using during their development.
Below is a playbook that installs vim, bat, exa, and podman packages using dnf package manager.
tasks:
- name: "Install system packages"
dnf:
name:
-vim
-bat
-exa
-podman
state: latest
the playbook below shows how to install openjdk8 using apt package manager
tasks:
- name: "Install latest version of openjdk-8-jdk"
apt:
name: openjdk-8-jdk
state: latest
- TESTING AND INTEGRATION PHASE - The first step is to ensure that the crucible server is running when the developer checks in the code to the source repository.
The playbook below uses an ansible module called "service", which is part of the system modules.
tasks:
- name: Restart crusible
service:
name =crusible
state:=reloaded
The service module lets you manage the services, the name parameter indicates the name of the service. The reloaded value for the state parameter ensures that the service is running and it will start the service if it is down.
Note that Ansible Collections can also be used to automate tasks. Ansible Collections are a higher abstraction of modules. In the future, ansible intends to use collections for distributing, maintaining, and consuming automation.
scenario: Imagine as part of your QA provisioning, you need to provision 1GB of a machine running a cent-os image. When we invoke an ansible playbook, the playbook will install other dependent packages, copy the build artifacts to a repository, and deploy the application.
- DEPLOYMENT PHASE: Note that Ansible provides a list of modules for all the cloud providers and most of their services. You can refer to the documentation for more info about the different modules ansible provides for different cloud providers.
ROLES — Ansible introduced the concept of Roles, which let you break down a large playbook into smaller manageable chunks called Roles. These roles can be used across multiple playbooks as reusable components. Ansible Roles are packaged as collections.
COLLECTIONS — These are micro component that contains roles, modules, playbooks, and plugins that addresses a set of use case.
Note that ansible modules used to be the distributed format for ansible but a bug fix and enhancement to the module means that they need to wait until the next ansible is released to update their module which causes delay. So Ansible changed this strategy and started packaging modules as part of collections that are independent of ansible releases. Hence ansible modules are no longer tightly coupled with its core releases.
ANSIBLE GALAXY AND ANSIBLE AUTOMATION HUB.
ANSIBLE GALAXY: Ansible uses ansible-galaxy as a repository to store all the Roles and Collections. You can check out ansible-galaxy through the command line or from the documentation website. when you install ansible, ansible-galaxy comes pre-installed with it.
ANSIBLE AUTOMATION HUB: This is the official location to discover and download supported collections, included as part of an Ansible Automation Platform subscription. These content collections contain modules, plugins, roles, and playbooks in a downloadable package. You can refer to the documentation website for more details.
In summary, other Automation tools are chef, puppet, ansible, and saltsatck. Also, ansible is called the desired state engine because the state of a resource must be maintained. For more information about ansible, you can refer to the ansible documentation website. Thank you