| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2016, Ansible, inc |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 8 |
'status': ['preview'], |
| 9 |
'supported_by': 'core'} |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: win_shell |
| 14 |
short_description: Execute shell commands on target hosts |
| 15 |
version_added: 2.2 |
| 16 |
description: |
| 17 |
- The C(win_shell) module takes the command name followed by a list of space-delimited arguments. |
| 18 |
It is similar to the M(win_command) module, but runs |
| 19 |
the command via a shell (defaults to PowerShell) on the target host. |
| 20 |
- For non-Windows targets, use the M(shell) module instead. |
| 21 |
options: |
| 22 |
free_form: |
| 23 |
description: |
| 24 |
- The C(win_shell) module takes a free form command to run. |
| 25 |
- There is no parameter actually named 'free form'. See the examples! |
| 26 |
type: str |
| 27 |
required: yes |
| 28 |
creates: |
| 29 |
description: |
| 30 |
- A path or path filter pattern; when the referenced path exists on the target host, the task will be skipped. |
| 31 |
type: path |
| 32 |
removes: |
| 33 |
description: |
| 34 |
- A path or path filter pattern; when the referenced path B(does not) exist on the target host, the task will be skipped. |
| 35 |
type: path |
| 36 |
chdir: |
| 37 |
description: |
| 38 |
- Set the specified path as the current working directory before executing a command |
| 39 |
type: path |
| 40 |
executable: |
| 41 |
description: |
| 42 |
- Change the shell used to execute the command (eg, C(cmd)). |
| 43 |
- The target shell must accept a C(/c) parameter followed by the raw command line to be executed. |
| 44 |
type: path |
| 45 |
stdin: |
| 46 |
description: |
| 47 |
- Set the stdin of the command directly to the specified value. |
| 48 |
type: str |
| 49 |
version_added: '2.5' |
| 50 |
no_profile: |
| 51 |
description: |
| 52 |
- Do not load the user profile before running a command. This is only valid |
| 53 |
when using PowerShell as the executable. |
| 54 |
type: bool |
| 55 |
default: no |
| 56 |
version_added: '2.8' |
| 57 |
output_encoding_override: |
| 58 |
description: |
| 59 |
- This option overrides the encoding of stdout/stderr output. |
| 60 |
- You can use this option when you need to run a command which ignore the console's codepage. |
| 61 |
- You should only need to use this option in very rare circumstances. |
| 62 |
- This value can be any valid encoding C(Name) based on the output of C([System.Text.Encoding]::GetEncodings()). |
| 63 |
See U(https://docs.microsoft.com/dotnet/api/system.text.encoding.getencodings). |
| 64 |
type: str |
| 65 |
version_added: '2.10' |
| 66 |
notes: |
| 67 |
- If you want to run an executable securely and predictably, it may be |
| 68 |
better to use the M(win_command) module instead. Best practices when writing |
| 69 |
playbooks will follow the trend of using M(win_command) unless C(win_shell) is |
| 70 |
explicitly required. When running ad-hoc commands, use your best judgement. |
| 71 |
- WinRM will not return from a command execution until all child processes created have exited. |
| 72 |
Thus, it is not possible to use C(win_shell) to spawn long-running child or background processes. |
| 73 |
Consider creating a Windows service for managing background processes. |
| 74 |
seealso: |
| 75 |
- module: psexec |
| 76 |
- module: raw |
| 77 |
- module: script |
| 78 |
- module: shell |
| 79 |
- module: win_command |
| 80 |
- module: win_psexec |
| 81 |
author: |
| 82 |
- Matt Davis (@nitzmahone) |
| 83 |
""" |
| 84 |
|
| 85 |
EXAMPLES = r""" |
| 86 |
# Execute a command in the remote shell; stdout goes to the specified |
| 87 |
# file on the remote. |
| 88 |
- win_shell: C:\somescript.ps1 >> C:\somelog.txt |
| 89 |
|
| 90 |
# Change the working directory to somedir/ before executing the command. |
| 91 |
- win_shell: C:\somescript.ps1 >> C:\somelog.txt chdir=C:\somedir |
| 92 |
|
| 93 |
# You can also use the 'args' form to provide the options. This command |
| 94 |
# will change the working directory to somedir/ and will only run when |
| 95 |
# somedir/somelog.txt doesn't exist. |
| 96 |
- win_shell: C:\somescript.ps1 >> C:\somelog.txt |
| 97 |
args: |
| 98 |
chdir: C:\somedir |
| 99 |
creates: C:\somelog.txt |
| 100 |
|
| 101 |
# Run a command under a non-Powershell interpreter (cmd in this case) |
| 102 |
- win_shell: echo %HOMEDIR% |
| 103 |
args: |
| 104 |
executable: cmd |
| 105 |
register: homedir_out |
| 106 |
|
| 107 |
- name: Run multi-lined shell commands |
| 108 |
win_shell: | |
| 109 |
$value = Test-Path -Path C:\temp |
| 110 |
if ($value) { |
| 111 |
Remove-Item -Path C:\temp -Force |
| 112 |
} |
| 113 |
New-Item -Path C:\temp -ItemType Directory |
| 114 |
|
| 115 |
- name: Retrieve the input based on stdin |
| 116 |
win_shell: '$string = [Console]::In.ReadToEnd(); Write-Output $string.Trim()' |
| 117 |
args: |
| 118 |
stdin: Input message |
| 119 |
""" |
| 120 |
|
| 121 |
RETURN = r""" |
| 122 |
msg: |
| 123 |
description: Changed. |
| 124 |
returned: always |
| 125 |
type: bool |
| 126 |
sample: true |
| 127 |
start: |
| 128 |
description: The command execution start time. |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: '2016-02-25 09:18:26.429568' |
| 132 |
end: |
| 133 |
description: The command execution end time. |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
sample: '2016-02-25 09:18:26.755339' |
| 137 |
delta: |
| 138 |
description: The command execution delta time. |
| 139 |
returned: always |
| 140 |
type: str |
| 141 |
sample: '0:00:00.325771' |
| 142 |
stdout: |
| 143 |
description: The command standard output. |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: 'Clustering node rabbit@slave1 with rabbit@master ...' |
| 147 |
stderr: |
| 148 |
description: The command standard error. |
| 149 |
returned: always |
| 150 |
type: str |
| 151 |
sample: 'ls: cannot access foo: No such file or directory' |
| 152 |
cmd: |
| 153 |
description: The command executed by the task. |
| 154 |
returned: always |
| 155 |
type: str |
| 156 |
sample: 'rabbitmqctl join_cluster rabbit@master' |
| 157 |
rc: |
| 158 |
description: The command return code (0 means success). |
| 159 |
returned: always |
| 160 |
type: int |
| 161 |
sample: 0 |
| 162 |
stdout_lines: |
| 163 |
description: The command standard output split in lines. |
| 164 |
returned: always |
| 165 |
type: list |
| 166 |
sample: [u'Clustering node rabbit@slave1 with rabbit@master ...'] |
| 167 |
""" |