Repository: ansible-collections/community.aws

Issue:
Unable to use ansible_connection aws_ssm to configure Ubuntu 22.04 Jammy targets
### Summary

Using the aws_ssm connection to provision a Ubuntu 22.04 Jammy target machine fails presumably due to unexpected characters in the output that it receives (see examples below).

### Issue Type

Bug Report

### Component Name

aws_ssm

### Ansible Version

```console
❱ ~  ansible --version
/usr/local/Cellar/ansible/5.7.1/libexec/lib/python3.10/site-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
ansible [core 2.12.5]
  config file = None
  configured module search path = ['/Users/abent/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/5.7.1/libexec/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/abent/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.4 (main, Apr 26 2022, 19:42:59) [Clang 13.1.6 (clang-1316.0.21.2)]
  jinja version = 3.1.2
  libyaml = True

```


### Collection Versions

```console
❱ ~  ansible-galaxy collection list

# /Users/abent/.ansible/collections/ansible_collections
Collection    Version
------------- -------
amazon.aws    3.2.0
community.aws 3.2.1

# /usr/local/Cellar/ansible/5.7.1/libexec/lib/python3.10/site-packages/ansible_collections
Collection                    Version
----------------------------- -------
amazon.aws                    2.2.0
ansible.netcommon             2.6.1
ansible.posix                 1.3.0
ansible.utils                 2.6.0
ansible.windows               1.9.0
arista.eos                    3.1.0
awx.awx                       19.4.0
azure.azcollection            1.12.0
check_point.mgmt              2.3.0
chocolatey.chocolatey         1.2.0
cisco.aci                     2.2.0
cisco.asa                     2.1.0
cisco.intersight              1.0.18
cisco.ios                     2.8.1
cisco.iosxr                   2.9.0
cisco.ise                     1.2.1
cisco.meraki                  2.6.1
cisco.mso                     1.4.0
cisco.nso                     1.0.3
cisco.nxos                    2.9.1
cisco.ucs                     1.8.0
cloud.common                  2.1.1
cloudscale_ch.cloud           2.2.1
community.aws                 2.4.0
community.azure               1.1.0
community.ciscosmb            1.0.4
community.crypto              2.2.4
community.digitalocean        1.16.0
community.dns                 2.1.0
community.docker              2.4.0
community.fortios             1.0.0
community.general             4.8.0
community.google              1.0.0
community.grafana             1.4.0
community.hashi_vault         2.4.0
community.hrobot              1.3.0
community.kubernetes          2.0.1
community.kubevirt            1.0.0
community.libvirt             1.0.2
community.mongodb             1.3.3
community.mysql               2.3.5
community.network             3.1.0
community.okd                 2.1.0
community.postgresql          1.7.2
community.proxysql            1.3.2
community.rabbitmq            1.1.0
community.routeros            2.0.0
community.sap                 1.0.0
community.skydive             1.0.0
community.sops                1.2.1
community.vmware              1.18.0
community.windows             1.9.0
community.zabbix              1.6.0
containers.podman             1.9.3
cyberark.conjur               1.1.0
cyberark.pas                  1.0.13
dellemc.enterprise_sonic      1.1.0
dellemc.openmanage            4.4.0
dellemc.os10                  1.1.1
dellemc.os6                   1.0.7
dellemc.os9                   1.0.4
f5networks.f5_modules         1.16.0
fortinet.fortimanager         2.1.5
fortinet.fortios              2.1.4
frr.frr                       1.0.3
gluster.gluster               1.0.2
google.cloud                  1.0.2
hetzner.hcloud                1.6.0
hpe.nimble                    1.1.4
ibm.qradar                    1.0.3
infinidat.infinibox           1.3.3
infoblox.nios_modules         1.2.1
inspur.sm                     1.3.0
junipernetworks.junos         2.10.0
kubernetes.core               2.3.0
mellanox.onyx                 1.0.0
netapp.aws                    21.7.0
netapp.azure                  21.10.0
netapp.cloudmanager           21.16.0
netapp.elementsw              21.7.0
netapp.ontap                  21.18.1
netapp.storagegrid            21.10.0
netapp.um_info                21.8.0
netapp_eseries.santricity     1.3.0
netbox.netbox                 3.7.0
ngine_io.cloudstack           2.2.3
ngine_io.exoscale             1.0.0
ngine_io.vultr                1.1.1
openstack.cloud               1.8.0
openvswitch.openvswitch       2.1.0
ovirt.ovirt                   1.6.6
purestorage.flasharray        1.12.1
purestorage.flashblade        1.9.0
sensu.sensu_go                1.13.1
servicenow.servicenow         1.0.6
splunk.es                     1.0.2
t_systems_mms.icinga_director 1.29.0
theforeman.foreman            2.2.0
vyos.vyos                     2.8.0
wti.remote                    1.0.3
```


