Ansible

Ansible

What is Ansible??

Ansible is an open-source automation tool that allows us to automate the deployment, configuration, and management of their infrastructure. It is a simple and easy-to-use tool that can help automate repetitive tasks, such as installing software, configuring servers, and deploying applications.

In simple terms, Ansible helps you automate the repetitive tasks that you would normally do manually. For example, imagine that you have to install a new software on multiple servers, you would have to go to each server and install the software manually. With Ansible, you can automate this process by writing a script that will install the software on all the servers at once. This saves you time and effort and ensures that all the servers have the same software installed.

Ansible Architecture

Ansible uses a client-server architecture. The client is called the "Ansible Control Node" and the server is called the "Managed Node." The Ansible Control Node is responsible for running the Ansible commands and communicating with the Managed Node. The Managed Node is the server or device that is being managed by Ansible. Ansible is agentless and the Control Node communicates through ssh.

Components

Ansible provides several key components that make it a powerful and flexible automation solution. Here's an overview of some of these components with examples:

Ansible modules:

Ansible modules are small programs that perform specific tasks on target hosts. These modules can be executed as part of Ansible playbooks. Ansible ships with a large collection of built-in modules, and you can also create custom modules for specific tasks.

Example: Let's say you want to use the apt module to ensure that a package, like "nginx," is installed on a target server. Here's an example task in a playbook:

- name: Ensure nginx is installed
  apt:
    name: nginx
    state: present

Ansible Playbooks:

Ansible playbooks are YAML files that define a set of tasks to be executed on one or more remote hosts. Playbooks describe what should be done, in what order, and under which conditions. Playbooks are the foundation of automation in Ansible.

Here's a simple Ansible playbook that installs Nginx, starts the Nginx service, and ensures it's enabled on system boot:

---
- name: Install and configure Nginx
  hosts: web_servers
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes

Inventories:

Ansible uses an inventory file to define the hosts on which you want to execute tasks. The inventory file can contain information about hostnames or IP addresses, remote users, and groupings of hosts.

An inventory file may look like this:

[web_servers]
web1.example.com
web2.example.com

[db_servers]
db1.example.com
db2.example.com

Ansible Variables:

Ansible allows you to define variables in your playbooks or inventory to make your automation more flexible and reusable. Variables can be used to customize the behavior of your tasks.

In your playbook, you can define variables like this:

vars:
  http_port: 80
  https_port: 443

Ansible Roles:

Ansible roles are a way to organize and reuse Ansible tasks, handlers, and variables in a structured manner. Roles allow you to break down complex automation tasks into smaller, reusable components.

Did you find this article valuable?

Support Nikhil Chauhan by becoming a sponsor. Any amount is appreciated!