Day 55: Understanding Ad-hoc commands in Ansible

Rahul sharmaRahul sharma
3 min read

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.

0
Subscribe to my newsletter

Read articles from Rahul sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul sharma
Rahul sharma