### AWS SDK versions

```console
❱ ~ pip show boto boto3 botocore
WARNING: Package(s) not found: boto
Name: boto3
Version: 1.23.0
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email:
License: Apache License 2.0
Location: /usr/local/lib/python3.10/site-packages
Requires: botocore, jmespath, s3transfer
Required-by:
---
Name: botocore
Version: 1.26.0
Summary: Low-level, data-driven core of boto 3.
Home-page: https://github.com/boto/botocore
Author: Amazon Web Services
Author-email:
License: Apache License 2.0
Location: /usr/local/lib/python3.10/site-packages
Requires: jmespath, python-dateutil, urllib3
Required-by: boto3, s3transfer
```


### Configuration

```console (paste below)
# Inventory (aws_ec2.yaml)
plugin: aws_ec2

aws_profile: qa
strict: False

regions:
  - us-east-2

# Example Automation (site.yaml)
- hosts: all
  become: true
  vars:
    ansible_connection: aws_ssm
    ansible_python_interpreter: /usr/bin/python3  #Added to overcome python detection failure
    aws_s3_region: us-east-2
    ansible_aws_ssm_profile: qa
    ansible_aws_ssm_region: us-east-2
    ansible_aws_ssm_bucket_name: <redacted bucket name>
    ansible_aws_ssm_instance_id: <redacted instance id>

  tasks:
    - name: Test command
      ansible.builtin.shell: mkdir /tmp/foo
```


### OS / Environment

Ubuntu 22.04 LTS

### Steps to Reproduce

1. Set SSM Linux shell profile to `/bin/bash` in order to work around incompatibility with `/bin/dash`, which exists for Ubuntu 20.04 LTS.

2. Run the configuration provided above
```yaml 
ansible-playbook -i aws_ec2.yml site.yml
```


### Expected Results

1. Python interpreter is detected
2. Empty folder `/tmp/foo` is created

### Actual Results

