Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Running CrewAI Ad-hoc Commands

Introduction

Ad-hoc commands in CrewAI allow you to execute simple tasks across your infrastructure without needing to write a playbook. These commands are useful for quick, one-off tasks. In this tutorial, we'll cover how to run ad-hoc commands, provide examples, and explain the output.

Prerequisites

Before you can run ad-hoc commands, you need to ensure the following:

  • You have CrewAI installed on your system.
  • You have SSH access to the target machines.
  • Your inventory file is properly set up.

Basic Syntax

The basic syntax for running an ad-hoc command is as follows:

crewai -i -m -a

Here:

  • <inventory>: Path to your inventory file.
  • <module>: The module you want to use (e.g., ping, shell).
  • <arguments>: Arguments for the module.
  • <hosts>: Target hosts (e.g., all, webservers).

Running a Ping Command

To check the connectivity to your hosts, you can use the ping module. This is a simple way to verify that your inventory is set up correctly and that you have access to the hosts.

crewai -i inventory.ini -m ping all

Output:

host1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
host2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
                

This output indicates that the command was successful and the hosts are reachable.

Running Shell Commands

You can also run shell commands on your remote hosts using the shell module. For example, to check the disk usage on all hosts, you can run:

crewai -i inventory.ini -m shell -a "df -h" all

Output:

host1 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G   15G  5G  75% /

host2 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   10G  40G  20% /
                

This output shows the disk usage on each host.

Using the Copy Module

The copy module allows you to copy files to your remote hosts. For example, to copy a file called example.txt to the /tmp directory on all hosts, you would run:

crewai -i inventory.ini -m copy -a "src=example.txt dest=/tmp/" all

Output:

host1 | SUCCESS => {
    "changed": true,
    "checksum": "6dcd4ce23d88e2ee9568ba546c007c63",
    "dest": "/tmp/example.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "5d41402abc4b2a76b9719d911017c592",
    "mode": "0644",
    "owner": "root",
    "size": 6,
    "src": "/root/.crewai/tmp/example.txt",
    "state": "file",
    "uid": 0
}
host2 | SUCCESS => {
    "changed": true,
    "checksum": "6dcd4ce23d88e2ee9568ba546c007c63",
    "dest": "/tmp/example.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "5d41402abc4b2a76b9719d911017c592",
    "mode": "0644",
    "owner": "root",
    "size": 6,
    "src": "/root/.crewai/tmp/example.txt",
    "state": "file",
    "uid": 0
}
                

This output indicates that the file has been successfully copied to the /tmp directory on each host.

Running Commands on Specific Hosts

Sometimes, you might want to run a command on a specific group of hosts instead of all hosts. For example, to only run the df -h command on the webservers group, you would use:

crewai -i inventory.ini -m shell -a "df -h" webservers

Output:

webserver1 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G   15G  5G  75% /

webserver2 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   10G  40G  20% /
                

This output shows the disk usage only for the hosts in the webservers group.

Conclusion

Ad-hoc commands in CrewAI provide a powerful way to perform quick tasks across your infrastructure. This tutorial covered the basics of running ad-hoc commands, including the syntax, common modules, and targeting specific hosts. With this knowledge, you can efficiently manage your systems and perform various administrative tasks.