Run Shell Commands with command & shell
Execute arbitrary commands on remote nodes.
Run Shell Commands with command & shell is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🖥️
Frequently asked questions
Is the “Run Shell Commands with command & shell” lesson free?
Yes — the full text of “Run Shell Commands with command & shell” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Run Shell Commands with command & shell”?
Execute arbitrary commands on remote nodes. You practise Ansible Academy 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 Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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 Ansible Academy lesson?
Yes. Every Ansible Academy 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
- Anatomy of the ansible Command
- Patterns: Targeting all, web & host1
- Run Shell Commands with command & shell
- Privilege Escalation with become