Table of contents
What are Ad-hoc commands?
Ad-hoc commands are quick, one-time commands that you run directly through the Ansible command line without needing to write a playbook. They are perfect for simple tasks and testing.
Running an Ad-hoc command
Here’s an example of running an Ad-hoc command to check the uptime of your servers:
bashCopy codeansible all -m command -a "uptime"
Explanation with Emojis
Choosing Hosts 🖥️
all
: Select all hosts in your inventory.Example:
ansible all
Emoji: 🌐
Selecting a Module 📦
-m command
: Use the command module.Example:
-m command
Emoji: ⚙️
Providing Arguments 📝
-a "uptime"
: Pass the argument to the module.Example:
-a "uptime"
Emoji: 🕒
Putting it all together 🧩
Command:
ansible all -m command -a "uptime"
Emoji Sequence: 🌐⚙️🕒
More Examples
Ping all hosts to check connectivity
bashCopy codeansible all -m ping
Explanation: Ping (check) all hosts to see if they are reachable.
Emoji: 🌐🏓
Install a package
bashCopy codeansible all -m yum -a "name=httpd state=present"
Explanation: Install
httpd
package on all hosts.Emoji: 🌐📦➡️🖥️
Copy a file
bashCopy codeansible all -m copy -a "src=/home/user/file.txt dest=/tmp/file.txt"
Explanation: Copy
file.txt
from source to destination on all hosts.Emoji: 🌐📄➡️📁
Summary with Emojis
Choose Hosts 🖥️
- 🌐
Select Module 📦
- ⚙️
Provide Arguments 📝
- 🕒 (or relevant emoji)
Run Command 🧩
- Combine all steps: 🌐⚙️🕒
Task-01
write an ansible ad hoc ping command to ping 3 servers from inventory file
Step 1: Inventory File
First, ensure you have an inventory file (let's call it hosts.ini
) that lists the three servers. Your hosts.ini
might look something like this:
iniCopy code[servers]
server1.example.com
server2.example.com
server3.example.com
Step 2: Ad-hoc Ping Command
Now, you can use the following Ansible Ad-hoc command to ping the servers listed in the inventory file:
bashCopy codeansible servers -i hosts.ini -m ping
Explanation with Emojis
Specify the Group of Hosts 🖥️
servers
: Target theservers
group in the inventory file.Emoji: 🖥️🖥️🖥️
Specify the Inventory File 📋
-i hosts.ini
: Usehosts.ini
as the inventory file.Emoji: 📋
Select the Module 📦
-m ping
: Use theping
module to check connectivity.Emoji: 🏓
Putting it all together 🧩
Command:
ansible servers -i hosts.ini -m ping
Emoji Sequence: 🖥️🖥️🖥️📋🏓
Full Command with Emojis
bashCopy codeansible servers -i hosts.ini -m ping
Choose Hosts: 🖥️🖥️🖥️
Specify Inventory File: 📋
Select Module: 🏓
Run Command: 🧩
Write an ansible ad hoc command to check uptime
Certainly! To check the uptime of your servers using an Ansible Ad-hoc command, you can use the
command
module with theuptime
command.Command
bashCopy codeansible all -m command -a "uptime"
Explanation with Emojis
Specify Hosts 🖥️
all
: Target all hosts in your inventory.Emoji: 🌐
Select Module 📦
-m command
: Use thecommand
module.Emoji: ⚙️
Provide Arguments 📝
-a "uptime"
: Pass theuptime
command as an argument.Emoji: 🕒
Putting it all together 🧩
Command:
ansible all -m command -a "uptime"
Emoji Sequence: 🌐⚙️🕒
Full Command with Emojis
bashCopy codeansible all -m command -a "uptime"
Choose Hosts: 🌐
Select Module: ⚙️
Provide Arguments: 🕒
Run Command: 🧩
This command will run the uptime
command on all the hosts in your inventory, displaying the current uptime for each host.