| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2018, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
# There is no actual shell module source, when you use 'shell' in ansible, |
| 7 |
# it runs the 'command' module with special arguments and it behaves differently. |
| 8 |
# See the command source and the comment "#USE_SHELL". |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: shell |
| 16 |
short_description: Execute shell commands on targets |
| 17 |
description: |
| 18 |
- The M(ansible.builtin.shell) module takes the command name followed by a list of space-delimited arguments. |
| 19 |
- Either a free form command or O(cmd) parameter is required, see the examples. |
| 20 |
- It is almost exactly like the M(ansible.builtin.command) module but runs |
| 21 |
the command through a shell (C(/bin/sh)) on the remote node. |
| 22 |
- For Windows targets, use the M(ansible.windows.win_shell) module instead. |
| 23 |
version_added: "0.2" |
| 24 |
options: |
| 25 |
free_form: |
| 26 |
description: |
| 27 |
- The shell module takes a free form command to run, as a string. |
| 28 |
- There is no actual parameter named 'free form'. |
| 29 |
- See the examples on how to use this module. |
| 30 |
type: str |
| 31 |
cmd: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- The command to run followed by optional arguments. |
| 35 |
creates: |
| 36 |
description: |
| 37 |
- A filename, when it already exists, this step will B(not) be run. |
| 38 |
type: path |
| 39 |
removes: |
| 40 |
description: |
| 41 |
- A filename, when it does not exist, this step will B(not) be run. |
| 42 |
type: path |
| 43 |
version_added: "0.8" |
| 44 |
chdir: |
| 45 |
description: |
| 46 |
- Change into this directory before running the command. |
| 47 |
type: path |
| 48 |
version_added: "0.6" |
| 49 |
executable: |
| 50 |
description: |
| 51 |
- Change the shell used to execute the command. |
| 52 |
- This expects an absolute path to the executable. |
| 53 |
type: path |
| 54 |
version_added: "0.9" |
| 55 |
stdin: |
| 56 |
description: |
| 57 |
- Set the stdin of the command directly to the specified value. |
| 58 |
type: str |
| 59 |
version_added: "2.4" |
| 60 |
stdin_add_newline: |
| 61 |
description: |
| 62 |
- Whether to append a newline to stdin data. |
| 63 |
type: bool |
| 64 |
default: yes |
| 65 |
version_added: "2.8" |
| 66 |
extends_documentation_fragment: |
| 67 |
- action_common_attributes |
| 68 |
- action_common_attributes.raw |
| 69 |
attributes: |
| 70 |
check_mode: |
| 71 |
details: while the command itself is arbitrary and cannot be subject to the check mode semantics it adds O(creates)/O(removes) options as a workaround |
| 72 |
support: partial |
| 73 |
diff_mode: |
| 74 |
support: none |
| 75 |
platform: |
| 76 |
support: full |
| 77 |
platforms: posix |
| 78 |
raw: |
| 79 |
support: full |
| 80 |
notes: |
| 81 |
- If you want to execute a command securely and predictably, it may be |
| 82 |
better to use the M(ansible.builtin.command) module instead. Best practices |
| 83 |
when writing playbooks will follow the trend of using M(ansible.builtin.command) |
| 84 |
unless the M(ansible.builtin.shell) module is explicitly required. When running ad-hoc |
| 85 |
commands, use your best judgement. |
| 86 |
- To sanitize any variables passed to the shell module, you should use |
| 87 |
C({{ var | quote }}) instead of just C({{ var }}) to make sure they |
| 88 |
do not include evil things like semicolons. |
| 89 |
- An alternative to using inline shell scripts with this module is to use |
| 90 |
the M(ansible.builtin.script) module possibly together with the M(ansible.builtin.template) module. |
| 91 |
- For rebooting systems, use the M(ansible.builtin.reboot) or M(ansible.windows.win_reboot) module. |
| 92 |
- If the command returns non UTF-8 data, it must be encoded to avoid issues. One option is to pipe |
| 93 |
the output through C(base64). |
| 94 |
seealso: |
| 95 |
- module: ansible.builtin.command |
| 96 |
- module: ansible.builtin.raw |
| 97 |
- module: ansible.builtin.script |
| 98 |
- module: ansible.windows.win_shell |
| 99 |
author: |
| 100 |
- Ansible Core Team |
| 101 |
- Michael DeHaan |
| 102 |
""" |
| 103 |
|
| 104 |
EXAMPLES = r""" |
| 105 |
- name: Execute the command in remote shell; stdout goes to the specified file on the remote |
| 106 |
ansible.builtin.shell: somescript.sh >> somelog.txt |
| 107 |
|
| 108 |
- name: Change the working directory to somedir/ before executing the command |
| 109 |
ansible.builtin.shell: somescript.sh >> somelog.txt |
| 110 |
args: |
| 111 |
chdir: somedir/ |
| 112 |
|
| 113 |
# You can also use the 'args' form to provide the options. |
| 114 |
- name: This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't exist |
| 115 |
ansible.builtin.shell: somescript.sh >> somelog.txt |
| 116 |
args: |
| 117 |
chdir: somedir/ |
| 118 |
creates: somelog.txt |
| 119 |
|
| 120 |
# You can also use the 'cmd' parameter instead of free form format. |
| 121 |
- name: This command will change the working directory to somedir/ |
| 122 |
ansible.builtin.shell: |
| 123 |
cmd: ls -l | grep log |
| 124 |
chdir: somedir/ |
| 125 |
|
| 126 |
- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does) |
| 127 |
ansible.builtin.shell: cat < /tmp/*txt |
| 128 |
args: |
| 129 |
executable: /bin/bash |
| 130 |
|
| 131 |
- name: Run a command using a templated variable (always use quote filter to avoid injection) |
| 132 |
ansible.builtin.shell: cat {{ myfile|quote }} |
| 133 |
|
| 134 |
# You can use shell to run other executables to perform actions inline |
| 135 |
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC |
| 136 |
ansible.builtin.shell: | |
| 137 |
set timeout 300 |
| 138 |
spawn ssh admin@{{ cimc_host }} |
| 139 |
|
| 140 |
expect "password:" |
| 141 |
send "{{ cimc_password }}\n" |
| 142 |
|
| 143 |
expect "\n{{ cimc_name }}" |
| 144 |
send "connect host\n" |
| 145 |
|
| 146 |
expect "pxeboot.n12" |
| 147 |
send "\n" |
| 148 |
|
| 149 |
exit 0 |
| 150 |
args: |
| 151 |
executable: /usr/bin/expect |
| 152 |
delegate_to: localhost |
| 153 |
""" |
| 154 |
|
| 155 |
RETURN = r""" |
| 156 |
msg: |
| 157 |
description: changed |
| 158 |
returned: always |
| 159 |
type: bool |
| 160 |
sample: True |
| 161 |
start: |
| 162 |
description: The command execution start time. |
| 163 |
returned: always |
| 164 |
type: str |
| 165 |
sample: '2016-02-25 09:18:26.429568' |
| 166 |
end: |
| 167 |
description: The command execution end time. |
| 168 |
returned: always |
| 169 |
type: str |
| 170 |
sample: '2016-02-25 09:18:26.755339' |
| 171 |
delta: |
| 172 |
description: The command execution delta time. |
| 173 |
returned: always |
| 174 |
type: str |
| 175 |
sample: '0:00:00.325771' |
| 176 |
stdout: |
| 177 |
description: The command standard output. |
| 178 |
returned: always |
| 179 |
type: str |
| 180 |
sample: 'Clustering node rabbit@slave1 with rabbit@master …' |
| 181 |
stderr: |
| 182 |
description: The command standard error. |
| 183 |
returned: always |
| 184 |
type: str |
| 185 |
sample: 'ls: cannot access foo: No such file or directory' |
| 186 |
cmd: |
| 187 |
description: The command executed by the task. |
| 188 |
returned: always |
| 189 |
type: str |
| 190 |
sample: 'rabbitmqctl join_cluster rabbit@master' |
| 191 |
rc: |
| 192 |
description: The command return code (0 means success). |
| 193 |
returned: always |
| 194 |
type: int |
| 195 |
sample: 0 |
| 196 |
stdout_lines: |
| 197 |
description: The command standard output split in lines. |
| 198 |
returned: always |
| 199 |
type: list |
| 200 |
sample: [u'Clustering node rabbit@slave1 with rabbit@master …'] |
| 201 |
stderr_lines: |
| 202 |
description: The command standard error split in lines. |
| 203 |
returned: always |
| 204 |
type: list |
| 205 |
sample: [u'ls cannot access foo: No such file or directory', u'ls …'] |
| 206 |
""" |