Effortless Automation with Ansible: Control Servers, Save Costs, and Optimize Resources

It is recommended to read this blog before proceeding with the current one: to understand what is Ansible.

We will create the same architecture as shown in the image above.

Steps to create instances:

  1. Visit the AWS Console and launch an EC2 instance:

  1. We will create 2 Ubuntu instances and 1 Amazon Linux instance.

  2. First, we will create 2 Ubuntu instances. Follow the same steps as shown in the screenshot.

  1. Now that we have two Ubuntu instances, we will rename one to ansible-ubuntu.

  1. Now we will launch Amazon Linux al2 Instance:

You can observe that there are three instances running: one Master server and two additional servers.

  1. As DevOps Engineers, our tasks are focused on the Master Server. To proceed, click on the Master Server and connect using SSH.

Installation of Ansible:

Refer this documentation to install Ansible.

Perform the following steps on the server to install Ansible:

As we can see, Python is required for Ansible. Therefore, we will add a Python repository.

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

Now run below command to check the version for Ansible and configurations.

ansible --version

Now, let's focus on the most important file: the hosts file.

What is the Host File?

The Master server contains all the information about the other servers, which is known as Host Information. (Host refers to Servers)

How to Remember the File Location:

For any Linux configuration file located in /etc/, you can find the Ansible configuration file in /etc/ansible/. Here, you will see the hosts file.

To connect to Ansible servers, you need to access the file located in /etc/ansible, named hosts.

The Master server has Ansible installed on it. The Master server contains a hosts file, which should include information about our two servers: Ubuntu and Amazon Linux.

Ungrouped vs. Grouped Hosts in hosts file:

  1. Ungrouped Hosts:
    These are individual systems listed without any group. For example:
host2.example.com

They don’t belong to a specific category but can still be targeted by name.

  1. Grouped Hosts:
    Systems can be grouped under a name for easy management. For example:
[webservers]
web1.example.com
web2.example.com

Now, you can target all webservers at once instead of listing them individually.

Think of it as a way to organize your systems like categories in a to-do app some tasks stand alone, and others belong to specific lists.

Now, we will inform the hosts file that there is a group named Servers, which includes the IP addresses of our two servers.

Let's add this information to the file:

cd /etc/ansible/

sudo vim hosts

Navigate to AWS and copy the public IP addresses for the Ubuntu and Amazon Linux 2 (AL2) servers. Then, add these IP addresses to the following file:

#Inside hosts file we will add our two servers entry
[servers]
server1 ansible_host=3.252.146.73
server2 ansible_host=3.250.114.79
server1 ansible_user=ubuntu
server2 ansible_user=ec2-user

#Assign variables to these servers
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible.pem

Code Explanation: Documentation for Variables

  • server1, server2: These are names you can assign to the servers, like server1 and server2, or any other names you prefer.

  • [all]: This denotes all groups and servers. Here, we will assign variables to our servers.

  • [all:vars]: This is used to specify variables for all servers.

  • ansible_python_interpreter: This specifies the Python interpreter that will be used for all servers.

  • ansible_user=ec2-user: When accessing an Amazon Linux instance, the default username is ec2-user.

When connecting to an instance, the username is specified in the SSH command, as shown in the image below:

  • ansible_ssh_private_key_file: This key is used to connect to all the Ansible servers.

    • Our private key is stored locally, so we will transfer it to our server using the scp command.

    • First, create the directory /home/ubuntu/keys on the Master server to store the private_key.

    • Then, execute the following command on your local machine where the private key is located.

        scp -i ansible.pem ansible.pem ubuntu@ec2-3-255-223-69.eu-west-1.compute.amazonaws.com:/home/ubuntu/keys
      
    • Explanation of the scp command:

      scp -i private_key master_server_user@ip:master_server_key_folder_path

    • Now, add the path of this private key directory to our hosts file.

  • To verify all the inventories, it is important to run this command every time you make changes to the hosts file.

ansible-inventory --list

This command will check for any errors in our file. If everything is correct, it will display the output in a tree structure format.

  • Now if your system wants to connect to servers let’s try to check with ping command
ansible servers -m ping 
#-a = Ad-hoc, like df-h, date, free -h
#-m = module, its value is already set. It is used to ping pong between 2 servers as

You’ll receive o/p like this

Now, we can execute some Ad-hoc commands using Ansible:

ansible servers -a "free -h"
#-a = Ad-hoc like df-h, date, free -h

ansible servers -a "uptime"

Now, let's proceed to create our fourth instance:

Now, send the instance we created to the Master server from your local machine using the following command:

scp -i ansible.pem ansible-redhat.pem ubuntu@ec2-3-252-241-164.eu-west-1.compute.amazonaws.com:/home/ubuntu/keys

Add an entry for our Redhat server code in the /etc/ansible/hosts file.

[servers]
server1 ansible_host=3.252.93.226 ansible_user=ubuntu
server2 ansible_host=34.250.19.185 ansible_user=ec2-user
server3 ansible_host=54.170.40.204 ansible_user=ec2-user
server3 ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-redhat.pem

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible.pem

Verify all the inventories:

ansible-inventory --list

Now, you can attempt to ping server3 as well as all the other servers.

We have now successfully connected all three servers to our Master server using Ansible.

You will also receive the Ad-hoc execution time.


Power of Ansible:

The primary use of Ansible is to manage configurations. Let's explore how to do this:

Ubuntu

  • Let’s try to run nginx on server1 from Master server.
ansible server1 -a "sudo apt-get update"

ansible server1 -a "sudo apt-get install nginx"

Now, copy the public IP of server1 from AWS and enter it in your browser (http://public_ip) to view the results.

Now, without directly accessing server1 you can see that nginx has been installed


RedHat

To install the httpd service on RedHat and test it, follow these steps:

ansible server3 -a "sudo dnf install httpd -y"

Here, dnf is the package installer used in RedHat. To check the services on the RedHat server, you can use the following command:

ansible server3 -a "sudo service httpd status"

The service is not running, so we need to start it.

ansible server3 -a "sudo service httpd start"

You can verify that it is functioning correctly:

Amazon Linux: CentOS

Perform the same steps as for RedHat.

This is the power of Ansible. It allows us to install or run anything on all servers from the Master server.

Happy Learning :)

Chetan Mohod ✨

For more DevOps updates, you can follow me on LinkedIn.

10
Subscribe to my newsletter

Read articles from Chetan Mohanrao Mohod directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chetan Mohanrao Mohod
Chetan Mohanrao Mohod

DevOps Engineer focused on automating workflows, optimizing infrastructure, and building scalable efficient solutions.