🌐 Managing Multiple Servers with Ansible on a Master Node 🖥️

Ankit RawatAnkit Rawat
3 min read

Ansible is a powerful tool to manage and automate tasks across multiple servers from a single master node. It’s lightweight, simple, and doesn’t require any agent installation on the managed nodes. Let's dive into the basics of setting up and running Ansible! 🚀

🔧 Installation and Initial Setup

First, let’s install Ansible on your master node (Ubuntu):

sudo apt update
sudo apt install ansible

Next, we need to set up SSH keys to communicate securely with the servers. Navigate to the .ssh directory and paste your private key:

cd ~/.ssh/
vim ansible_key

Remember to change the permissions on the SSH directory and key:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/ansible_key

📝 Setting Up Your Inventory File

Create a dedicated directory for your Ansible files and set up your inventory file (hosts):

mkdir ~/ansible
cd ~/ansible/
nano hosts

Create new servers

Add your servers to the hosts file:

[servers] # All the server you want to manage
server1 ansible_host=3.109.185.251 
server2 ansible_host=13.126.195.24
server3 ansible_host=13.234.110.190

✅ Verify the Inventory File

To check if your inventory file is set up correctly, use:

ansible-inventory --list -y -i ~/ansible/hosts

🔗 Testing the Connection to All Servers

Now, let's verify connectivity to all the servers using the following command:

ansible all -m ping -i ~/ansible/hosts --private-key=~/.ssh/ansible_key

If the connection is successful, you’ll see pong from each server. 🎉

💻 Running Useful Commands on All Servers

You can run commands on all your servers from the master node in one go! Here are a few examples:

Check Free Memory:

ansible all -a "free -h" -i ~/ansible/hosts --private-key=~/.ssh/ansible_key

Check Disk Space:

ansible all -a "df -h" -i ~/ansible/hosts --private-key=~/.ssh/ansible_key

Check Uptime:

ansible all -a "uptime" -i ~/ansible/hosts --private-key=~/.ssh/ansible_key

What is Ansible playbook?

A play in Ansible is like a set of instructions that tells Ansible what to do on your servers. It’s part of a bigger file called a playbook, which is written in YAML format.

Think of it like this: if you have multiple tasks you want to run on your servers (like installing software, restarting services, or copying files), you can group them into a play. Each play targets specific servers and runs the tasks you’ve defined.

📋 Building and Running Playbooks

The next step is to build playbooks to automate tasks across all connected servers. A playbook is a YAML file where you can define a set of tasks to run on your servers. This will help you execute more complex automation, from installing software to deploying apps.

Stay tuned for the playbook creation guide in the next post! 🚀

0
Subscribe to my newsletter

Read articles from Ankit Rawat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankit Rawat
Ankit Rawat

Automating & Optimizing Cloud Infrastructure | Jenkins | CI/CD & Kubernetes | AWS | Student