1. Python version fails to detect - verbose output from Gathering Facts, some IDs redacted with XXXX
```
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> ESTABLISH SSM CONNECTION TO: i-03bd947822XXXXXXX
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> SSM CONNECTION ID: XXX@XXX.XXX-03b08b5c019356307
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC echo ~
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r/home/ssm-user\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC ( umask 77 && mkdir -p "` echo /home/ssm-user/.ansible/tmp `"&& mkdir "` echo /home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323 `" && echo ansible-tmp-1653405370.894557-13636-81324261820323="` echo /home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323 `" )
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\ransible-tmp-1653405370.894557-13636-81324261820323=/home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> Attempting python interpreter discovery
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC echo PLATFORM; uname; echo FOUND; command -v 'python3.10'; command -v 'python3.9'; command -v 'python3.8'; command -v 'python3.7'; command -v 'python3.6'; command -v 'python3.5'; command -v '/usr/bin/python3'; command -v '/usr/libexec/platform-python'; command -v 'python2.7'; command -v 'python2.6'; command -v '/usr/bin/python'; command -v 'python'; echo ENDFOUND
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\rPLATFORM\r\r\nLinux\r\r\nFOUND\r\r\n/usr/bin/python3.10\r\r\n/usr/bin/python3\r\r\nENDFOUND\r\r', '')
[WARNING]: Unhandled error in Python interpreter discovery for host ec2-3-16-81-57.us-east-2.compute.amazonaws.com: unexpected output from Python interpreter discovery
Using module file /usr/local/Cellar/ansible/5.7.1/libexec/lib/python3.10/site-packages/ansible/modules/setup.py
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> PUT /Users/abent/.ansible/tmp/ansible-local-13610fw9y0i6k/tmpnfywx2vw TO /home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323/AnsiballZ_setup.py
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC curl 'https://XXXX.s3.amazonaws.com/i-03bd94782282fe77c//home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323/AnsiballZ_setup.py?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIASE6ULJM5CWCSSYVQ%2F20220524%2Fus-east-2%2Fs3%2Faws4_request&X-Amz-Date=20220524T151612Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEJD%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIQDH2GZyL9nZYpBxHBFH3%2FH%2BHEpHGIQhgtbktk%2FGcY0LPAIgJZw9%2BYrZ62Dok15Agfy40vFcfN8zLa%2B9xxhzoj8caMEqiAMIeBADGgwxNDgwODQ4MzcxNzgiDE2enjGjMDfRwEomgirlArjBOtuUi0t0haBKlfdyi0KK3NOaivLGdT9Q8fomo29gHNdR3dCXePMHXPb29D3mjpUYyfbgQg7PIUeyW2OexxFgDWzLm3Td%2BuHFDm%2B6WKM7x8OpRrjnGpdcIXYRBMCRSYlWZS%2Fjcd2AD5DYSo6V4nd9jlRG5th24bIJ48rUtpKX2Jn2HOwrwvJg9CC64sxHgKS%2BZoFcEo1fqUutY3P2aBoJzRGGl%2BrYDjUYew3PQLiqa15LXixtnS66Ppltg5G6Zrm9RFNxxPEccw5r2FdNZxR5CLqZFC7%2BXFLoCSMEQy5J2%2FAlRq2z7k0IL9f7YnSGZgcwCprrHvz3GQVv%2Bp439siS5cez2RMVfTOJTnNCQIDOi0YSFcRqYvuWdeIycNIsZGBEJTOckvXNUuI3ZLxBpjxyI6xTFF3%2FFvc%2BncDMjRUv8YhRaakyD%2FsycwggSjGyfNBTSalnJPGTDKc2kVVTrlbQR%2BIEmDC87bOUBjqmARSckHcf%2BQ27lMTsgWO%2BuxO%2BVjbzx3ZX8VO4RLkQmWvmaEROCOhSWw2vVxKh1IiVAbM7i9Fb%2Br1WF27YK1mfHiMIcZu1fXWYcGmI4nRPjCNSpse7vTi%2FeXEPqKCWyWkjTlpGHCPPrD%2BMWMdZptWECslkk%2BSyu%2BEoaDUVYy%2FCSzlXZi1uxMBGvKaMwldLsvYNYKCnWpWX2Q1M7oN5vbB8NzmYPYGoFyo%3D&X-Amz-Signature=4c0b039a9c2c1a87dbc44bee09bdb18e5a99da85b41593b18f0d2d9bc31ca13b' -o '/home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323/AnsiballZ_setup.py'
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\r\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100   509    0   509    0     0   4129      0 --:--:-- --:--:-- --:--:--  4138\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\r\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100   509    0   509    0     0   4129      0 --:--:-- --:--:-- --:--:--  4138\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC chmod u+x /home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323/ /home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323/AnsiballZ_setup.py
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r\x1b[?2004h\x1b[?2004l\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC sudo -H -S -n  -u root /bin/sh -c 'echo BECOME-SUCCESS-wmigxosgjbdymebxzjuthvcxpronrokr ; /usr/bin/python /home/ssm-user/.ansible/tmp/ansible-tmp-1653405370.894557-13636-81324261820323/AnsiballZ_setup.py'
```
2. If the python interpreter is manually specified, the `AnsiballZ_setup.py` script hangs waiting for output.  However, investigating on the target machine, the script did indeed complete running, and is able to run successfully.
```
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> ESTABLISH SSM CONNECTION TO: i-03bd947822XXXXXXX
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> SSM CONNECTION ID: XXX@XXX.XXX-03b08b5c019356307
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC echo ~
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r/home/ssm-user\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC ( umask 77 && mkdir -p "` echo /home/ssm-user/.ansible/tmp `"&& mkdir "` echo /home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497 `" && echo ansible-tmp-1653484883.342571-21201-234108973790497="` echo /home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497 `" )
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\ransible-tmp-1653484883.342571-21201-234108973790497=/home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497\r\r', '')
Using module file /usr/local/Cellar/ansible/5.7.1/libexec/lib/python3.10/site-packages/ansible/modules/setup.py
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> PUT /Users/abent/.ansible/tmp/ansible-local-2119271p8m00m/tmp1r3pqqr0 TO /home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497/AnsiballZ_setup.py
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC curl 'https://imt-ansible-aws-ssm-bucket-qa-us-east-2.s3.amazonaws.com/i-03bd947822XXXXXXX//home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497/AnsiballZ_setup.py?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIASE6ULJM5NTXR7R5U%2F20220525%2Fus-east-2%2Fs3%2Faws4_request&X-Amz-Date=20220525T132124Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEKb%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIQDI6v71%2FadgKOOSpRmoqr8D4YBeGNTeJJYOQNFOZ%2B5O1AIgN%2F4EhrywCxPmlZYQG81P5H4%2FHvLx7KWNQwosNMLREX8qkQMIjv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARADGgwxNDgwODQ4MzcxNzgiDJmqMfH5Rm%2BRBoa%2BWSrlAp3NBPulNa255uMjuq2tUJJeDY60qSRvK9%2BA5Zk0MiOsc2eOfezBPVddZu93aaujhy9yQ23sNAVQ%2BrO04IO%2Br2NKqu0WCJhMLtx3OyHLDdqqZy1%2BFuyRkz%2FkgJeDimgrTPQy99B2VTHo3Ax%2F0G%2BXUi8MuZLlKzwp5I2LKoGYp91PIXjnoIjv5zHyVU5nx8c6SjZ5vDFt1avxXqc6jgZeLip8aUn6GM7z1dXpjhKUgXeW8q7oawSWNmD93ko2ceDqn7VzWvw6Va%2Fpy4YdnD2IHCcP0cdVxD5Cx1D2PtQ51PjOsiE6Ltkk4w5Ua7mVxZ%2BOxu6okTjdx56vMj7fPp1FkRgikyxp8UCElNgCmjDd7SOfoFqunrX9HaP23QLC9dTvHtMpviBDsEZqiU%2BIGuxmrNUlvp2R%2FhKKy9VKXKCjzIsMWLUrXuTOiu5wslWkNHieFOtvvcc1iObj1sYmll26vVq5Uj9E3zDU2riUBjqmAYveqZhqsCSRUWCZ3PH3MB8%2BHSzGCLNZ4HE1Foeu9QdOmL6kYCyFAOkm9uevonuVlXbcVshNinKUtWk%2Fam1L2%2BaxpU7uAgYiVOp8Fa3y97I35zcz%2BNDhzIHlf0TF%2F7I%2Bs%2Bclr4HmjuHC8nn%2BnQ8MFGW5OvVtsxdVEacIvP2qSl9uN6DosqGHKvbjFKQnCA43aJnfcMYpTVWQrIsvcd%2FeepD3isr1TdA%3D&X-Amz-Signature=e518065e2f831f3ad4f785825507c029e1423ca11044da4178714f97496ca13e' -o '/home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497/AnsiballZ_setup.py'
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\r\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100  285k  100  285k    0     0  1885k      0 --:--:-- --:--:-- --:--:-- 1887k\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\r\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100  285k  100  285k    0     0  1885k      0 --:--:-- --:--:-- --:--:-- 1887k\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC chmod u+x /home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497/ /home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497/AnsiballZ_setup.py
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> (0, '\x1b[?2004h\x1b[?2004l\r\x1b[?2004h\x1b[?2004l\r\r', '')
<ec2-3-16-81-57.us-east-2.compute.amazonaws.com> EXEC sudo -H -S -n  -u root /bin/sh -c 'echo BECOME-SUCCESS-sfoqjiryaeqmfzpqvguespjphnvpenos ; /usr/bin/python3 /home/ssm-user/.ansible/tmp/ansible-tmp-1653484883.342571-21201-234108973790497/AnsiballZ_setup.py'
```

