What are the differences between the raw, command, and shell modules in Ansible?
sobes.tech AI
Answer from AI
In Ansible, the raw, command, and shell modules are used to execute commands on remote hosts, but they differ in execution method and capabilities:
-
raw — the lowest-level module that simply sends a command to the remote host and executes it without using Python or a shell. It is used when Python is not available on the target machine (e.g., minimal images). It does not process output and does not support many Ansible features.
-
command — executes a command on the remote host via SSH, but without using a shell. This means shell constructs like pipes, redirects, environment variables, etc., cannot be used. It is safer than shell because it does not invoke a shell.
-
shell — executes a command through a shell (usually /bin/sh), thus supporting all shell features: pipes, redirects, variables, conditionals, etc. It is used when complex commands requiring shell functionality need to be run.
Example:
- name: Run command without shell
command: ls -l /tmp
- name: Run command with shell
shell: cat /var/log/syslog | grep error
- name: Run command on host without Python
raw: uname -a
Thus, the choice of module depends on the presence of Python on the host and the need to use shell features.