Day 55: Understanding Ad-hoc commands in Ansible
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.
Subscribe to my newsletter
Read articles from Rahul sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by