# Day 56: Understanding Ad-hoc commands in Ansible  (Jan 21, 2024)

## **🙏 Introduction:**

In this blog, we'll explore ***Ansible*** Ad-hoc commands.

### **🔶What is Ad-hoc commands in Ansible**

* Ansible ad hoc commands are one-liners designed to achieve a very specific task they are like quick snippets and your compact swiss army knife when you want to do a quick task across multiple machines.
    
* To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic.
    
* Ansible ad hoc commands come handy when you want to perform a quick task.
    

# **🎯Task: 1**

1. ### **Write an ansible ad hoc ping command to ping 3 servers from inventory file**
    

* Create one master node server and three server nodes.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703863767342/8ab2f6df-83e7-454a-8ff5-3caa2d746bad.png?auto=compress,format&format=webp align="left")

* Edit the host file
    

```plaintext
[servers]
host_1 ansible_host=54.184.85.99
host_2 ansible_host=54.200.206.23
host_3 ansible_host=34.215.56.203

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703864006346/28ed41fb-407c-4038-b145-704a88a4a239.png?auto=compress,format&format=webp align="left")

* To verify the inventory of hosts
    

```plaintext
ansible-inventory --list
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703864340334/5b941778-d358-485d-b688-c585fab7d59e.png?auto=compress,format&format=webp align="left")

* ad hoc command to ping 3 servers from inventory file
    

```plaintext
ansible host_1:host_2:host_3 -m ping
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703864514345/01c54395-f13e-47dc-a887-ba5e0db8b23e.png?auto=compress,format&format=webp align="left")

1. ### **Write an ansible ad hoc command to check uptime**
    

```plaintext
ansible -a "uptime" servers
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703864668924/79ba2e48-b52d-4b64-92b2-46a58a8b8522.png?auto=compress,format&format=webp align="left")

For more Ansible ad-hoc commands we can refer to this [link](https://www.middlewareinventory.com/blog/ansible-ad-hoc-commands/)
