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.

Members can save this page, take notes and track progress. See membership · Sign in

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

ping all hosts to check connectivity
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.

Gather facts from a host
cd ansible-lab
ansible localhost -m setup -a "filter=ansible_distribution*"

Output

Run this on your computer — see “Get Started” for setup.

Run a shell command and install a package
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 shell when command works. shell passes through /bin/sh, so a string like ; rm -rf / in a variable would execute. command does 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 apt command every week, that belongs in a playbook. Ad-hoc commands leave no audit trail and can't be code-reviewed.
  • Running against all by accident. ansible all -m shell -a "reboot" reboots every machine in the inventory. Use --limit webservers or --check first.

Key points

  • Ad-hoc commands run one module against selected hosts: ansible <pattern> -m <module> -a "<args>".
  • ping tests connectivity and Python availability, not ICMP.
  • command is safer than shell — use shell only when you need pipes or redirects.
  • --become escalates to root; --limit restricts the target hosts.
  • setup gathers 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.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake