🔧 Understanding Ansible: The Magic of -a, -m, and Inventories
Raees Qazi | DevOps Engineer | Learner | Mentor | Creator
Hello everyone! 👋
Today I’m going to explain two very important options in Ansible — -a and -m.
If you’re new to Ansible, don’t worry! I’ll keep things simple and easy to follow. 😊
🧠 What is -a in Ansible?
In Ansible, -a is used for ad-hoc commands.
It means you can directly run Linux commands on your remote servers using Ansible — no need to log in one by one.
For example:
ansible -a "ping google.com" serversExplanation:
This command will run ping google.com on all the servers in your servers group and show you the result.
Basically, -a lets you execute shell commands remotely — that’s the beauty of ad-hoc commands.
⚙️ What is -m in Ansible?
Now, -m is used for modules — the built-in tools that come with Ansible.
Modules help you perform tasks like installing packages, copying files, managing services, etc.
Example:
ansible -m ping serversExplanation:
Here, ping is not the Linux ping command — it’s an Ansible built-in module that checks whether your Ansible controller can connect to the remote machines successfully.
You can check all available modules on Ansible’s official documentation:
🔗 https://docs.ansible.com/
🗂️ Understanding Inventories in Ansible
An inventory in Ansible is like a contact list of all your servers.
It contains details such as:
- Server names
- IP addresses
- SSH users
- Python interpreter paths
…and more.
You can see your current inventory with:
ansible-inventory --listYou can also create custom inventories to manage different environments (like dev, staging, or production).
🚀 Starting Your Ansible Project
Let’s start a simple Ansible setup project step by step.
Step 1: Create a Master (Ansible) Machine
First, launch an Ubuntu instance on AWS EC2.
Then run the following commands:
sudo apt update
sudo apt upgradeNow install Ansible:
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -yVerify the installation:
ansible --versionStep 2: Create Two More Machines (Managed Nodes)
You’ll connect these two nodes with your Ansible master via SSH.
Generate SSH keys on the master machine:
ssh-keygenCopy the public key from the master and paste it into the authorized_keys file of both managed nodes.
This allows Ansible to connect without a password.
Step 3: Configure the Hosts File
Open the Ansible hosts file:
vim /etc/ansible/hostsAdd the following details:
[servers]
server1 ansible_host=<public_ip_1>
server2 ansible_host=<public_ip_2>[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/path/to/private/key
Save and exit the file (:wq).
Step 4: Test the Connection
Now let’s check if Ansible can communicate with the servers.
ansible -m ping servers✅ If everything is correct, you’ll get a pong response from each machine.
You can also run ad-hoc commands like:
ansible -a "free -h" servers(Shows RAM usage of all servers.)
Or update all servers at once:
ansible -a "sudo apt-get update" serversImagine how powerful this is — you can manage 2 servers or even 50 at the same time! 💪
That’s the real magic of Ansible.
🧩 Working with Custom Inventories
Let’s create separate inventories for dev and prod environments.
mkdir inventories
cd inventoriesdev inventory
vim devAdd:
[servers]
server1 ansible_host=<public_ip_1>[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/path/to/private/key
prod inventory
vim prdAdd:
[servers]
server2 ansible_host=<public_ip_2>[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/path/to/private/key
Now check each inventory individually:
ansible-inventory -i dev --list
ansible-inventory -i prd --listOr check all inventories together:
ansible-inventory --list📝 Important Notes
/etc/ansible/hostsis a read-only file, so it’s better to create your own custom inventory files.- Ansible works on Python, so make sure Python is installed on your servers.
- Always use SSH keys for secure communication between Ansible and managed nodes.
🎯 Conclusion
So today we learned:
- The difference between
-a(ad-hoc command) and-m(module). - The purpose of Ansible inventories.
- How to install and configure Ansible.
- How to connect multiple machines and run commands in one go.
Ansible saves time, reduces effort, and makes system administration fun and efficient!
Hope you enjoyed this blog and found it easy to understand. 🚀
Comments
Post a Comment