I suspect the issue has to do with the strange control characters in the output, but I am unclear as to the source of them.  The machine image is a minimal Ubuntu install, and if I switch back to a 20.04 image instead of 22.04, everything works properly.

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Hello,

I got the same problem, is someone can look to this problem.

Thanks
I'm running into the same issue. Some debugging points me to the following where it hangs waiting on the self._stdout.readline() on line 421 after receiving the command output:

https://github.com/ansible-collections/community.aws/blob/cb9716e14d44357aaadd2be733bbaa0dd8a522bc/plugins/connection/aws_ssm.py#L420-L422

It should run four commands which are wrapped together in the `_wrap_command` function:
```
echo FPJvOAbODNLrUBDNwlzdtqUVqn
sudo sudo -H -S -n  -u root /bin/sh -c 'echo BECOME-SUCCESS-mjvhieztmhqnhbcfiprxpidwfjxyvqzj ; /usr/bin/python3 /home/ssm-user/.ansible/tmp/ansible-tmp-1661540567.1878705-101559-269564787724829/AnsiballZ_ping.py'
echo $'\n'$?
echo iXsDFJCFrsWXYtdqfofbGhMbzm
```
Then it provides to output:
```
EXEC stdout line: FPJvOAbODNLrUBDNwlzdtqUVqn
EXEC stdout line: 
EXEC stdout line: {"ping": "pong", "invocation": {"module_args": {"data": "pong"}}}
```
What it should be:
```
EXEC stdout line: FPJvOAbODNLrUBDNwlzdtqUVqn
EXEC stdout line: 
EXEC stdout line: {"ping": "pong", "invocation": {"module_args": {"data": "pong"}}}
EXEC stdout line: 
EXEC stdout line: 0
EXEC stdout line: iXsDFJCFrsWXYtdqfofbGhMbzm
POST_PROCESS: 

{"ping": "pong", "invocation": {"module_args": {"data": "pong"}}}

0
```

