Day 55: Understanding Ad-hoc commands in Ansible

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

  1. Choosing Hosts 🖥️

    • all: Select all hosts in your inventory.

    • Example: ansible all

    • Emoji: 🌐

  2. Selecting a Module 📦

    • -m command: Use the command module.

    • Example: -m command

    • Emoji: ⚙️

  3. Providing Arguments 📝

    • -a "uptime": Pass the argument to the module.

    • Example: -a "uptime"

    • Emoji: 🕒

  4. 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

  1. Choose Hosts 🖥️

    • 🌐
  2. Select Module 📦

    • ⚙️
  3. Provide Arguments 📝

    • 🕒 (or relevant emoji)
  4. 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

  1. Specify the Group of Hosts 🖥️

    • servers: Target the servers group in the inventory file.

    • Emoji: 🖥️🖥️🖥️

  2. Specify the Inventory File 📋

    • -i hosts.ini: Use hosts.ini as the inventory file.

    • Emoji: 📋

  3. Select the Module 📦

    • -m ping: Use the ping module to check connectivity.

    • Emoji: 🏓

  4. 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 the uptime command.

    Command

      bashCopy codeansible all -m command -a "uptime"
    

    Explanation with Emojis

    1. Specify Hosts 🖥️

      • all: Target all hosts in your inventory.

      • Emoji: 🌐

    2. Select Module 📦

      • -m command: Use the command module.

      • Emoji: ⚙️

    3. Provide Arguments 📝

      • -a "uptime": Pass the uptime command as an argument.

      • Emoji: 🕒

    4. 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.