
    Vho                        d Z dZddlZddlZddlZddlZddlZddlmZm	Z	 ddlm
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl'm)Z)  e       Z*dede+de
ee      fdZ,de-de.de-fdZ/de-de-fd Z0 G d! d"ee      Z1y)#a  
name: aws_ssm
author:
  - Pat Sharkey (@psharkey) <psharkey@cleo.com>
  - HanumanthaRao MVL (@hanumantharaomvl) <hanumanth@flux7.com>
  - Gaurav Ashtikar (@gau1991) <gaurav.ashtikar@flux7.com>

short_description: connect to EC2 instances via AWS Systems Manager
description:
  - This connection plugin allows Ansible to execute tasks on an EC2 instance via an AWS SSM Session.
notes:
  - The C(community.aws.aws_ssm) connection plugin does not support using the ``remote_user`` and
    ``ansible_user`` variables to configure the remote user.  The ``become_user`` parameter should
    be used to configure which user to run commands as.  Remote commands will often default to
    running as the ``ssm-agent`` user, however this will also depend on how SSM has been configured.
  - This plugin requires an S3 bucket to send files to/from the remote instance. This is required even for modules
    which do not explicitly send files (such as the C(shell) or C(command) modules), because Ansible sends over the C(.py) files of the module itself, via S3.
  - Files sent via S3 will be named in S3 with the EC2 host ID (e.g. C(i-123abc/)) as the prefix.
  - The files in S3 will be deleted by the end of the playbook run. If the play is terminated ungracefully, the files may remain in the bucket.
    If the bucket has versioning enabled, the files will remain in version history. If your tasks involve sending secrets to/from the remote instance
    (e.g. within a C(shell) command, or a SQL password in the C(community.postgresql.postgresql_query) module) then those passwords will be included in
    plaintext in those files in S3 indefinitely, visible to anyone with access to that bucket. Therefore it is recommended to use a bucket with versioning
    disabled/suspended.
  - The files in S3 will be deleted even if the C(keep_remote_files) setting is C(true).

requirements:
  - The remote EC2 instance must be running the AWS Systems Manager Agent (SSM Agent).
    U(https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started.html)
  - The control machine must have the AWS session manager plugin installed.
    U(https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html)
  - The remote EC2 Linux instance must have curl installed.
  - The remote EC2 Linux instance and the controller both need network connectivity to S3.
  - The remote instance does not require IAM credentials for S3. This module will generate a presigned URL for S3 from the controller,
    and then will pass that URL to the target over SSM, telling the target to download/upload from S3 with C(curl).
  - The controller requires IAM permissions to upload, download and delete files from the specified S3 bucket. This includes
    `s3:GetObject`, `s3:PutObject`, `s3:ListBucket`, `s3:DeleteObject` and `s3:GetBucketLocation`.

