Ansible

Ansible

Simplifying IT Automation: The Power of Ansible

·

6 min read

Ansible is an open-source IT automation tool used to manage and configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero-downtime rolling updates. It simplifies and streamlines tasks that would otherwise be repetitive or time-consuming.

Key Features of Ansible:

  1. Agentless: Unlike some other automation tools, Ansible does not require any agent or additional software to be installed on the target machines. It uses SSH (or WinRM for Windows) to connect and manage them.

  2. Declarative Language: Ansible uses YAML-based playbooks to define tasks, making it easy to read and write configurations.

  3. Idempotent: It ensures that tasks produce the same result regardless of how many times they are executed.

  4. Lightweight: There is no heavy infrastructure or server required to run Ansible. All you need is Python installed on the control machine.

  5. Extensible: Ansible is highly modular, with a wide range of modules to handle various tasks, from provisioning cloud resources to configuring databases.

What Can Ansible Do?

  • Configuration Management: Automates the configuration of systems and keeps them in a consistent state.

  • Application Deployment: Deploys applications across multiple servers with minimal effort.

  • Provisioning: Sets up the necessary infrastructure for an application (e.g., creating VMs, networks, and storage).

  • Orchestration: Coordinates the workflow between multiple systems and tools.

  • Security Automation: Applies consistent security policies and configurations across all systems.

Why Use Ansible?

  • Simple: Easy to learn and use, especially for those with no coding background.

  • Efficient: Reduces manual tasks and human error, speeding up IT operations.

  • Cross-Platform: Works on Linux, Windows, cloud environments, and even network devices.

  • Scalable: Manages a single machine or thousands of machines seamlessly.

In short, Ansible is an essential tool for IT administrators and DevOps engineers looking to automate and simplify their workflows.

Now lets see how ansible works through step by step.

Step 1 : In the first step we have create a instances in AWS and connect it through the ssh in your terminal and update the instances in your terminal.

Step 2 : Now lets install the ansible by the below images.

Now we have successfully installed ansible in your system.

Step 3 : In Terra form, you automate the process of launching instances by writing configuration files, which define the desired state of your infrastructure. Terra form then uses these files to create and manage the infrastructure automatically.

In Ansible, while you don't typically launch instances directly, you can update and configure those instances once they are created. Ansible uses playbooks to define the tasks needed to configure and manage the instances, ensuring they are in the desired state. This approach allows you to automate the configuration and management of instances without manual intervention.

Step 4 : Now lets see the updating the instances using ansible through practical .

To edit the Ansible hosts file, follow these steps:

  1. Open the terminal on your control machine.

  2. Use the command sudo nano /etc/ansible/hosts to open the hosts file in the nano text editor.

  3. In the hosts file, add the public IP address of the instances created using Terraform. You can organize them under a group name if needed. For example:

  4. Replace ip address with the actual public IP address of your instance, your_user with the SSH username, and /path/to/your/private/key with the path to your SSH private key.

  5. Save the changes by pressing CTRL + O, then press Enter.

  6. Exit the editor by pressing CTRL + X.

This configuration allows Ansible to connect to your instances using the specified SSH user and key.

Step 5 : Running the command ansible all -m ping -u ubuntu is used to test connectivity to all hosts listed in your Ansible inventory file. Here's a breakdown of the command:

  • ansible: This is the command-line tool for running Ansible tasks.

  • all: This specifies that the command should be run on all hosts defined in the inventory file.

  • -m ping: This uses the "ping" module, which is a simple way to test the connection to the hosts. It doesn't actually use ICMP ping; instead, it checks if Ansible can connect via SSH and execute Python.

  • -u ubuntu: This specifies the SSH user to connect as, in this case, "ubuntu".

When you run this command, Ansible attempts to connect to each host using SSH and execute a simple Python script to confirm connectivity. If successful, you'll see a "pong" response from each host, indicating that Ansible can communicate with them. If there are any issues, such as SSH connectivity problems or incorrect user credentials, you'll receive error messages detailing the problem.

The inventory file in Ansible lists the systems (hosts) it manages. It helps define targets, group them logically (e.g., webservers, databases), and assign custom variables for dynamic configurations.

Now lets see a small project using playbook:

Step 1 : Create a New YAML File: Open your terminal and create a new file with a .yml extension. For example, you can use the command nano my_playbook.yml to create and open a new file in the nano text editor.

For example, you can use the command nano my_playbook.yml to create and open a new file in the nano text editor.

  • hosts: Specifies the target hosts or groups of hosts.

  • tasks: Lists the tasks to be executed on the target hosts

  • services: It will list the service stauts and enable the service .

As you can see in the bellow image.

Step 2 : Create a New HTML File: Open your text editor and create a new file named index.html.

This index.html wil be linked to the playbook yml file in the previous step.

Step 3 : To run a YAML playbook file in Ansible, you would use the ansible-playbook command followed by the name of your playbook file. If your playbook is named deploy_static_page_play.yml.

Make sure that your playbook file is correctly named and located in the directory from which you are running the command. If you need to specify a different inventory file or other options, you can include those in the command as well.

Step 4 : To access your deployed application or service, you need to copy the public IP address of your instance and paste it into your web browser's address bar. This will allow you to view the application or service running on that instance. Make sure that any necessary ports (such as port 80 for HTTP) are open in your instance's security group settings to allow incoming traffic.

THANK YOU FOR READING!

Do follow me on LinkedIn for further blogs.

linkedin.com/in/manasa-j-03b633238