Skip to content

Commit

Permalink
Add powershell wrap and selective newline replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
psharkey committed Jan 6, 2020
1 parent 8edff26 commit 4649409
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/ansible/plugins/connection/aws_ssm.py
Expand Up @@ -165,6 +165,7 @@
from ansible.module_utils.six.moves import xrange
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.plugins.connection import ConnectionBase
from ansible.plugins.shell.powershell import _common_args
from ansible.utils.display import Display

display = Display()
Expand Down Expand Up @@ -391,7 +392,9 @@ def _wrap_command(self, cmd, sudoable, mark_start, mark_end):
''' wrap command so stdout and status can be extracted '''

if self.is_windows:
cmd = cmd + "; echo " + mark_start + " $? $LASTEXITCODE\necho " + mark_end + "\n"
if not cmd.startswith(" ".join(_common_args) + " -EncodedCommand"):
cmd = self._shell._encode_script(cmd, preserve_rc=True)
cmd = cmd + "; echo " + mark_start + " $LASTEXITCODE\necho " + mark_end + "\n"
else:
if sudoable:
cmd = "sudo " + cmd
Expand All @@ -408,16 +411,11 @@ def _post_process(self, stdout):
if stdout.splitlines()[-1].isdigit():
returncode = int(stdout.splitlines()[-1])
stdout = stdout[:stdout.rfind('\n') + 1]
stdout = stdout.replace('\r\n', '').replace('\n', '')

success = stdout.rfind('True')
fail = stdout.rfind('False')

if success > fail:
returncode = 0
stdout = stdout[:success]
elif fail > success:
stdout = stdout[:fail]
stdout = stdout.replace('\r\n', '')
if stdout.endswith('\n0'):
stdout = stdout[:-2]
if stdout.startswith('{'):
stdout = stdout.replace('\n', '')
else:
returncode = -51

Expand Down

0 comments on commit 4649409

Please sign in to comment.