Ansible: Automate Everything from First Playbook to Production · Foundations: Installation, Inventory and Ad-hoc Commands
Ad-hoc Commands: ping, shell, copy, apt
Run one-off tasks with the `ansible` command: check connectivity, execute shell commands, copy files, install packages and manage services — no playbook required.
What you'll build
A set of ad-hoc commands that check host connectivity, gather facts, install a package, copy a configuration file and restart a service — the five operations you will use most when debugging or bootstrapping servers.
The idea
An ad-hoc command runs a single Ansible module against one or more hosts from the command line. The syntax is:
ansible <pattern> -m <module> -a "<arguments>" [options]
Ad-hoc commands are for quick, one-off tasks: "is every web server reachable?", "what kernel version is db1 running?", "copy this hotfix config to all load balancers right now." For repeatable work, write a playbook instead.
Common flags:
| Flag | Purpose |
|---|---|
-m <module> |
Module to run (default: command) |
-a "<args>" |
Module arguments |
--become / -b |
Run with sudo on the managed node |
--limit <pattern> |
Restrict to a subset of the inventory |
-f <N> |
Forks — number of parallel connections (default 5) |
-o |
One-line condensed output |
The most-used modules for ad-hoc work:
| Module | Purpose |
|---|---|
ping |
Verify Ansible can connect and Python works |
command |
Run a command (no shell features) |
shell |
Run a command through /bin/sh (supports pipes, redirects) |
copy |
Copy a local file to managed nodes |
apt / yum / dnf |
Install, update or remove packages |
service / systemd |
Start, stop or restart services |
file |
Create directories, set permissions, manage symlinks |
setup |
Gather facts (OS, IP, memory, disks) |
The code
mkdir -p ansible-lab && cd ansible-lab
cat > ansible.cfg << 'EOF'
[defaults]
inventory = inventory.ini
host_key_checking = False
stdout_callback = yaml
EOF
cat > inventory.ini << 'EOF'
[local]
localhost ansible_connection=local
EOF
ansible all -m ping
Output
Run this on your computer — see “Get Started” for setup.
cd ansible-lab
ansible localhost -m setup -a "filter=ansible_distribution*"
Output
Run this on your computer — see “Get Started” for setup.
cd ansible-lab
# Shell command: check disk usage
ansible localhost -m shell -a "df -h / | tail -1"
# Install a package (requires become/sudo)
ansible localhost -m apt -a "name=curl state=present" --become
# Copy a file
echo "server_name example.com;" > /tmp/vhost.conf
ansible localhost -m copy -a "src=/tmp/vhost.conf dest=/tmp/nginx-vhost.conf mode=0644"
# Create a directory
ansible localhost -m file -a "path=/tmp/app-logs state=directory mode=0755"
ls -la /tmp/nginx-vhost.conf /tmp/app-logs/
Output
Run this on your computer — see “Get Started” for setup.
command vs. shell: The command module runs the binary directly (no shell interpretation), so pipes, redirects and environment variable expansion don't work. Use shell when you need |, >, or $VAR. Prefer command when possible — it avoids shell-injection risks.
Common mistakes
- Using
shellwhencommandworks.shellpasses through/bin/sh, so a string like; rm -rf /in a variable would execute.commanddoes not interpret shell metacharacters, making it safer for untrusted input. - Forgetting
--become. Package installation and service management need root. Without--become, you get a permission-denied error that can look like a module bug. - Using ad-hoc commands for repeatable work. If you run the same
ansible … -m aptcommand every week, that belongs in a playbook. Ad-hoc commands leave no audit trail and can't be code-reviewed. - Running against
allby accident.ansible all -m shell -a "reboot"reboots every machine in the inventory. Use--limit webserversor--checkfirst.
Key points
- Ad-hoc commands run one module against selected hosts:
ansible <pattern> -m <module> -a "<args>". pingtests connectivity and Python availability, not ICMP.commandis safer thanshell— useshellonly when you need pipes or redirects.--becomeescalates to root;--limitrestricts the target hosts.setupgathers host facts; filter with-a "filter=ansible_distribution*".- For anything you will run more than once, write a playbook.
Further reading: Ad-hoc commands · Module index
Practice questions
What is the difference between the command module and the shell module?
Answer
The command module runs the binary directly without a shell, so pipes, redirects and variable expansion are not available. The shell module passes the command through /bin/sh, enabling those features but introducing shell-injection risk if arguments contain untrusted input.
What does the ping module actually test?
Answer
It tests that Ansible can connect to the managed node (via SSH), that Python is available on the remote host, and that the Ansible module system works. It does not send ICMP packets — it is an Ansible-level connectivity check, not a network ping.
Open in the app to mark lessons complete, take notes and track your progress.