options:
  access_key_id:
    description: The STS access key to use when connecting via session-manager.
    vars:
    - name: ansible_aws_ssm_access_key_id
    env:
    - name: AWS_ACCESS_KEY_ID
    version_added: 1.3.0
  secret_access_key:
    description: The STS secret key to use when connecting via session-manager.
    vars:
    - name: ansible_aws_ssm_secret_access_key
    env:
    - name: AWS_SECRET_ACCESS_KEY
    version_added: 1.3.0
  session_token:
    description: The STS session token to use when connecting via session-manager.
    vars:
    - name: ansible_aws_ssm_session_token
    env:
    - name: AWS_SESSION_TOKEN
    version_added: 1.3.0
  instance_id:
    description: The EC2 instance ID.
    vars:
    - name: ansible_aws_ssm_instance_id
  region:
    description: The region the EC2 instance is located.
    vars:
    - name: ansible_aws_ssm_region
    env:
    - name: AWS_REGION
    - name: AWS_DEFAULT_REGION
    default: 'us-east-1'
  bucket_name:
    description: The name of the S3 bucket used for file transfers.
    vars:
    - name: ansible_aws_ssm_bucket_name
  bucket_endpoint_url:
    description: The S3 endpoint URL of the bucket used for file transfers.
    vars:
    - name: ansible_aws_ssm_bucket_endpoint_url
    version_added: 5.3.0
  plugin:
    description:
    - This defines the location of the session-manager-plugin binary.
    - Support for environment variable was added in version 9.1.0.
    - The plugin will first check the V('/usr/local/bin/session-manager-plugin') as the default path of the SSM plugin
      if this does not exist, it will find the session-manager-plugin from the PATH environment variable. Added in version 9.1.0.
    vars:
    - name: ansible_aws_ssm_plugin
    env:
    - name: AWS_SESSION_MANAGER_PLUGIN
  profile:
    description: Sets AWS profile to use.
    vars:
    - name: ansible_aws_ssm_profile
    env:
    - name: AWS_PROFILE
    version_added: 1.5.0
  reconnection_retries:
    description: Number of attempts to connect.
    default: 3
    type: integer
    vars:
    - name: ansible_aws_ssm_retries
  ssm_timeout:
    description: Connection timeout seconds.
    default: 60
    type: integer
    vars:
    - name: ansible_aws_ssm_timeout
  bucket_sse_mode:
    description: Server-side encryption mode to use for uploads on the S3 bucket used for file transfer.
    choices: [ 'AES256', 'aws:kms' ]
    required: false
    version_added: 2.2.0
    vars:
    - name: ansible_aws_ssm_bucket_sse_mode
  bucket_sse_kms_key_id:
    description: KMS key id to use when encrypting objects using C(bucket_sse_mode=aws:kms). Ignored otherwise.
    version_added: 2.2.0
    vars:
    - name: ansible_aws_ssm_bucket_sse_kms_key_id
  ssm_document:
    description:
    - SSM Session document to use when connecting.
    - To configure the remote_user (when C(become=False), it is possible to use an SSM Session
      document and define the C(runAsEnabled) and C(runAsDefaultUser) parameters.  See also
      U(https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-schema.html)
    vars:
    - name: ansible_aws_ssm_document
    version_added: 5.2.0
  s3_addressing_style:
    description:
    - The addressing style to use when using S3 URLs.
    - When the S3 bucket isn't in the same region as the Instance
      explicitly setting the addressing style to 'virtual' may be necessary
      U(https://repost.aws/knowledge-center/s3-http-307-response) as this forces
      the use of a specific endpoint.
    choices: [ 'path', 'virtual', 'auto' ]
    default: 'auto'
    version_added: 5.2.0
    vars:
    - name: ansible_aws_ssm_s3_addressing_style
a  
---
# Wait for SSM Agent to be available on the Instance
- name: Wait for connection to be available
  vars:
    ansible_connection: aws_ssm
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-west-2
    # When the S3 bucket isn't in the same region as the Instance
    # Explicitly setting the addressing style to 'virtual' may be necessary
    # https://repost.aws/knowledge-center/s3-http-307-response
    ansible_aws_ssm_s3_addressing_style: virtual
  tasks:
    - name: Wait for connection
      wait_for_connection:

# Stop Spooler Process on Windows Instances
- name: Stop Spooler Service on Windows Instances
  vars:
    ansible_connection: aws_ssm
    ansible_shell_type: powershell
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-east-1
  tasks:
    - name: Stop spooler service
      win_service:
        name: spooler
        state: stopped

# Install a Nginx Package on Linux Instance
- name: Install a Nginx Package
  vars:
    ansible_connection: aws_ssm
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-west-2
  tasks:
    - name: Install a Nginx Package
      yum:
        name: nginx
        state: present

# Create a directory in Windows Instances
- name: Create a directory in Windows Instance
  vars:
    ansible_connection: aws_ssm
    ansible_shell_type: powershell
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-east-1
  tasks:
    - name: Create a Directory
      win_file:
        path: C:\Windows\temp
        state: directory

---

# Making use of Dynamic Inventory Plugin
# =======================================
# # aws_ec2.yml (Dynamic Inventory - Linux)
# plugin: aws_ec2
# regions:
#   - us-east-1
# hostnames:
#   - instance-id
# # This will return the Instances with the tag "SSMTag" set to "ssmlinux"
# filters:
#   tag:SSMTag: ssmlinux
# -----------------------
- name: install aws-cli
  hosts: all
  gather_facts: false
  vars:
    ansible_connection: aws_ssm
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-east-1
  tasks:
    - name: aws-cli
      raw: yum install -y awscli
      tags: aws-cli

---

# Execution: ansible-playbook linux.yaml -i aws_ec2.yml
# =====================================================
# # aws_ec2.yml (Dynamic Inventory - Windows)
# plugin: aws_ec2
# regions:
#   - us-east-1
# hostnames:
#   - instance-id
# # This will return the Instances with the tag "SSMTag" set to "ssmwindows"
# filters:
#   tag:SSMTag: ssmwindows
# -----------------------
- name: Create a dir.
  hosts: all
  gather_facts: false
  vars:
    ansible_connection: aws_ssm
    ansible_shell_type: powershell
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-east-1
  tasks:
    - name: Create the directory
      win_file:
        path: C:\Temp\SSM_Testing5
        state: directory

---

# Execution:  ansible-playbook win_file.yaml -i aws_ec2.yml
# The playbook tasks will get executed on the instance ids returned from the dynamic inventory plugin using ssm connection.

# Install a Nginx Package on Linux Instance; with specific SSE CMK used for the file transfer
- name: Install a Nginx Package
  vars:
    ansible_connection: aws_ssm
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-west-2
    ansible_aws_ssm_bucket_sse_mode: 'aws:kms'
    ansible_aws_ssm_bucket_sse_kms_key_id: alias/kms-key-alias
  tasks:
    - name: Install a Nginx Package
      yum:
        name: nginx
        state: present

# Install a Nginx Package on Linux Instance; using the specified SSM document
- name: Install a Nginx Package
  vars:
    ansible_connection: aws_ssm
    ansible_aws_ssm_bucket_name: nameofthebucket
    ansible_aws_ssm_region: us-west-2
    ansible_aws_ssm_document: nameofthecustomdocument
  tasks:
    - name: Install a Nginx Package
      yum:
        name: nginx
        state: present

---
# Execution: ansible-playbook play.yaml -i ssm_inventory.ini
# =====================================================
# ssm_inventory.ini
# [all]
# linux ansible_aws_ssm_instance_id=i-01234567829abcdef ansible_aws_ssm_region=us-east-1

# [all:vars]
# ansible_connection=community.aws.aws_ssm
# ansible_python_interpreter=/usr/bin/python3
# local_tmp=/tmp/ansible-local-ssm-0123456
# ansible_aws_ssm_bucket_name=my-test-bucket
# ansible_aws_ssm_s3_addressing_style=virtual
# -----------------------
# Transfer file and run script on remote host
- name: Transfer file and Run script into SSM manage node
  hosts: all
  gather_facts: false

  tasks:
    - name: Create shell script
      ansible.builtin.copy:
        mode: '0755'
        dest: '/tmp/date.sh'
        content: |
          #!/usr/bin/env bash
          date

    - name: Execute script from remote host
      ansible.builtin.shell:
        cmd: '/tmp/date.sh'
    N)AnyDict)Iterator)List)Tuple)AnsibleError)AnsibleFileNotFound)to_bytes)to_text)missing_required_lib)get_bin_path)ConnectionBase)Display)	HAS_BOTO3)AwsConnectionPluginBase)S3ClientManager)TerminalManager)SSMSessionManager)FileTransferManager)	ssm_retry)CommandResultlstnreturnc              #   V   K   t        dt        |       |      D ]  }| |||z      yw)z)Yield successive n-sized chunks from lst.r   N)rangelen)r   r   is      l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/connection/aws_ssm.pychunksr    h  s2     1c#h" !AEls   ')line
