Ansible Ad-Hoc Commands

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
Ansible ad-hoc commands are quick and simple commands used to perform tasks on managed nodes without the need to write a playbook. They are useful for tasks that don't require complex logic or need to be executed on the fly.
Key Features of Ad-Hoc Commands
Immediate Execution: Ad-hoc commands let you run tasks immediately without creating a playbook file.
Direct Commands: They use Ansible's modules directly from the command line to manage your infrastructure.
Simple Syntax: They have a straightforward syntax, making them easy to use for basic tasks.
Here’s a guide to help you get started with Ansible ad-hoc commands:
First, we need to generate SSH key pairs on the master machine, which will be used to securely connect to the slave machines.
This command will generate a pair of keys (private and public) in the default location (~/.ssh/).
ssh-keygen

To check the key pair, navigate to the SSH directory and list the contents.
cd ~/.ssh/
ls -lrt
The id_rsa file is the private key, and id_rsa.pub is the public key.

Next, copy the public key to each of the slave machines. This will allow passwordless SSH connections from the master machine.
cat id_rsa.pub
You’ll need to manually copy the content of the id_rsa.pub file.

Step 4: Paste the Public Key on Slave Machines
For each slave machine, you’ll paste the public key into the ~/.ssh/authorized_keys file.



Step 5: Create an Inventory File
Ansible requires an inventory file to manage the list of hosts. Let’s create an inventory file and add the slave machines.
touch inventory.ini

Edit the inventory.ini file to include the IP addresses and usernames of your slave machines:
username@ipaddress
username@ipaddress
username@ipaddress

To verify connectivity to all slave machines, use the following Ansible ad hoc command:
ansible all -i inventory.ini -m ping
This command should return a pong from each machine, confirming that Ansible can communicate with them.

Step 7: Execute System Commands with Ansible
You can now run various system commands across all slave machines:
ansible all -i inventory.ini -a "uname -a"

ansible all -i inventory.ini -a "uptime"

ansible all -i inventory.ini -a "df -h"

Step 8: Install Apache on All Machines
To install Apache across all slave machines, use the following command:
ansible all -i inventory.ini -m apt -a "name=apache2 state=present" -b
This command uses the apt module to install Apache and the -b flag to execute with sudo privileges.

After installation, start the Apache service on all machines:
ansible all -i inventory.ini -m service -a "name=apache2 state=started" -b

Open the IP address of each slave machine in a browser to ensure Apache is running. You should see the Apache default page.
Slave Machine - 1

Slave Machine - 2

Slave Machine - 3

Let’s create a simple index.html file on the master machine:
vi index.html

Add some content to this file, then use Ansible to copy it to the Apache root directory on all slave machines:

Then use Ansible to copy it to the Apache root directory on all slave machines:
ansible all -i inventory.ini -m copy -a "src=/home/ubuntu/index.html dest=/var/www/html/index.html" -b

Step 12: Verify the HTML Deployment
Finally, check the IP addresses of all slave machines in a browser. The custom content from index.html should be displayed.
Slave machine - 1

Slave machine - 2

Slave machine - 3
