Loading...

Watch: Ansible Command vs Shell Modules: Key Differences Explained

Explore the differences between Ansible's command and shell modules. Learn when to use each, with practical examples and code Playbooknstrations.

Ready to advance your Ansible expertise? [Explore Linux Administrator](https://jooble.org/jobs-remote-linux-administrator) Job openings today!

What is the difference between command vs shell Ansible modules?

These two Ansible modules are confused one for another but they're fundamentally different.

Both modules allow you to execute command on a target host but in a slightly different way.

I'm Luca Berton and welcome to today's episode of Ansible Pilot.

command vs shell

command

  • execute commands against the target Unix-based hosts
  • it bypasses the shell
  • always set changed to True

shell

  • execute shell commands against the target Unix-based hosts
  • redirections and shell's inbuilt functionality
  • always set changed to True

The command and shell Ansible modules execute commands on the target node.

Generally speaking, is always better to use a specialized Ansible module to execute a task.

However, sometimes the only way is to execute a Linux command via command or shell module.

Let me reinforce again, you should avoid as much as possible the usage of command/shell instead of a better module.

Both modules execute commands on target nodes but in a sensible different way.

The command modules execute commands on the target machine without using the target shell, it simply executes the command. The target shell is for example the popular bash, zsh, or sh. As a side effect user environment, variable expansions, output redirections, stringing two commands together, and other shell features are not available. On the other side, every command executed using shell module has all shell features so it could be expanded in runtime. From the security point of viewcommand module is more robust and has a more predictable outcome because it bypasses the shell.

Both modules returned always changed status because Ansible is not able to predict if the execution has or has not altered the target system.

command module

  • Execute commands on targets

The "command" module is the default module in Ansible Ad-hoc mode. The command module is able to execute only the binaries on remote hosts. The command module won't be impacted by local shell variables because it bypasses the shell. At the same time, it may not be able to run "shell" built-in features and redirections.

shell module

  • Execute shell commands on targets

The shell Ansible module is potentially more dangerous than the command module and should only be used whe

Read the full tutorial: Ansible Command vs Shell Modules: Key Differences Explained