is_windowsc                    t        |       } |rst        j                  d      }|j                  d|       } t        j                  d      }|j                  d|       } | j	                  dd      } t        |       dk(  r| dd } | S )	zRemove any ANSI terminal control codes.

    :param line: The input line.
    :param is_windows: Whether the output is coming from a Windows host.
    :returns: The result line.
    z\x1b\][^\x07]*\x07 z(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]z

   N)r   recompilesubreplacer   )r!   r"   
osc_filteransi_filters       r   filter_ansir.   n  s|     4=DZZ 56
~~b$'jj!BCr4( ||Hd+t99DK    pathc                 &    | j                  dd      S )z
    Converts a file path to a safe format by replacing backslashes with forward slashes.
    :param path: The file path to escape.
    :return: The escaped file path.
    \/)r+   )r0   s    r   escape_pathr4     s     <<c""r/   c                   T    e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZded	ed
df fdZed1d       Zed1d       Zed        Zej(                  d        Zed        Zed
efd       Zej(                  ded
dfd       Zd1dZd
efdZd1dZdeded
dfdZd1dZd
efdZd1dZdededed ed
e eeef   f
d!Z!e"d
efd"       Z#e$d2ded#e%d$e%d
e eeef   f fd%       Z&d&eded
e eef   fd'Z'd(ed)ed*ed
e eee(f   fd+Z)d(ed)ed,e*d
e+fd-Z,d(ed)ed
e+f fd.Z-d(ed)ed
e+f fd/Z.d1d0Z/ xZ0S )3
ConnectionzAWS SSM based connectionszcommunity.aws.aws_ssmr$   FTN   argskwargsr   c                    t        |   |i | t        st        t	        d            | j
                  j                  | _        d | _        t        |       | _
        | j                  d      | _        t        | j                  dd      dk(  r9d | _        d| _        d| _        d| _        d | _        d | _        d| _        d| _        y y )Nboto3reconnection_retriesSHELL_FAMILYr$   
powershellT)z.ps1z.exer$   )super__init__r   r   r   _play_contextremote_addrhost_instance_idr   terminal_manager
get_optionr<   getattr_shelldelegatehas_native_asyncalways_pipeline_modules!module_implementation_preferencesprotocolshell_id_shell_typer"   )selfr8   r9   	__class__s      r   r@   zConnection.__init__  s    $)&)3G<==&&22	  / 5$(OO4J$K!4;;3|C DM$(D!+/D(5ID2 DM DM+D"DO Dr/   c                 H    | j                   | j                   j                  S y N)_s3_managerclientrP   s    r   	s3_clientzConnection.s3_client  s#    '##***r/   c                    | j                   dd| j                  d      id}| j                  d      }t        j                  | j                  d      || j                  d      | j                  d      | j                  d	      | j                  d
      | j                  d            \  }}| j	                  d|||      }t        |      | _         | j                   S )Ns3v4addressing_styles3_addressing_stylesignature_versions3bucket_endpoint_urlbucket_nameaccess_key_idsecret_access_keysession_tokenregionprofile)r`   r_   ra   secret_key_idrc   region_nameprofile_namer^   )endpoint_urlrg   config)rT   rF   r   get_bucket_endpoint_get_boto_client)rP   rj   r_   s3_endpoint_urls3_region_namerW   s         r   
s3_managerzConnection.s3_manager  s    #+1:Ldoo^sNt9uvF"&//2G"H.=.Q.Q OOM:$7"ooo>"oo.AB"ooo> OOH5!__Y7/+O^ --?W] . I  /y9Dr/   c                     | j                   S rS   _session_managerrV   s    r   session_managerzConnection.session_manager  s    $$$r/   c                     || _         y rS   rq   )rP   values     r   rs   zConnection.session_manager  s
     %r/   c                     | j                   >dd| j                  d      id}| j                  d| j                  d      |      | _         | j                   S )NrY   rZ   r[   r\   ssmrd   )rg   rj   )_clientrF   rl   )rP   rj   s     r   
ssm_clientzConnection.ssm_client  sT    <<+1:Ldoo^sNt9uvF00DOOT\D]fl0mDL||r/   c                     | j                   s3| j                  d      | j                  n| j                  d      | _         | j                   S )Ninstance_id)rD   rF   rC   rV   s    r   r{   zConnection.instance_id  s@      -1__]-K-S		Y]YhYhivYwD   r/   r{   c                     || _         y rS   )rD   )rP   r{   s     r   r{   zConnection.instance_id  s
    'r/   c                 $    | j                          y rS   )closerV   s    r   __del__zConnection.__del__  s    

r/   c                     t        j                         | j                  _        | j                  s| j                          | S )zconnect to the host via ssm)getpassgetuserrA   remote_userrs   start_sessionrV   s    r   _connectzConnection._connect  s1    )0):&## r/   c           	         | j                  dd       | j                   | j                   t        | j	                  d      | j
                  | j                  | j                  | j                   | j                  | j                        | _
        y)z
        Initializes required AWS clients (SSM and S3).
        Delegates client initialization to specialized methods.
           zINITIALIZE BOTO3 CLIENTSr`   )r`   r{   rW   r<   verbosity_displayr~   exec_commandN)r   ro   ry   r   rF   r{   rW   r<   r~   r   file_transfer_managerrV   s    r   _init_clientszConnection._init_clients  st     	q"<= 	 	 &96((nn!%!:!:"44****&
"r/   levelmessagec                 &   | j                   rd| j                   i}ni }t        j                  t        j                  t        j                  t        j
                  d}||j                         vrt        d|        ||   t        |      fi | y)z
        Displays the given message depending on the verbosity level.

        :param message: The message to display.
        :param display_level: The verbosity level (1-4).

        :return: None
        rC   )         r   zInvalid verbosity level: N)	rC   displayvvvvvvvvvvkeysr   r   )rP   r   r   	host_argsverbosity_levels        r   r   zConnection.verbosity_display  sy     99+II%iiGJJ7;;7<<X,,..!:5'BCCww/=9=r/   c                 h    | j                  dd       | j                          | j                          y)zstart a fresh ssm sessionr   zreset called on ssm connectionN)r   r~   r   rV   s    r   resetzConnection.reset#  s(    q"BC

r/   c                 `   | j                  d      }|r;t        j                  j                  t	        |d            st        d| d      |S d}t        j                  j                  t	        |d            s	 t        d      }|S |S # t        $ r}t        t        |            d }~ww xY w)Npluginsurrogate_or_stricterrorsz(failed to find the executable specified .z%/usr/local/bin/session-manager-pluginzsession-manager-plugin)	rF   osr0   existsr
   r   r   
