|
86 | 86 | vars:
|
87 | 87 | - name: ansible_aws_ssm_bucket_endpoint_url
|
88 | 88 | version_added: 5.3.0
|
| 89 | + host_port_number: |
| 90 | + description: |
| 91 | + - The Port number of the server on the instance when using Port Forwarding Using AWS System Manager Session Manager |
| 92 | + to transfer files from/to local host to/from remote host. |
| 93 | + - The port V(80) is used if not provided. |
| 94 | + - This is not supported for Windows hosts for now. |
| 95 | + type: integer |
| 96 | + default: 80 |
| 97 | + vars: |
| 98 | + - name: ansible_aws_ssm_host_port_number |
| 99 | + version_added: 9.3.0 |
| 100 | + local_port_number: |
| 101 | + description: |
| 102 | + - Port number on local machine to forward traffic to when using Port Forwarding Using AWS System Manager Session Manager |
| 103 | + to transfer files from/to local host to/from remote host. |
| 104 | + - An open port is chosen at run-time if not provided. |
| 105 | + - This is not supported for Windows hosts for now. |
| 106 | + type: integer |
| 107 | + vars: |
| 108 | + - name: ansible_aws_ssm_local_port_number |
| 109 | + version_added: 9.3.0 |
89 | 110 | plugin:
|
90 | 111 | description:
|
91 | 112 | - This defines the location of the session-manager-plugin binary.
|
|
359 | 380 | from ansible.utils.display import Display
|
360 | 381 |
|
361 | 382 | from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3
|
| 383 | +from ansible_collections.community.aws.plugins.plugin_utils.ssm_file_transfer import PortForwardingFileTransferManager |
362 | 384 |
|
363 | 385 | display = Display()
|
364 | 386 |
|
@@ -469,6 +491,7 @@ class Connection(ConnectionBase):
|
469 | 491 | _stdout = None
|
470 | 492 | _session_id = ""
|
471 | 493 | _timeout = False
|
| 494 | + _filetransfer_mgr = None |
472 | 495 | MARK_LENGTH = 26
|
473 | 496 |
|
474 | 497 | def __init__(self, *args, **kwargs):
|
@@ -517,8 +540,29 @@ def _init_clients(self) -> None:
|
517 | 540 | # Initialize SSM client
|
518 | 541 | self._initialize_ssm_client(region_name, profile_name)
|
519 | 542 |
|
520 |
| - # Initialize S3 client |
521 |
| - self._initialize_s3_client(profile_name) |
| 543 | + if self._should_use_port_forwarding_for_file_transfer(): |
| 544 | + # Initialize S3 client |
| 545 | + self._initialize_s3_client(profile_name) |
| 546 | + else: |
| 547 | + self._initialize_file_transfer_manager() |
| 548 | + |
| 549 | + def _initialize_file_transfer_manager(self) -> None: |
| 550 | + ssm_timeout = self.get_option("ssm_timeout") |
| 551 | + region_name = self.get_option("region") |
| 552 | + profile_name = self.get_option("profile") or "" |
| 553 | + host_port = self.get_option("host_port_number") |
| 554 | + local_port = self.get_option("local_port_number") |
| 555 | + self._filetransfer_mgr = PortForwardingFileTransferManager( |
| 556 | + self.host, |
| 557 | + ssm_client=self._client, |
| 558 | + instance_id=self.instance_id, |
| 559 | + executable=self.get_executable(), |
| 560 | + ssm_timeout=ssm_timeout, |
| 561 | + region_name=region_name, |
| 562 | + profile_name=profile_name, |
| 563 | + host_port=host_port, |
| 564 | + local_port=local_port, |
| 565 | + ) |
522 | 566 |
|
523 | 567 | def _initialize_ssm_client(self, region_name: Optional[str], profile_name: str) -> None:
|
524 | 568 | """
|
@@ -621,6 +665,10 @@ def reset(self):
|
621 | 665 | self.close()
|
622 | 666 | return self.start_session()
|
623 | 667 |
|
| 668 | + def _should_use_port_forwarding_for_file_transfer(self) -> bool: |
| 669 | + """return true if the user has defined a bucket_name to be used for transport""" |
| 670 | + return (not self.is_windows and self.get_option("bucket_name") is not None) |
| 671 | + |
624 | 672 | @property
|
625 | 673 | def instance_id(self) -> str:
|
626 | 674 | if not self._instance_id:
|
@@ -1159,15 +1207,21 @@ def put_file(self, in_path, out_path):
|
1159 | 1207 | if not os.path.exists(to_bytes(in_path, errors="surrogate_or_strict")):
|
1160 | 1208 | raise AnsibleFileNotFound(f"file or module does not exist: {in_path}")
|
1161 | 1209 |
|
1162 |
| - return self._file_transport_command(in_path, out_path, "put") |
| 1210 | + if self._should_use_port_forwarding_for_file_transfer(): |
| 1211 | + return self._file_transport_command(in_path, out_path, "put") |
| 1212 | + else: |
| 1213 | + return self._filetransfer_mgr.put_file(in_path, out_path) |
1163 | 1214 |
|
1164 | 1215 | def fetch_file(self, in_path, out_path):
|
1165 | 1216 | """fetch a file from remote to local"""
|
1166 | 1217 |
|
1167 | 1218 | super().fetch_file(in_path, out_path)
|
1168 | 1219 |
|
1169 | 1220 | self._vvv(f"FETCH {in_path} TO {out_path}")
|
1170 |
| - return self._file_transport_command(in_path, out_path, "get") |
| 1221 | + if self._should_use_port_forwarding_for_file_transfer(): |
| 1222 | + return self._file_transport_command(in_path, out_path, "get") |
| 1223 | + else: |
| 1224 | + return self._filetransfer_mgr.fetch_file(in_path, out_path) |
1171 | 1225 |
|
1172 | 1226 | def close(self):
|
1173 | 1227 | """terminate the connection"""
|
|
0 commit comments