I also have the same issue that I need to provide the Python interpreter, this might be linked to each other because the query result is working:
```
echo PLATFORM; uname; echo FOUND; command -v 'python3.10'; command -v 'python3.9'; command -v 'python3.8'; command -v 'python3.7'; command -v 'python3.6'; command -v 'python3.5'; command -v '/usr/bin/python3'; command -v '/usr/libexec/platform-python'; command -v 'python2.7'; command -v 'python2.6'; command -v '/usr/bin/python'; command -v 'python'; echo ENDFOUND


POST_PROCESS:
PLATFORM
Linux
FOUND
/usr/bin/python3.10
/usr/bin/python3
ENDFOUND
```

Unfortunately my Python knowledge isn't enough to further debug and find a solution.
Experiencing the exact same issue. Is there any workaround available?
Experiencing the same issue on amazonlinux

```<i-0f7a67efa9c5edae5> ESTABLISH SSM CONNECTION TO: i-0f7a67efa9c5edae5
<i-0f7a67efa9c5edae5> SSM CONNECTION ID: gl_gitlab_traefik-06221e159cef6ebda
<i-0f7a67efa9c5edae5> EXEC echo ~
<i-0f7a67efa9c5edae5> (0, '/home/ssm-user\r\r', '')
<i-0f7a67efa9c5edae5> EXEC ( umask 77 && mkdir -p "` echo /home/ssm-user/.ansible/tmp `"&& mkdir "` echo /home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205 `" && echo ansible-tmp-1
666267435.5919795-7743-117523018265205="` echo /home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205 `" )
<i-0f7a67efa9c5edae5> (0, 'ansible-tmp-1666267435.5919795-7743-117523018265205=/home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205\r\r', '')
Using module file /usr/lib/python3/dist-packages/ansible/modules/setup.py
<i-0f7a67efa9c5edae5> PUT /root/.ansible/tmp/ansible-local-774056h3q49o/tmpj0326k_f TO /home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/AnsiballZ_setup.py
<i-0f7a67efa9c5edae5> EXEC curl 'https://ansible-ssm-gitlab.s3.amazonaws.com/i-0f7a67efa9c5edae5//home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/AnsiballZ_setup.py?X-Amz-Algorit
hm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIATJO4GSHEUW7EIKGG%2F20221020%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date=20221020T120356Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=bca081bf63d6234
339fd966289a572af4a3eb059068f13174d83718674b68ec8' -o '/home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/AnsiballZ_setup.py'
<i-0f7a67efa9c5edae5> (0, '  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\r\n\r  0     0
 0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100   472    0   472    0     0    550      0 --:--:-- --:--:
-- --:--:--   550\r\r', '')
<i-0f7a67efa9c5edae5> (0, '  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\r\n\r  0     0
 0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100   472    0   472    0     0    550      0 --:--:-- --:--:
-- --:--:--   550\r\r', '')
<i-0f7a67efa9c5edae5> EXEC chmod u+x /home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/ /home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/AnsiballZ_s
etup.py
<i-0f7a67efa9c5edae5> (0, '\r', '')
<i-0f7a67efa9c5edae5> EXEC /bin/python3 /home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/AnsiballZ_setup.py
<i-0f7a67efa9c5edae5> (1, '  File "/home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/AnsiballZ_setup.py", line 1\r\r\n    <?xml version="1.0" encoding="UTF-8"?>\r\r\n    ^\r\r\nSy
ntaxError: invalid syntax\r\r', '')
<i-0f7a67efa9c5edae5> EXEC rm -f -r /home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/ > /dev/null 2>&1
<i-0f7a67efa9c5edae5> (0, '\r', '')
<i-0f7a67efa9c5edae5> CLOSING SSM CONNECTION TO: i-0f7a67efa9c5edae5
fatal: [my-instance]: FAILED! => {
    "ansible_facts": {},
    "changed": false,
    "failed_modules": {
        "ansible.legacy.setup": {
            "failed": true,
            "module_stderr": "",
            "module_stdout": "  File \"/home/ssm-user/.ansible/tmp/ansible-tmp-1666267435.5919795-7743-117523018265205/AnsiballZ_setup.py\", line 1\r\r\n    <?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\r\n
^\r\r\nSyntaxError: invalid syntax\r\r",
            "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
            "rc": 1
        }
    },
    "msg": "The following modules failed to execute: ansible.legacy.setup\n"
}
```
I am now on the following versions, and I no longer experience the problem:

