mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-04-27 19:17:01 +00:00
perf: as account username
This commit is contained in:
parent
1ee70af93d
commit
2aace05099
@ -2,11 +2,13 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
import sys
|
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
from assets import const
|
||||||
|
|
||||||
__all__ = ['JMSInventory']
|
__all__ = ['JMSInventory']
|
||||||
|
|
||||||
|
|
||||||
@ -38,6 +40,12 @@ class JMSInventory:
|
|||||||
.prefetch_related('platform', 'domain', 'accounts')
|
.prefetch_related('platform', 'domain', 'accounts')
|
||||||
return assets
|
return assets
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_username(asset, account):
|
||||||
|
if asset.category == const.Category.DS:
|
||||||
|
return account.full_username
|
||||||
|
return account.username
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def group_by_platform(assets):
|
def group_by_platform(assets):
|
||||||
groups = defaultdict(list)
|
groups = defaultdict(list)
|
||||||
@ -76,10 +84,10 @@ class JMSInventory:
|
|||||||
proxy_command = f"-o ProxyCommand='{' '.join(proxy_command_list)}'"
|
proxy_command = f"-o ProxyCommand='{' '.join(proxy_command_list)}'"
|
||||||
return {"ansible_ssh_common_args": proxy_command}
|
return {"ansible_ssh_common_args": proxy_command}
|
||||||
|
|
||||||
@staticmethod
|
def make_account_ansible_vars(self, asset, account, path_dir):
|
||||||
def make_account_ansible_vars(account, path_dir):
|
username = self.get_username(asset, account)
|
||||||
var = {
|
var = {
|
||||||
'ansible_user': account.full_username,
|
'ansible_user': username,
|
||||||
}
|
}
|
||||||
if not account.secret:
|
if not account.secret:
|
||||||
return var
|
return var
|
||||||
@ -129,13 +137,13 @@ class JMSInventory:
|
|||||||
host.update(self.make_custom_become_ansible_vars(account, su_from_auth, path_dir))
|
host.update(self.make_custom_become_ansible_vars(account, su_from_auth, path_dir))
|
||||||
elif platform.su_enabled and not su_from and \
|
elif platform.su_enabled and not su_from and \
|
||||||
self.task_type in (AutomationTypes.change_secret, AutomationTypes.push_account):
|
self.task_type in (AutomationTypes.change_secret, AutomationTypes.push_account):
|
||||||
host.update(self.make_account_ansible_vars(account, path_dir))
|
host.update(self.make_account_ansible_vars(asset, account, path_dir))
|
||||||
if platform.type not in ["windows", "windows_ad"]:
|
if platform.type not in ["windows", "windows_ad"]:
|
||||||
host['ansible_become'] = True
|
host['ansible_become'] = True
|
||||||
host['ansible_become_user'] = 'root'
|
host['ansible_become_user'] = 'root'
|
||||||
host['ansible_become_password'] = account.escape_jinja2_syntax(account.secret)
|
host['ansible_become_password'] = account.escape_jinja2_syntax(account.secret)
|
||||||
else:
|
else:
|
||||||
host.update(self.make_account_ansible_vars(account, path_dir))
|
host.update(self.make_account_ansible_vars(asset, account, path_dir))
|
||||||
|
|
||||||
if platform.is_huawei():
|
if platform.is_huawei():
|
||||||
host['ansible_connection'] = 'network_cli'
|
host['ansible_connection'] = 'network_cli'
|
||||||
@ -192,6 +200,7 @@ class JMSInventory:
|
|||||||
tp, category = asset.type, asset.category
|
tp, category = asset.type, asset.category
|
||||||
name = re.sub(r'[ \[\]/]', '_', asset.name)
|
name = re.sub(r'[ \[\]/]', '_', asset.name)
|
||||||
secret_info = {k: v for k, v in asset.secret_info.items() if v}
|
secret_info = {k: v for k, v in asset.secret_info.items() if v}
|
||||||
|
username = self.get_username(asset, account)
|
||||||
host = {
|
host = {
|
||||||
'name': name,
|
'name': name,
|
||||||
'local_python_interpreter': sys.executable,
|
'local_python_interpreter': sys.executable,
|
||||||
@ -204,7 +213,8 @@ class JMSInventory:
|
|||||||
'origin_address': asset.address
|
'origin_address': asset.address
|
||||||
},
|
},
|
||||||
'jms_account': {
|
'jms_account': {
|
||||||
'id': str(account.id), 'username': account.full_username,
|
'id': str(account.id),
|
||||||
|
'username': username,
|
||||||
'secret': account.escape_jinja2_syntax(account.secret),
|
'secret': account.escape_jinja2_syntax(account.secret),
|
||||||
'secret_type': account.secret_type, 'private_key_path': account.get_private_key_path(path_dir)
|
'secret_type': account.secret_type, 'private_key_path': account.get_private_key_path(path_dir)
|
||||||
} if account else None
|
} if account else None
|
||||||
|
Loading…
Reference in New Issue
Block a user