DevOps Bootcamp · Lesson

Run Shell Commands with command & shell

Execute arbitrary commands on remote nodes.

Lesson 3 of 413 steps

Run Shell Commands with command & shell is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

Run Real Commands Remotely

Sometimes you just need to run a raw command on your servers. Ansible gives you two modules for that: command and shell. 🖥️

The command Module

The command module runs a program directly on the remote host. It is the safe default for simple, single commands.

ansible all -m command -a "uptime"

command Skips the Shell

Because command runs without a shell, it ignores pipes, redirects and variables like $HOME. That keeps it predictable and safe.

ansible all -m command -a "ls /etc"

The shell Module

The shell module runs your command through a real shell on the host, so pipes, redirects and variables all work.

ansible all -m shell -a "cat /etc/*release"

Pipes Need shell

When your command uses a pipe or redirect, reach for shell. The command module would just treat | as a plain argument.

ansible web -m shell -a "ps aux | grep nginx"

Environment Variables in shell

Only the shell module expands variables. Use it when your command relies on something like $HOME or $PATH.

ansible all -m shell -a "echo $HOME"

Prefer command When You Can

Reach for command by default and use shell only when you truly need shell features. Less shell means fewer surprises.

Run in a Directory with chdir

Both modules accept chdir to change into a directory before running, which is cleaner than cd inside the command.

ansible app -m command -a "chdir=/srv git pull"

Guard Reruns with creates

The creates option skips the command if a given file already exists, a simple way to avoid repeating work.

ansible app -m command -a "touch /tmp/done creates=/tmp/done"

They Are Not Idempotent

Both modules report changed on every run, since Ansible cannot know what your raw command did. Prefer real modules when one exists.

Last Resort, Not First Choice

Treat shell and command as escape hatches. If a dedicated module like service or package fits, use it instead for safer results.

Quick Check

You need to run a command that uses a pipe. Which module should you choose?

Recap

Use command for plain commands and shell when you need pipes or variables. Remember, both always report changed. 🖥️

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Run Shell Commands with command & shell” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Run Shell Commands with command & shell”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Run Shell Commands with command & shell”?

Execute arbitrary commands on remote nodes. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Run Shell Commands with command & shell” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Anatomy of the ansible Command
  2. Patterns: Targeting all, web & host1
  3. Run Shell Commands with command & shell
  4. Privilege Escalation with become
← Back to DevOps Bootcamp