```
Collection                    Version
----------------------------- -------
amazon.aws                    3.5.0
community.aws                 3.6.0
The issues seems to be on line 466 of aws_ssm.py.

https://github.com/ansible-collections/community.aws/blob/cb9716e14d44357aaadd2be733bbaa0dd8a522bc/plugins/connection/aws_ssm.py#L465-L467

In Dash (which is symlinked as /bin/sh), an `echo $'\n'$?`, returns

```
$
0
```

While Bash returns

```

0
```
Same problem here, did some debugging between an instance running Ubuntu 22.94 minimal (default shell is dash) and another one that is working and use Amazon Linux 2.

What I see is that on Ubuntu the command itself is parsed instead of it results, comparing the outputs

Amazon Linux 2 
```
<i-0846ef408bafd44b1> POST_PROCESS: > ansible-tmp-1668587955.2343445-102-113985281649023=/tmp/.ansible-ssm/ansible-tmp-1668587955.2343445-102-113985281649023

0
<i-0846ef408bafd44b1> (0, '> ansible-tmp-1668587955.2343445-102-113985281649023=/tmp/.ansible-ssm/ansible-tmp-1668587955.2343445-102-113985281649023\r\r', '')
```

Ubuntu 22.04
```
<i-0845d1adbe853d3e0> POST_PROCESS: ( umask 77 && mkdir -p "` echo /tmp/.ansible-ssm `"&& mkdir -p "` echo /tmp/.ansible-ssm/ansible-tmp-1668533193.4742794-568-272265113538326 `" && echo ansible-tmp-1668533193.4742794-568-272265113538326="` echo /tmp/.ansible-ssm/ansible-tmp-1668533193.4742794-568-272265113538326 `" )
echo $'\n'$?
<i-0845d1adbe853d3e0> ssm_retry: attempt: 0, caught exception(invalid literal for int() with base 10: "echo $'\\n'$?") from cmd (( umask 77 && mkdir -p "` echo /tmp/.ansible-ssm `"&& mkdir -p "` echo /tmp/.ansible-ssm/ansible-tmp-1668533193.4742794-568-272265113538326 `" && echo ansible-tmp-1668533193.4742794-568-272265113538326="` echo /tmp/.ansible-ssm/ansible-tmp-1668533193.4742794-568-272265113538326 `" )...), pausing for 0 seconds
```

I still do not know why, but writing the command all on a single line works, so if you want a work around apply this patch, I am going to open a PR too

```
index 3734d64c..a558f47c 100644
--- a/plugins/connection/aws_ssm.py
+++ b/plugins/connection/aws_ssm.py
@@ -463,7 +463,7 @@ class Connection(ConnectionBase):
         else:
             if sudoable:
                 cmd = "sudo " + cmd
-            cmd = "echo " + mark_start + "\n" + cmd + "\necho $'\\n'$?\n" + "echo " + mark_end + "\n"
+            cmd = " echo " + mark_start + "; " + cmd + "; echo $'\\n'$?; " + " echo " + mark_end + ";\n"

         display.vvvv(u"_wrap_command: '{0}'".format(to_text(cmd)), host=self.host)
         return cmd
