Skip to main content

Command Palette

Search for a command to run...

πŸš€πŸ“… Day 45 DevOps Challenge -Understanding Configuration Management with Ansible

Published
β€’5 min read
πŸš€πŸ“… Day 45 DevOps Challenge -Understanding Configuration Management with Ansible
A

Passionate software engineering student | DevOps enthusiast | Seeking innovation and excellence in software engineering! πŸ‘¨β€πŸ’»

Welcome to Day 45 of our DevOps and automation series! Today, we are delving into the intricacies of Configuration Management with Ansible, a robust open-source automation tool. Ansible is versatile, allowing you to automate various IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

What is Ansible? πŸ€–

Ansible is an open-source automation tool used for configuration management, application deployment, task automation, and orchestration. It allows you to manage and automate the provisioning and configuration of infrastructure, making it easier to deploy and manage applications and services.

  1. Agentless 🚫: Ansible is agentless, meaning it doesn't require any software or agent to be installed on the target hosts. It uses SSH (Secure Shell) and PowerShell to communicate and execute commands remotely.

  2. Playbooks πŸ“˜: Ansible uses playbooks, which are written in YAML format and define a set of tasks and configurations that need to be applied to target hosts. Playbooks allow for the automation of tasks and processes in a structured and reusable way.

  3. Modules 🧩: Ansible uses modules to perform specific tasks on target hosts. Modules are pre-built scripts that are executed on the target hosts to carry out actions like installing packages, configuring services, managing users, etc.

  4. Inventory πŸ“‹: Ansible uses an inventory file to define the hosts on which actions will be performed. The inventory file can be static or dynamic and is used to group hosts and assign variables for use in playbooks.

  5. Ad-hoc Commands πŸ› οΈ: Ansible allows you to run ad-hoc commands against one or more hosts without the need for a playbook. This is useful for performing quick, one-off tasks.

  6. Roles 🎭: Roles in Ansible provide a way to organize and structure playbooks. They allow for code reuse and make it easier to manage and maintain playbooks by breaking them down into modular components.

  7. Templates πŸ“: Ansible can use Jinja2 templates to generate dynamic configuration files based on variables and other data, allowing for flexible and customizable configurations.

  8. Handlers πŸ›‘οΈ: Handlers are used in Ansible to trigger actions based on certain conditions. They are typically used to restart services or perform other actions after a configuration change.

  9. Task Control ⏩: Ansible provides options for task control, such as error handling, task retries, and task dependencies, to ensure that tasks are executed in a controlled and reliable manner.

Ansible simplifies complex tasks related to managing and configuring systems and applications, making it an effective tool for automating various aspects of IT operations and infrastructure management. πŸš€

Task-01: Installation of Ansible on AWS EC2 (Master Node) πŸ› οΈ

To kick off our exploration of Ansible, let's start by installing it on an AWS EC2 instance. This instance will serve as the master node for our Ansible configurations. Follow these steps to set up Ansible:

sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible

These commands add the Ansible repository and install Ansible on the EC2 instance, effectively configuring it as a master node for our Ansible operations.

Task-02: Understanding the Hosts File πŸ“

In Ansible, the 'hosts' file is a crucial component that defines the hosts (machines or nodes) Ansible will manage. Let's explore this file and list the configured hosts:

sudo nano /etc/ansible/hosts
ansible-inventory --list -y

The 'hosts' file is where you define the IP addresses or hostnames of the machines you want Ansible to manage. The ansible-inventory command assists in listing the configured hosts, providing crucial insights into the managed infrastructure.

Task-03: Setting Up Additional EC2 Instances βš™οΈ

To deepen our understanding of Ansible, we'll set up two more EC2 instances (nodes) with the same private keys as the previous instance. Here's a step-by-step guide:

  1. Create EC2 Instances: Launch one new EC2 instance on AWS and ensure it uses the same private keys as the master node. Make note of the IP address of this new instance, as you'll need it to add the target server's IP address to the Ansible host file.

  2. Add Target Server IP Address to Ansible Host File: Open the Ansible 'hosts' file located at /etc/ansible/hosts on the Ansible master server using your preferred text editor, such as Nano:

     sudo nano /etc/ansible/hosts
    

    In this file, you'll define the IP addresses or hostnames of the machines you want Ansible to manage. Add the IP address of the target server (the newly created EC2 instance) following the format:

     [target_servers]
     <target_server_ip>
    

    Replace <target_server_ip> with the actual IP address of the target server.

    By adding the target server's IP address to the Ansible host file, you enable Ansible to communicate and manage this new instance seamlessly.

  3. Generate SSH Key Pair:

    Open your ansible-master-server terminal and run the ssh-keygen command to generate a new SSH key pair. The id_rsa and id_rsa.pub files will be saved in the ~/.ssh/ folder by default.

     ssh-keygen
    

  4. Copy the Public Key:

    After generating the SSH key pair, you need to copy the public key (id_rsa.pub) to the target server.

     cat ~/.ssh/id_rsa.pub
    

    This will display the contents of the public key. Copy the entire output.

  5. Copy to Authorized Keys on Target Server:

    Log in to the target server and navigate to the ~/.ssh/ folder (create it if it doesn't exist):

     mkdir -p ~/.ssh
    

    Inside the ~/.ssh/ folder, create a file called authorized_keys if it doesn't already exist:

     touch ~/.ssh/authorized_keys
    

    Edit the authorized_keys file and paste the public key you copied earlier into this file:

     nano ~/.ssh/authorized_keys
    

    Paste the copied public key into the authorized_keys file, save, and exit the editor.

  6. Set Permissions on the Target Server:

    Ensure proper permissions are set for the ~/.ssh/ folder and the authorized_keys file on the target server:

     chmod 700 ~/.ssh
     chmod 600 ~/.ssh/authorized_keys
    

    These commands set the appropriate permissions

  7. Ping Command using Ansible: Try a ping command using Ansible to communicate with the new nodes:

ansible -m ping <node-ip>

Replace <node-ip> with the IP address of the node you want to ping. This command showcases the fundamental functionality of Ansible in communicating with and managing remote hosts.

Configuration management with Ansible is a powerful tool that streamlines and automates tasks within an IT infrastructure. Stay tuned for more insights and practical hands-on experiences as we continue our journey into the realm of Ansible and automation.

Join me tomorrow as we delve deeper into Ansible and explore its playbook functionalities! Happy automating! πŸŽ‰

More from this blog

Adarsh Jha

65 posts