# Ansible Ad-Hoc Commands

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.

**<mark>Key Features of Ad-Hoc Commands</mark>**

1. **Immediate Execution**: Ad-hoc commands let you run tasks immediately without creating a playbook file.
    
2. **Direct Commands**: They use Ansible's modules directly from the command line to manage your infrastructure.
    
3. **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:

#### **<mark>Step 1: Generate SSH Key Pairs on the Master Machine</mark>**

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/`).

```plaintext
ssh-keygen
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723829838122/b7beb1cb-3b27-497d-9133-e75ac3bc5401.png align="center")

#### **<mark>Step 2: Verify the SSH Key Pair</mark>**

To check the key pair, navigate to the SSH directory and list the contents.

```plaintext
cd ~/.ssh/
ls -lrt
```

The `id_rsa` file is the private key, and `id_`[`rsa.pub`](http://rsa.pub) is the public key.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723829952693/ef1ed789-0e38-491d-9b83-fe19ee9bfde4.png align="center")

#### **<mark>Step 3: Copy the Public Key to Slave Machines</mark>**

Next, copy the public key to each of the slave machines. This will allow passwordless SSH connections from the master machine.

```plaintext
cat id_rsa.pub
```

You’ll need to manually copy the content of the `id_`[`rsa.pub`](http://rsa.pub) file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723830001394/ffec1eee-2af7-4a16-8704-c521e46e015c.png align="center")

**<mark>Step 4: Paste the Public Key on Slave Machines</mark>**

For each slave machine, you’ll paste the public key into the `~/.ssh/authorized_keys` file.

1. **SSH into Slave Machine 1:**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723830432519/48b49c78-acf2-4041-a2ce-a8a83437441f.png align="center")

2. **SSH into Slave Machine 2:**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723830500402/54b9a132-58c7-4fc3-9bdc-c098a55f15b7.png align="center")

3. **SSH into Slave Machine 3:**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723830549274/e9d16b64-4b93-4d84-a289-9efb22ff565e.png align="center")

**<mark>Step 5: Create an Inventory File</mark>**

Ansible requires an inventory file to manage the list of hosts. Let’s create an inventory file and add the slave machines.

```plaintext
touch inventory.ini
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723829747061/d695cd4f-cc66-42e4-8032-d78620e44ce7.png align="left")

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

```plaintext
username@ipaddress
username@ipaddress
username@ipaddress
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723832476750/5d7142c0-13af-42d5-8c6a-8917260f35b1.png align="center")

#### **<mark>Step 6: Ping All Machines Using Ansible</mark>**

To verify connectivity to all slave machines, use the following Ansible ad hoc command:

```plaintext
ansible all -i inventory.ini -m ping
```

This command should return a `pong` from each machine, confirming that Ansible can communicate with them.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723832805832/f5809c6e-5188-4717-9888-bdf686979a61.png align="center")

**<mark>Step 7: Execute System Commands with Ansible</mark>**

You can now run various system commands across all slave machines:

* **Check System Information:**
    

```plaintext
ansible all -i inventory.ini -a "uname -a"
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723832956233/9d434b12-a577-485d-a5a1-0999550a5116.png align="center")

* **Check System Uptime:**
    

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723833031241/d9c11004-6d94-4dab-8d10-077acaf56d33.png align="center")

* **Check Disk Usage:**
    

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723833200763/73d62942-eded-4d2f-b10d-2f47fcd0d08f.png align="center")

**<mark>Step 8: Install Apache on All Machines</mark>**

To install Apache across all slave machines, use the following command:

```plaintext
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723833838281/ecc21b29-7201-4567-a9c6-6018f7e40b35.png align="center")

#### **<mark>Step 9: Start the Apache Service</mark>**

After installation, start the Apache service on all machines:

```plaintext
 ansible all -i inventory.ini -m service -a "name=apache2 state=started" -b
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723834049258/9b8c2dae-4f16-4bb6-a799-6e0590892854.png align="center")

#### **<mark>Step 10: Verify Apache Installation</mark>**

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**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723834190668/609a87b3-f728-4e3c-ae77-8877740ab8f3.png align="center")

**Slave Machine - 2**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723834221917/f2570229-af72-4857-933d-045340417e8f.png align="center")

**Slave Machine - 3**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723834245193/c01db897-039d-48d2-bce3-239eccf9c05c.png align="center")

#### **<mark>Step 11: Deploy a Custom HTML File</mark>**

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

```plaintext
vi index.html
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723834434314/b229d0b6-ac8f-430b-be6a-ddecdc011d64.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723834396203/bf207a1c-4175-4fa7-9a34-865a53b4b630.png align="center")

Then use Ansible to copy it to the Apache root directory on all slave machines:

```plaintext
ansible all -i inventory.ini -m copy -a "src=/home/ubuntu/index.html dest=/var/www/html/index.html" -b
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723835123826/7e691358-f685-4f67-8131-9aa0d08d1488.png align="center")

**<mark>Step 12: Verify the HTML Deployment</mark>**

Finally, check the IP addresses of all slave machines in a browser. The custom content from `index.html` should be displayed.

**Slave machine - 1**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723835264389/c39d74af-e8fb-4a6d-b4af-c4fe77c9a52e.png align="center")

**Slave machine - 2**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723835285952/8037dc6e-e3b1-4801-88da-f13c3f286b78.png align="center")

**Slave machine - 3**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723835309616/443baf0e-7f3f-4fd9-b8ea-f6d2256f3116.png align="center")
