Skip to main content

Command Palette

Search for a command to run...

Mastering Infrastructure Management with Ansible: Simplify, Automate, and Scale your Infrastructure!

Updated
β€’5 min read
Mastering Infrastructure Management with Ansible: Simplify, Automate, and Scale your Infrastructure!
P

πŸ‘‹ Hello! I'm passionate about DevOps and have over 1+ years of experience in the field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!

SKILLS:

πŸ”Ή Languages & Runtimes: Python, Shell Scripting, HCL, YAML πŸ”Ή Cloud Technologies: AWS, Microsoft Azure, GCP πŸ”Ή Infrastructure Tools: Docker, Terraform, AWS CloudFormation πŸ”Ή Other Tools: Linux, Git and GitHub Actions, Jenkins, Jira, GitLab (beginner), Docker, AWS DevOps πŸ”Ή Web Development: HTML, CSS, Bootstrap, Python, SQL

Job & Responsibilities:

πŸš€ Improved development efficiency by implementing CI/CD pipelines, resulting in a 30% reduction in deployment time on the test server. πŸ”’ Strengthened deployment and testing reliability by utilizing Docker containers and optimizing Dockerfile, reducing development issues on the test server by 20%. βš™οΈ Automated S3 bucket log creation with Shell scripting, eliminating 100% of manual search and saving 2 hours per week. πŸ“… Scheduled EC2 instance start/stop using Lambda functions and Event Bridge, leading to a 25% decrease in infrastructure costs. πŸ”§ Utilized AWS, Linux, Python, Docker, Shell scripting, Terraform, Jenkins Pipelines, and automation to streamline workflows and improve overall system performance.

I'm very detail-oriented and possess strong written and verbal communication skills. As a high performer with a possibility mindset, I strive to solve problems using efficient approaches.

Let's Connect & Grow:

If you find my profile suitable for the role you are searching for, please feel free to reach out to me at sumanprasad9766@gmail.com.

πŸ“ Introduction:

Ansible, an open-source automation tool, simplifies server management, configuration updates, and application deployments. In this blog post, we will explore the steps for installing Ansible, learn how to copy files to remote servers, configure server hosts and variables, execute commands on servers, and write Ansible playbooks for system configuration and package installations. Let's dive in!

πŸ”§ What is Ansible?

Ansible is an open-source automation tool that simplifies IT infrastructure management, configuration management, and application deployment. It provides a simple and agentless approach to automating tasks, making it easy to manage and control multiple servers from a single control node.

🌟 Why Use Ansible?

  • πŸš€ Simplifies Automation: Ansible eliminates the need for complex scripting and manual intervention, allowing you to automate repetitive tasks and streamline operations.

  • 🌐 Infrastructure Management: With Ansible, you can define and manage infrastructure as code, making it easier to scale, replicate, and maintain infrastructure components.

  • πŸ”§ Configuration Management: Ansible enables you to define the desired state of your systems, enforce configuration consistency, and perform system-wide updates effortlessly.

  • πŸš€ Application Deployment: Ansible facilitates the deployment of applications across multiple servers, ensuring consistency and reducing deployment time.

  • πŸ”„ Continuous Delivery: Ansible integrates with CI/CD pipelines, enabling automated deployments, testing, and rolling updates.

πŸ’‘ How Ansible Solves Problems - Real-Time Example:

Imagine you have a web application running on multiple servers, and you need to perform a routine configuration update. Manually logging into each server and making the changes can be time-consuming and error-prone. Here's where Ansible comes to the rescue:

🎯 Problem: Manual Configuration Updates Let's say you need to update the Nginx configuration file across all servers.

✨ Ansible Solution:

  1. πŸ“ Playbook Creation: Create an Ansible playbook, a YAML file that describes the desired state of the servers.

  2. πŸ“„ Define Tasks: Define tasks to copy the updated configuration file to the servers and restart the Nginx service.

  3. πŸ”‘ Inventory Configuration: Configure the inventory file to specify the target servers.

  4. πŸš€ Run the Playbook: Execute the playbook with a single command.

πŸ” Step 1: Installing Ansible

To begin, let's install Ansible on your control node using the following commands:

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

Now you have Ansible up and running, ready to automate your server management tasks.

πŸ” Step 2: Copying Files to Remote Servers

