Understanding Ansible : An Introduction and Installation Guide

Understanding Ansible : An Introduction and Installation Guide


Ansible is an open-source automation tool that simplifies IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. It's designed to be simple to set up and use, powerful, and flexible.

What is Ansible?

Ansible is a tool that automates repetitive IT tasks. Here are a few key points about Ansible:

  • Open-Source: Ansible is free to use and modify, with a large community contributing to its development.

  • Automation: Automates tasks such as server setup, application deployment, and task orchestration.

  • Configuration Management: Ensures that your systems and applications are in the desired state.

  • Agentless: Unlike other tools, Ansible doesn't require any software to be installed on the client machines (nodes). It uses SSH to communicate with them.

Why Use Ansible?

  • Simple Syntax: Ansible uses YAML (Yet Another Markup Language) for its playbooks, which are easy to read and write.

  • No Agents: Ansible is agentless, meaning you don't need to install any software on the nodes you manage.

  • Idempotent: Ansible ensures that operations can be applied multiple times without changing the result beyond the initial application.

  • Powerful Modules: Ansible has a vast library of modules for various tasks, from managing files and packages to configuring network devices and cloud services.

Getting Started with Ansible

Let’s dive into the process of installing Ansible on an AWS EC2 instance and setting it up to manage multiple nodes.

Task 1 : Installing Ansible on AWS EC2 (Master Node)

To start using Ansible, you’ll need to install it on a master node. The master node is the main server that you will use to control other nodes (servers).

Steps to Install Ansible on an AWS EC2 Instance:

  1. Launch an EC2 Instance:

    • Log in to your AWS account: Go to the AWS Management Console and log in.

    • Navigate to EC2 Dashboard: In the Services menu, select EC2 to open the EC2 Dashboard.

    • Launch Instance: Click on the “Launch Instance” button.

    • Choose an Amazon Machine Image (AMI): Select an Ubuntu AMI (Amazon Machine Image), such as Ubuntu Server 20.04 LTS.

    • Choose Instance Type: Select an instance type, such as t2.micro, which is free-tier eligible.

    • Configure Instance Details: Leave the default settings and proceed to the next step.

    • Add Storage: The default storage configuration is usually sufficient.

    • Configure Security Group: Create a new security group or use an existing one to allow SSH (port 22) access.

    • Review and Launch: Review your instance settings and click “Launch.” Select or create a key pair to access the instance.

  2. Connect to Your EC2 Instance:

    • Download the private key file (.pem): Make sure to save it securely as it will be used to connect to the instance.

    • Use SSH to connect: Open a terminal and use the following command to connect to your EC2 instance:

        ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
      

      Replace /path/to/your-key.pem with the path to your private key file, and your-ec2-public-ip with the public IP address of your EC2 instance.

  3. Install Ansible:

    • Add Ansible PPA repository:

        sudo apt-add-repository ppa:ansible/ansible
      
    • Update the package list:

        sudo apt update
      
    • Install Ansible:

        sudo apt install ansible
      

Task 2 : Understanding the Hosts File

The hosts file is a configuration file where you define the servers that Ansible will manage. This file tells Ansible where your nodes are and how to connect to them.

  1. Open the Hosts File:

    • Use the nano text editor to open the hosts file:

        sudo nano /etc/ansible/hosts
      
  2. Edit the Hosts File:

    • Add the IP addresses or hostnames of the nodes (servers) you want to manage. You can organize them into groups. For example:

        [webservers]
        192.168.1.10
        192.168.1.11
      
        [databases]
        192.168.1.20
      
  3. List the Inventory:

    • To verify your hosts file and see the inventory, run:

        ansible-inventory --list -y
      

Task 3 : Setting Up Additional EC2 Instances

For this task, you need to set up additional EC2 instances (nodes) and manage them using Ansible.

  1. Launch More EC2 Instances:

    • Repeat the steps from Task 1 to launch additional EC2 instances. Ensure you use the same private key pair as the master node.
  2. Use the Same Private Keys:

    • Make sure the new instances use the same private key pair. This ensures that Ansible can connect to them without additional configuration.
  3. Copy the Private Key to the Master Server:

    • Copy the private key to the master server using SCP or SFTP. For example, you can use the scp command:

        scp -i /path/to/your-key.pem /path/to/your-key.pem ubuntu@your-master-public-ip:/home/ubuntu/
      
  4. Ping the Nodes Using Ansible:

    • Test the connection to the nodes using the ping module:

        ansible all -m ping -u ubuntu --private-key=/path/to/your-key.pem
      
    • You should see a response from each node indicating that the connection is successful.

  5. Document the Process:

    • Take screenshots of each step and document the process in a blog post. Include details on the challenges you faced and how you overcame them.

    • Share the blog post on LinkedIn to help others learn how to set up and use Ansible.

Conclusion

By following these steps, you’ve learned how to install Ansible on an AWS EC2 instance, configure the hosts file, and set up additional nodes. Ansible makes it easy to manage multiple servers and automate tasks, which can save you time and reduce errors in your IT environment.

HAPPY LEARNING : )