ValueErrorstr)rP   ssm_plugin_executablees      r   get_executablezConnection.get_executable)  s     $ 9 77>>(+@I^"_`"%MNcMdde#fgg %$ %L!77>>(+@I^"_`/,89Q,R) %$$$ " /&s1v../s   <B 	B-B((B-c                    | j                  dd| j                          | j                         }| j                          | j                  t        | j                  | j                  | j                   | j                  d            | _        | j                  j                  || j                  d      | j                  d      | j                  d      xs d	
       | j                  j                          yy)zstart ssm sessionr   zESTABLISH SSM CONNECTION TO: Nssm_timeout)r   r   ssm_documentrd   re   r$   )
executabledocument_namerg   rh   )r   r{   r   r   rs   r   ry   rF   r   rE   prepare_terminal)rP   r   s     r   r   zConnection.start_session9  s     	q$A$BRBRAS"TU((*
'#4  "&"8"8 OOM:	$D    ..%"oon= OOH5!__Y7=2	 /  !!224! (r/   cmd
mark_start
mark_beginmark_endc                 6   d}d}d}d}| j                   j                  d|      D ]  }	|	st        | j                   j                         | j                        }
| j                  dd|
        |s| j                  r||
z   }|}
||
v rd}|
j                  |      sd}x|s{||
v rS| j                  ddt        |              | j                  ||      \  }}| j                  dd	t        |               n||
z   } ||| j                   j                         fS )
a}  Interact with session.
        Read stdout between the markers until 'mark_end' is reached.

        :param cmd: The command being executed.
        :param mark_start: The marker which starts the output.
        :param mark_begin: The begin marker.
        :param mark_end: The end marker.
        :returns: A tuple with the return code, the stdout and the stderr content.
        r$   FNEXECr   zEXEC stdout line: 
