mirror of https://github.com/jumpserver/jumpserver
feat: winrm协议支持网域自动化
parent
bc186df8d5
commit
cfe0206179
|
@ -251,6 +251,10 @@ class BasePlaybookManager:
|
||||||
err_msg = 'Gateway is not active: %s' % jms_asset.get('name', '')
|
err_msg = 'Gateway is not active: %s' % jms_asset.get('name', '')
|
||||||
print('\033[31m %s \033[0m\n' % err_msg)
|
print('\033[31m %s \033[0m\n' % err_msg)
|
||||||
not_valid.append(k)
|
not_valid.append(k)
|
||||||
|
else:
|
||||||
|
if host['ansible_connection'] == 'winrm':
|
||||||
|
host['ansible_host'] = '127.0.0.1'
|
||||||
|
host['ansible_port'] = server.local_bind_port
|
||||||
else:
|
else:
|
||||||
jms_asset['address'] = '127.0.0.1'
|
jms_asset['address'] = '127.0.0.1'
|
||||||
jms_asset['port'] = server.local_bind_port
|
jms_asset['port'] = server.local_bind_port
|
||||||
|
|
|
@ -77,8 +77,9 @@ class JMSInventory:
|
||||||
host['error'] = _("No account available")
|
host['error'] = _("No account available")
|
||||||
return host
|
return host
|
||||||
|
|
||||||
|
port = protocol.port if protocol else 22
|
||||||
host['ansible_host'] = asset.address
|
host['ansible_host'] = asset.address
|
||||||
host['ansible_port'] = protocol.port if protocol else 22
|
host['ansible_port'] = port
|
||||||
|
|
||||||
su_from = account.su_from
|
su_from = account.su_from
|
||||||
if platform.su_enabled and su_from:
|
if platform.su_enabled and su_from:
|
||||||
|
@ -95,36 +96,31 @@ class JMSInventory:
|
||||||
host.update(self.make_account_ansible_vars(account))
|
host.update(self.make_account_ansible_vars(account))
|
||||||
|
|
||||||
if gateway:
|
if gateway:
|
||||||
|
ansible_connection = host.get('ansible_connection', 'ssh')
|
||||||
|
if ansible_connection in ('local', 'winrm'):
|
||||||
|
host['gateway'] = {
|
||||||
|
'address': gateway.address, 'port': gateway.port,
|
||||||
|
'username': gateway.username, 'secret': gateway.password
|
||||||
|
}
|
||||||
|
host['jms_asset']['port'] = port
|
||||||
|
else:
|
||||||
host.update(self.make_proxy_command(gateway))
|
host.update(self.make_proxy_command(gateway))
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_primary_protocol(protocols):
|
|
||||||
if protocols:
|
|
||||||
primary = protocols[0]
|
|
||||||
protocol = primary.name
|
|
||||||
port = primary.port
|
|
||||||
else:
|
|
||||||
protocol = 'null'
|
|
||||||
port = 0
|
|
||||||
return protocol, port
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_ansible_protocol(ansible_config, protocols):
|
def get_ansible_protocol(ansible_config, protocols):
|
||||||
|
invalid_protocol = type('protocol', (), {'name': 'null', 'port': 0})
|
||||||
ansible_connection = ansible_config.get('ansible_connection')
|
ansible_connection = ansible_config.get('ansible_connection')
|
||||||
if ansible_connection and not ansible_config == 'smart':
|
# 数值越小,优先级越高,若用户在 ansible_config 中配置了,则提高用户配置方式的优先级
|
||||||
# 数值越小,优先级越高
|
|
||||||
protocol_priority = {'ssh': 10, 'winrm': 9, ansible_connection: 1}
|
protocol_priority = {'ssh': 10, 'winrm': 9, ansible_connection: 1}
|
||||||
|
protocol_sorted = sorted(protocols, key=lambda x: protocol_priority.get(x.name, 999))
|
||||||
protocol_matched = list(filter(lambda x: x.name in protocol_priority, protocols))
|
protocol = protocol_sorted[0] if protocol_sorted else invalid_protocol
|
||||||
protocol_sorted = sorted(protocol_matched, key=lambda x: protocol_priority[x.name])
|
|
||||||
|
|
||||||
protocol = protocol_sorted[0] if protocol_sorted else None
|
|
||||||
return protocol
|
return protocol
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fill_ansible_config(ansible_config, protocol):
|
def fill_ansible_config(ansible_config, protocol):
|
||||||
|
if protocol.name in ('ssh', 'winrm'):
|
||||||
|
ansible_config['ansible_connection'] = protocol.name
|
||||||
if protocol and protocol.name == 'winrm':
|
if protocol and protocol.name == 'winrm':
|
||||||
ansible_config['ansible_connection'] = 'winrm'
|
|
||||||
if protocol.setting.get('use_ssl', False):
|
if protocol.setting.get('use_ssl', False):
|
||||||
ansible_config['ansible_winrm_scheme'] = 'https'
|
ansible_config['ansible_winrm_scheme'] = 'https'
|
||||||
ansible_config['ansible_winrm_transport'] = 'ssl'
|
ansible_config['ansible_winrm_transport'] = 'ssl'
|
||||||
|
@ -135,14 +131,19 @@ class JMSInventory:
|
||||||
return ansible_config
|
return ansible_config
|
||||||
|
|
||||||
def asset_to_host(self, asset, account, automation, protocols, platform):
|
def asset_to_host(self, asset, account, automation, protocols, platform):
|
||||||
protocol, port = self.get_primary_protocol(protocols)
|
try:
|
||||||
|
ansible_config = dict(automation.ansible_config)
|
||||||
|
except (AttributeError, TypeError):
|
||||||
|
ansible_config = {}
|
||||||
|
|
||||||
|
protocol = self.get_ansible_protocol(ansible_config, protocols)
|
||||||
|
|
||||||
host = {
|
host = {
|
||||||
'name': '{}'.format(asset.name.replace(' ', '_')),
|
'name': '{}'.format(asset.name.replace(' ', '_')),
|
||||||
'jms_asset': {
|
'jms_asset': {
|
||||||
'id': str(asset.id), 'name': asset.name, 'address': asset.address,
|
'id': str(asset.id), 'name': asset.name, 'address': asset.address,
|
||||||
'type': asset.type, 'category': asset.category,
|
'type': asset.type, 'category': asset.category,
|
||||||
'protocol': protocol, 'port': port,
|
'protocol': protocol.name, 'port': protocol.port,
|
||||||
'spec_info': asset.spec_info, 'secret_info': asset.secret_info,
|
'spec_info': asset.spec_info, 'secret_info': asset.secret_info,
|
||||||
'protocols': [{'name': p.name, 'port': p.port} for p in protocols],
|
'protocols': [{'name': p.name, 'port': p.port} for p in protocols],
|
||||||
},
|
},
|
||||||
|
@ -155,29 +156,15 @@ class JMSInventory:
|
||||||
if host['jms_account'] and asset.platform.type == 'oracle':
|
if host['jms_account'] and asset.platform.type == 'oracle':
|
||||||
host['jms_account']['mode'] = 'sysdba' if account.privileged else None
|
host['jms_account']['mode'] = 'sysdba' if account.privileged else None
|
||||||
|
|
||||||
try:
|
ansible_config = self.fill_ansible_config(ansible_config, protocol)
|
||||||
ansible_config = dict(automation.ansible_config)
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
ansible_config = {}
|
|
||||||
|
|
||||||
ansible_protocol = self.get_ansible_protocol(ansible_config, protocols)
|
|
||||||
ansible_config = self.fill_ansible_config(ansible_config, ansible_protocol)
|
|
||||||
host.update(ansible_config)
|
host.update(ansible_config)
|
||||||
|
|
||||||
gateway = None
|
gateway = None
|
||||||
if not asset.is_gateway and asset.domain:
|
if not asset.is_gateway and asset.domain:
|
||||||
gateway = asset.domain.select_gateway()
|
gateway = asset.domain.select_gateway()
|
||||||
|
|
||||||
ansible_connection = ansible_config.get('ansible_connection', 'ssh')
|
|
||||||
if ansible_connection == 'local':
|
|
||||||
if gateway:
|
|
||||||
host['gateway'] = {
|
|
||||||
'address': gateway.address, 'port': gateway.port,
|
|
||||||
'username': gateway.username, 'secret': gateway.password
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
self.make_account_vars(
|
self.make_account_vars(
|
||||||
host, asset, account, automation, ansible_protocol, platform, gateway
|
host, asset, account, automation, protocol, platform, gateway
|
||||||
)
|
)
|
||||||
return host
|
return host
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue