Author: | Michael DeHaan |
---|
The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.
parameter | required | default | choices | comments |
---|---|---|---|---|
chdir | no | cd into this directory before running the command (added in Ansible 0.6) | ||
creates | no | a filename, when it already exists, this step will not be run. | ||
executable | no | change the shell used to execute the command. Should be an absolute path to the executable. (added in Ansible 0.9) | ||
free_form | yes | The shell module takes a free form command to run, as a string. There's not an actual option named "free form". See the examples! | ||
removes | no | a filename, when it does not exist, this step will not be run. (added in Ansible 0.8) |
# Execute the command in remote shell; stdout goes to the specified
# file on the remote.
- shell: somescript.sh >> somelog.txt
# Change the working directory to somedir/ before executing the command.
- shell: somescript.sh >> somelog.txt chdir=somedir/
# You can also use the 'args' form to provide the options. This command
# will change the working directory to somedir/ and will only run when
# somedir/somelog.txt doesn't exist.
- shell: somescript.sh >> somelog.txt
args:
chdir: somedir/
creates: somelog.txt
Note
If you want to execute a command securely and predictably, it may be better to use the command module instead. Best practices when writing playbooks will follow the trend of using command unless shell is explicitly required. When running ad-hoc commands, use your best judgement.
Note
To sanitize any variables passed to the shell module, you should use “{{ var | quote }}” instead of just “{{ var }}” to make sure they don’t include evil things like semicolons.