```         

Resolving patch:
diff --git a/plugins/connection/aws_ssm.py b/plugins/connection/aws_ssm.py
--- a/plugins/connection/aws_ssm.py
+++ b/plugins/connection/aws_ssm.py
@@ -447,11 +447,94 @@ def exec_command(self, cmd, in_data=None, sudoable=True):
 
     def _prepare_terminal(self):
         ''' perform any one-time terminal settings '''
+        # No windows setup for now
+        if self.is_windows:
+            return
+
+        # *_complete variables are 3 valued:
+        #   - None: not started
+        #   - False: started
+        #   - True: complete
+
+        startup_complete = False
+        disable_echo_complete = None
+        disable_echo_cmd = to_bytes("stty -echo\n", errors="surrogate_or_strict")
+
+        disable_prompt_complete = None
+        end_mark = "".join(
+            [random.choice(string.ascii_letters) for i in xrange(self.MARK_LENGTH)]
+        )
+        disable_prompt_cmd = to_bytes(
+            "PS1='' ; printf '\\n%s\\n' '" + end_mark + "'\n",
+            errors="surrogate_or_strict",
+        )
+        disable_prompt_reply = re.compile(
+            r"\r\r\n" + re.escape(end_mark) + r"\r\r\n", re.MULTILINE
+        )
 
-        if not self.is_windows:
-            cmd = "stty -echo\n" + "PS1=''\n"
-            cmd = to_bytes(cmd, errors='surrogate_or_strict')
-            self._session.stdin.write(cmd)
+        stdout = ""
+        # Custom command execution for when we're waiting for startup
+        stop_time = int(round(time.time())) + self.get_option("ssm_timeout")
+        while (not disable_prompt_complete) and (self._session.poll() is None):
+            remaining = stop_time - int(round(time.time()))
+            if remaining < 1:
+                self._timeout = True
+                display.vvvv(
+                    "PRE timeout stdout: {0}".format(to_bytes(stdout)), host=self.host
+                )
+                raise AnsibleConnectionFailure(
+                    "SSM start_session timeout on host: %s" % self.instance_id
+                )
+            if self._poll_stdout.poll(1000):
+                stdout += to_text(self._stdout.read(1024))
+                display.vvvv(
+                    "PRE stdout line: {0}".format(to_bytes(stdout)), host=self.host
+                )
+            else:
+                display.vvvv("PRE remaining: {0}".format(remaining), host=self.host)
+
+            # wait til prompt is ready
+            if startup_complete is False:
+                match = str(stdout).find("Starting session with SessionId")
+                if match != -1:
+                    display.vvvv("PRE startup output received", host=self.host)
+                    startup_complete = True
+
+            # disable echo
+            if startup_complete and (disable_echo_complete is None):
+                display.vvvv(
+                    "PRE Disabling Echo: {0}".format(disable_echo_cmd), host=self.host
+                )
+                self._session.stdin.write(disable_echo_cmd)
+                disable_echo_complete = False
+
+            if disable_echo_complete is False:
+                match = str(stdout).find("stty -echo")
+                if match != -1:
+                    disable_echo_complete = True
+
+            # disable prompt
+            if disable_echo_complete and disable_prompt_complete is None:
+                display.vvvv(
+                    "PRE Disabling Prompt: {0}".format(disable_prompt_cmd),
+                    host=self.host,
+                )
+                self._session.stdin.write(disable_prompt_cmd)
+                disable_prompt_complete = False
+
+            if disable_prompt_complete is False:
+                match = disable_prompt_reply.search(stdout)
+                if match:
+                    stdout = stdout[match.end():]
+                    disable_prompt_complete = True
+
+        if not disable_prompt_complete:
+            raise AnsibleConnectionFailure(
+                "SSM process closed during _prepare_terminal on host: %s"
+                % self.instance_id
+            )
+        else:
+            display.vvv("PRE Terminal configured", host=self.host)
 
     def _wrap_command(self, cmd, sudoable, mark_start, mark_end):
         ''' wrap command so stdout and status can be extracted '''
@@ -463,7 +546,11 @@ def _wrap_command(self, cmd, sudoable, mark_start, mark_end):
         else:
             if sudoable:
                 cmd = "sudo " + cmd
-            cmd = "echo " + mark_start + "\n" + cmd + "\necho $'\\n'$?\n" + "echo " + mark_end + "\n"
+            cmd = (
+                f"printf '%s\\n' '{mark_start}';\n"
+                f"echo | {cmd};\n"
+                f"printf '\\n%s\\n%s\\n' \"$?\" '{mark_end}';\n"
+            )
 
         display.vvvv(u"_wrap_command: '{0}'".format(to_text(cmd)), host=self.host)
         return cmd


Test patch: