| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
import os |
| 9 |
|
| 10 |
from ansible.errors import AnsibleActionFail |
| 11 |
from ansible.plugins.action import ActionBase |
| 12 |
from ansible.utils.display import Display |
| 13 |
from ansible_collections.azure.azcollection.plugins.plugin_utils import (file_utils, ssh_info, connectivity_utils) |
| 14 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 15 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMAuth |
| 16 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AZURE_COMMON_ARGS |
| 17 |
try: |
| 18 |
from azure.core.exceptions import ClientAuthenticationError, ResourceExistsError, ResourceNotFoundError |
| 19 |
except ImportError: |
| 20 |
pass |
| 21 |
|
| 22 |
|
| 23 |
display = Display() |
| 24 |
|
| 25 |
|
| 26 |
class ActionModule(ActionBase): |
| 27 |
''' Configures ssh proxy for connecting to ARC hosts ''' |
| 28 |
|
| 29 |
TRANSFERS_FILES = False |
| 30 |
BYPASS_HOST_LOOP = True |
| 31 |
|
| 32 |
def __init__(self, *args, **kwargs): |
| 33 |
if not connectivity_utils.HAS_ORAS: |
| 34 |
raise AnsibleActionFail("oras.client is not installed.", orig_exc=connectivity_utils.HAS_ORAS_EXC) |
| 35 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 36 |
|
| 37 |
def run(self, tmp=None, task_vars=None): |
| 38 |
''' run the pause action module ''' |
| 39 |
if task_vars is None: |
| 40 |
task_vars = dict() |
| 41 |
|
| 42 |
result = super(ActionModule, self).run(tmp, task_vars) |
| 43 |
del tmp |
| 44 |
|
| 45 |
merged_arg_spec = dict() |
| 46 |
merged_arg_spec.update(AZURE_COMMON_ARGS) |
| 47 |
merged_arg_spec.update( |
| 48 |
{ |
| 49 |
'inventory_hostname': {'type': 'str'}, |
| 50 |
'ansible_host': {'type': 'str'}, |
| 51 |
'resource_group': {'type': 'str'}, |
| 52 |
'resource_type': {'type': 'str'}, |
| 53 |
'private_key_file': {'type': 'str'}, |
| 54 |
'local_user': {'type': 'str'}, |
| 55 |
'port': {'type': 'int'}, |
| 56 |
'ssh_config_file': {'type': 'str'}, |
| 57 |
'ssh_relay_file': {'type': 'str'}, |
| 58 |
'ssh_proxy_folder': {'type': 'str'} |
| 59 |
} |
| 60 |
) |
| 61 |
|
| 62 |
validation_result, new_module_args = self.validate_argument_spec( |
| 63 |
argument_spec=merged_arg_spec, |
| 64 |
) |
| 65 |
|
| 66 |
auth_source = os.environ.get('ANSIBLE_AZURE_AUTH_SOURCE', None) or new_module_args.get('auth_source') |
| 67 |
auth_options = dict( |
| 68 |
auth_source=auth_source, |
| 69 |
profile=new_module_args.get('profile'), |
| 70 |
subscription_id=new_module_args.get('subscription_id'), |
| 71 |
client_id=new_module_args.get('client_id'), |
| 72 |
secret=new_module_args.get('secret'), |
| 73 |
tenant=new_module_args.get('tenant'), |
| 74 |
ad_user=new_module_args.get('ad_user'), |
| 75 |
password=new_module_args.get('password'), |
| 76 |
cloud_environment=new_module_args.get('cloud_environment'), |
| 77 |
cert_validation_mode=new_module_args.get('cert_validation_mode'), |
| 78 |
api_profile=new_module_args.get('api_profile'), |
| 79 |
track1_cred=True, |
| 80 |
adfs_authority_url=new_module_args.get('adfs_authority_url') |
| 81 |
) |
| 82 |
|
| 83 |
inventory_hostname = new_module_args.get('inventory_hostname') |
| 84 |
ansible_host = new_module_args.get('ansible_host') |
| 85 |
resource_group = new_module_args.get('resource_group') |
| 86 |
resource_type = new_module_args.get('resource_type') |
| 87 |
private_key_file = new_module_args.get('private_key_file') |
| 88 |
local_user = new_module_args.get('local_user') |
| 89 |
port = new_module_args.get('port') |
| 90 |
ssh_config_file = new_module_args.get('ssh_config_file') |
| 91 |
ssh_relay_file = new_module_args.get('ssh_relay_file') |
| 92 |
ssh_proxy_folder = new_module_args.get('ssh_proxy_folder') |
| 93 |
result.update(dict( |
| 94 |
changed=False, |
| 95 |
rc=0, |
| 96 |
stderr='', |
| 97 |
stdout='' |
| 98 |
)) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
azure_auth = AzureRMAuth(**auth_options) |
| 104 |
rest_client = GenericRestClient(azure_auth.azure_credential_track2, |
| 105 |
azure_auth.subscription_id, |
| 106 |
azure_auth._cloud_environment.endpoints.resource_manager, |
| 107 |
credential_scopes=[azure_auth._cloud_environment.endpoints.resource_manager + ".default"]) |
| 108 |
|
| 109 |
rest_client.error_map = { |
| 110 |
401: ClientAuthenticationError, |
| 111 |
404: ResourceNotFoundError, |
| 112 |
409: ResourceExistsError, |
| 113 |
} |
| 114 |
|
| 115 |
config_session = ssh_info.ConfigSession(ssh_config_file, |
| 116 |
ssh_relay_file, |
| 117 |
resource_group, |
| 118 |
inventory_hostname, |
| 119 |
ansible_host, |
| 120 |
private_key_file, |
| 121 |
local_user, |
| 122 |
port, |
| 123 |
resource_type, |
| 124 |
ssh_proxy_folder) |
| 125 |
|
| 126 |
try: |
| 127 |
cert_lifetime = None |
| 128 |
config_session.proxy_path = connectivity_utils.install_client_side_proxy(config_session.ssh_proxy_folder) |
| 129 |
(config_session.relay_info, |
| 130 |
config_session.new_service_config) = connectivity_utils.get_relay_information(rest_client, |
| 131 |
azure_auth.subscription_id, |
| 132 |
config_session.resource_group_name, |
| 133 |
config_session.hostname, |
| 134 |
config_session.resource_type, |
| 135 |
cert_lifetime, |
| 136 |
config_session.port) |
| 137 |
except Exception as e: |
| 138 |
raise AnsibleActionFail("Failed to get relay information.", orig_exc=e) |
| 139 |
|
| 140 |
config_text = config_session.get_config_text() |
| 141 |
ssh_config_path = config_session.ssh_config_file |
| 142 |
|
| 143 |
ssh_config_dir = os.path.dirname(ssh_config_path) |
| 144 |
if not os.path.isdir(ssh_config_dir): |
| 145 |
os.makedirs(ssh_config_dir) |
| 146 |
|
| 147 |
file_utils.write_to_file(ssh_config_path, |
| 148 |
'w', |
| 149 |
'\n'.join(config_text), |
| 150 |
f"Couldn't write ssh config to file {ssh_config_path}.", |
| 151 |
'utf-8') |
| 152 |
|
| 153 |
result['stdout'] = "SSH proxy configured for %s in %s" % (inventory_hostname, config_session.ssh_config_file) |
| 154 |
return result |