A common scenario is transferring files from your local machine to remote servers. For example, let's copy a .pem key to an Ubuntu server using the scp command:

scp -i "ansible-master.pem" /home/suman/Downloads/ansible-master.pem ubuntu@ec2-54-144-50-121.compute-1.amazonaws.com:/home/ubuntu/keys

This command securely copies the .pem key to the specified location on the remote server.

πŸ” Step 3: Configuring Server Hosts and Variables

To manage multiple servers, we need to configure them in the Ansible inventory file. Open the file using the following command:

sudo nano /etc/ansible/hosts

Inside the file, define your server groups and variables. For example:

[dev-server]
My-server-1 ansible_host=public_ip

[prod-server]
My-server-2 ansible_host=public_ip

[dev-server:vars]
ansible_ssh_private_key_file=/home/ubuntu/ansible-master.pem
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu

[prod-server:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu

Make sure to replace public_ip with the actual IP addresses of your servers. Save the file after making the necessary changes.

πŸ” Step 4: Checking Server Authentication

To verify if the authentication with servers is properly configured, we can use the ping module in Ansible. Run the following command:

ansible dev-server -m ping

This command will check the connectivity and authentication with the dev-server group.

πŸ” Step 5: Executing Commands on Servers

Ansible allows executing commands on remote servers easily. For example, to check the uptime of a server, use the following command:

ansible dev-server -a "uptime"

This command will display the uptime information for the servers in the dev-server group.

πŸ” Step 6: Writing Ansible Playbooks

Ansible playbooks are written in YAML format and define the desired state of the systems. Let's create a playbook to perform system configuration tasks. Create a file named commands_play.yml:

nano commands_play.yml

Inside the file, add the following playbook content:

---
- name: System configuration stats
  hosts: dev-server
  become: yes

  tasks:
    - name: Show date
      command: date

    - name: Checking uptime
      command: uptime

This playbook executes two tasks: displaying the current date and checking the server uptime.

πŸ” Step 7: Running Ansible Playbooks

To run the playbook and execute the defined tasks, use the following command:

ansible-playbook commands_play.yml -v

This command will execute the tasks defined in the playbook on the servers in the dev-server group.

πŸ” Step 8: Installing Nginx on Servers

We can also use Ansible to install packages on servers. For example, let's install Nginx on servers using a playbook. Create a file named nginx_install.yml:

---
- name: Install Nginx on server using Ansible
  hosts: dev-server
  become: yes

  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: latest

This playbook installs the latest version of Nginx on the servers in the dev-server group.

πŸ” Step 9: Deploying Web Pages with Ansible

Ansible can also deploy files to remote servers. To deploy an index.html web page to a server, modify the playbook as follows:

---
- name: Install Nginx on server using Ansible
  hosts: prod-server
  become: yes

  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: latest

    - name: Starting the server
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Deploy Web Page
      copy:
        src: index.html
        dest: /var/www/html/

This playbook installs Nginx, starts the service, and deploys the index.html file to the specified location.

πŸ’₯ Benefits:

  • βœ… Automation: Ansible automates the task of copying the configuration file and restarting services across all servers.

  • ⏱️ Time Savings: The process is significantly faster and eliminates the need for manual intervention.

  • πŸ’‘ Consistency: Ansible ensures that all servers have the updated configuration, maintaining consistency across the infrastructure.

πŸ“ Conclusion:

Ansible is a powerful automation tool that simplifies IT operations, improves efficiency, and reduces human error. Whether it's infrastructure management, configuration updates, or application deployments, Ansible offers a flexible and scalable solution for managing and orchestrating your systems. Embrace Ansible to unlock the potential for streamlined operations and increased productivity. By following the steps outlined in this guide, you can leverage Ansible to automate routine tasks, ensure consistency across your infrastructure, and save time. With its simplicity and versatility, Ansible empowers you to scale your operations efficiently. Start exploring Ansible today and unlock the potential of automation in your server management journey!πŸ’ͺπŸš€

πŸ” Checkout GitHub Repository for projects:

πŸ”— github.com/sumanprasad007

More from this blog

D

DeployToCloud

405 posts

πŸ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer with 2+ years of experience. Join ~5k followers and explore 320+ blogs on Python, AWS, Docker, Jenkins, Linux, and more. Let's connect & grow πŸš€