TzPOST_PROCESS: 
zPOST_PROCESSED: 
)
rs   pollr.   stdout_readliner"   r   
startswithr   _post_processflush_stderr)rP   r   r   r   r   stdoutwin_linebegin
returncodepoll_resultr!   s              r   exec_communicatezConnection.exec_communicateT  s5    
//44VSA 	'Kt33CCEtWD""1(<TF&CDT__#d?T!z2Ft#**10@@Q.RS)-););FJ)O&J**10B76?BS.TU$-	'2 FD$8$8$E$E$GHHr/   c                      dj                  t        t        j                        D  cg c]%  } t	        j
                  t        j                        ' c}       }|S c c} w )zCGenerates a random string of characters to delimit SSM CLI commandsr$   )joinr   r6   MARK_LENGTHrandomchoicestringascii_letters)r   marks     r   generate_markzConnection.generate_mark~  sB     wwU:KaKaEbcf&:&:;cd ds   *Ain_datasudoablec                    t         |   |||       | j                  ddt        |              | j	                         }| j
                  r|dz   }n|}| j	                         }| j                  j                  |||      }| j                  j                          t        |d      D ](  }| j                  j                  t        |d             * | j                  ||||      S )zTWhen running a command on the SSM host, uses generate_mark to get delimiting stringsr   r   r   zEXEC: z $LASTEXITCODEi   r   r   )r?   r   r   r   r   r"   rE   wrap_commandrs   r   r    stdin_writer
   r   )	rP   r   r   r   r   r   r   chunkrQ   s	           r   r   zConnection.exec_command  s     	S'HEqF73<."9:'')
