Automate software installation using Ansible

Introduction:

In this blog post, we will discuss how to automate software installation and configuration using Ansible. The primary focus is on harnessing Ansible’s capabilities to streamline the process of installing and configuring software automatically. If you’re keen on unlocking the potential of Ansible to automate tasks that would otherwise be manual, this article promises to enrich your understanding.

Let’s start with a brief introduction to Ansible – Ansible is a configuration management tool that means installing software on multiple systems. For example, if you have a fleet of 120-200 servers and you want to do a particular software installed on those servers, if you do it manually it will take time. So, we needed something that could automate the installation on multiple servers and the way forward was configuration management.

Using Ansible, any software that you want to install you can do with a single click of a button on all the servers that exist in your fleet using the master and slave connection of Ansible. You must specify on what machine what to install. 

It solves two things:

  • Software installed takes time when done manually, now with Ansible we can do it automatically. 
  • Second, there is no room for error because everything is automated. 

What is an Ansible Playbook?

Ansible playbooks are YAML files where we need to mention the tasks that you want Ansible to perform on the target machine.

Ansible Playbook

Why Ansible?

Ansible stands out as a preferred choice for many users due to its user-friendly nature, making it accessible without requiring extensive specialized knowledge. Its appeal lies in the ease with which users can interact using natural language, catering to a diverse audience that includes developers, researchers, businesses, and individuals seeking natural language processing capabilities.

Ansible offers a balance between simplicity and functionality, allowing users to express configurations in a straightforward manner. This ease of use is a significant advantage, particularly for those who may not have extensive experience with complex configuration management tools.

Unlike Puppet, which has limited abstraction and higher-level constructs, and Chef, which presents a steeper learning curve, Ansible’s approach empowers users to manage configurations efficiently without sacrificing simplicity or scalability. The broad accessibility and flexibility of Ansible make it an ideal choice for a wide range of users seeking an intuitive and effective solution for their automation and configuration management needs.

Ansible  Puppet Chef 
Easy to use without requiring much-specialized knowledge.Limited abstraction and higher-level constructs compared to configuration management tools. Learning Curve: The declarative nature and DSL can lead to a steeper learning curve, particularly for those not familiar with Ruby and Puppet’s concepts. 
Users can interact with the model using natural language. Managing state and idempotency can be challenging. 
Intended for a broad audience including developers, researchers, businesses, and individuals looking for natural language processing capabilities. Not as declarative as some higher-level tools, making complex configurations more difficult to express. 
Advantages of Ansible

How Ansible Works?

If you want to install software on multiple servers, you can use Ansible. We will see how you can install Ansible on the master and configure SSH access to the Ansible host. 

The first step that you have to do is you’ll have to install Ansible on the master. But you do not have to install Ansible on the slave, that’s because Ansible works in such a way that Ansible installation is just required on the master on the sleeves. It just requires Python to be installed, if Python is installed it can directly connect to that Ansible slave.

The second step is to configure SSH from the master to the slave. That is, you should be able to do an SSH to the slaves from the master without any key and without any password.

So, we’ll see how we can configure it in the third step. We’ll have to set up Ansible host and then we’ll have to test the connection.

So we’ll do this step by step:

Master Node (Control Node): 

Step 1- Launch the Control Node

Create an EC2 instance on AWS to serve as the Ansible control node (master). Ensure the instance has the necessary permissions and roles to interact with other AWS resources. 

Step 2- SSH into the Control Node

Connect to the control node using SSH. 

Step 3- Install Ansible

Install Ansible on the control node as described earlier.

Step 4- Create an Inventory File

Create an Ansible inventory file that lists the IP addresses or DNS names of the managed nodes (slaves). You can use /etc/ansible/hosts or create a custom inventory file. 

Step 5- Set Up SSH Key-Based Authentication

Ensure that your control node can SSH into the managed nodes without using a password. And use SSH key pairs for doing authentication. 

Step 6- Create Ansible Playbooks

Write Ansible playbooks that define the tasks and configurations you want to execute on the managed nodes. 

Step 7- Run Ansible Playbooks

Use the ansible-playbook command on the control node to execute your playbooks, targeting the managed nodes. For example: 

Managed Nodes (Slaves): 

Step 1- Launch the Managed Nodes

Create EC2 instances on AWS to serve as managed nodes (slaves). Ensure these instances are running the desired Linux distribution (e.g., Ubuntu, Amazon Linux, CentOS). 

Step 2- Set Up SSH Key-Based Authentication

On the managed nodes, configure SSH key-based authentication and ensure the public key is added to the ~/.ssh/authorized_keys file for the user that Ansible will use. 

Step 3- Install Python

Ansible requires Python to be installed on the managed nodes. Most Linux distributions come with Python pre-installed, but you may need to ensure it’s available. 

Step 4- Prepare Managed Nodes for Ansible

You might need to configure your managed nodes to accept SSH connections from the control node and open the necessary ports.

Step 5- Test Connectivity

Test SSH connectivity from the control node to the managed nodes using the SSH command. Ensure the key-based authentication is working. 

Step 6- Execute Playbooks

Once everything is set up, run Ansible playbooks from the control node, which will execute tasks on the managed nodes. 

Remember to replace your_playbook.yml and your_inventory_file with the actual playbook and inventory file names you’re using. Ensure that your Ansible playbooks are written to target the managed nodes properly. Also, always check the latest Ansible and AWS documentation for any updates or changes in the configuration process. 

Conclusion:

In this blog, we have demonstrated how to automate software installation and configuration using Ansible. Additionally, we’ve gained insights into the essence of Ansible and explored the distinctive features that set Ansible apart.