??#&66J#J%%' ##00j(K))+C& 	\E  ,,XeDY-Z[	\ $$S*j(KKr/   r   c                    | j                   sEt        |j                         d         }t        dd      D ]  }|d|j	                  d       } ||fS ||j	                  |      d }|j                         d   }|j
                  rt        |      }nd}|d|j	                  |       }t        j                  d      }|j                  d	|      }|j                  d
      r|j                  dd	      }||fS )z/extract command status and strip unwanted linesr   r   Nr%   r   r'   z#<\sCLIXML\s<Objs.*</Objs>r$   {)r"   int
splitlinesr   rfindisdigitr(   r)   r*   r   r+   )rP   r   r   r   _xtrailerlast_exit_codeclixml_filters           r   r   zConnection._post_process  s    V..045J Aqk 5 3d!345 '' j123 ++-a0!!^,JJ1j12 

#@A""2v. S!^^D"-FF##r/   in_pathout_path
ssm_actionc           
          t        | j                   d|       }d}g }| j                  j                  | j	                  d      | j	                  d      | j	                  d      |||| j
                  |      \  }}|||fS )aD  
        Generate S3 path and associated transport commands for file transfer.
        :param in_path: The local file path to transfer from.
        :param out_path: The remote file path to transfer to (used to build the S3 key).
        :param ssm_action: The SSM action to perform ("get" or "put").
        :return: A tuple containing:
            - s3_path (str): The S3 key used for the transfer.
            - commands (List[Dict]): A list of commands to be executed for the transfer.
            - put_args (Dict): Additional arguments needed for a 'put' operation.
        r3   r$   r`   bucket_sse_modebucket_sse_kms_key_id)r4   r{   ro   generate_host_commandsrF   r"   )rP   r   r   r   s3_pathcommandput_argss          r   generate_commandszConnection.generate_commands  s     !1!1 2!H:>? OOBBOOM*OO-.OO34OO	
 ))r/   r   c           
      t    | j                  |dd      \  }}}|dk7  rt        d| d| d| d|       |||fS )	a  
        Execute the provided transport command.

        :param in_path: The input path.
        :param out_path: The output path.
        :param command: A command to execute on the host.

        :returns: A tuple containing the return code, stdout, and stderr.
        NFr   r   zfailed to transfer file to  z:
r%   )r   r   )rP   r   r   r   r   r   stderrs          r   _exec_transport_commandsz#Connection._exec_transport_commands  sb     &*%6%6wW\%6%]"
FF?!<WIQxjPSTZS[[]^d]efgg66))r/   c                 2   t         |   ||       | j                  dd| d|        t        j                  j                  t        |d            st        d|       | j                  ||d      \  }}}| j                  j                  ||d|||      S )z$transfer a file from local to remoter   zPUT  TO r   r   zfile or module does not exist: put)r?   put_filer   r   r0   r   r
   r	   r   r   _file_transport_commandrP   r   r   r   r   r   rQ   s         r   r   zConnection.put_file  s     	(+qD	hZ"@Aww~~hw7LMN%(Gy&QRR%)%;%;GXu%U"())AA'8UZ\cemovwwr/   c                     t         |   ||       | j                  dd| d|        | j                  ||d      \  }}}| j                  j                  ||d|||      S )z!fetch a file from remote to localr   zFETCH r   get)r?   
fetch_filer   r   r   r   r   s         r   r   zConnection.fetch_file  sq     	7H-qF7)4z"BC%)%;%;GXu%U"())AA'8UZ\cemovwwr/   c                 `    | j                   "| j                   j                          d| _         yy)zterminate the connectionN)rs   	terminaterV   s    r   r~   zConnection.close  s-    +  **,#'D  ,r/   )r   N)NT)1__name__
__module____qualname____doc__	transportdefault_userallow_executableallow_extrashas_pipeliningr"   rx   rT   rr   r   r   r@   propertyrW   ro   rs   setterry   r   r{   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   boolr   r   r   r   dictr   r   r   r   r~   __classcell__)rQ   s   @r   r6   r6     s   #'ILLNJGKK#c #S #T #*  
    . % % & &   !S ! !
 (s (t ( (# 
2>s >S >T >(% % 56(IC (IS (Ic (IUX (I]bcfhkmpcp]q (IT 3  
 L Ld LT LUZ[^`ceh[hUi L L0 $C  $S  $U38_  $D* * * *QVWZ\_aeWeQf *4* *s *T *Vc *$
x 
xs 
x} 
xx# x x x(r/   r6   )2DOCUMENTATIONEXAMPLESr   r   r   r(   r   typingr   r   r   r   r   ansible.errorsr   r	   ansible.module_utils._textr
   r   ansible.module_utils.basicr   #ansible.module_utils.common.processr   ansible.plugins.connectionr   ansible.utils.displayr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ?ansible_collections.community.aws.plugins.plugin_utils.ssm.baser   Jansible_collections.community.aws.plugins.plugin_utils.ssm.s3clientmanagerr   Jansible_collections.community.aws.plugins.plugin_utils.ssm.terminalmanagerr   Iansible_collections.community.aws.plugins.plugin_utils.ssm.sessionmanagerr   Nansible_collections.community.aws.plugins.plugin_utils.ssm.filetransfermanagerr   Aansible_collections.community.aws.plugins.plugin_utils.ssm.commonr   r   r   r   r    r   r  r.   r4   r6    r/   r   <module>r     s   N`kX  	  	      ' . / . ; < 5 ) R c f f o W [ )  $s)!4 c t  .#c #c #w(!8 w(r/   