2017-12-10 16:29:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from .ansible.inventory import BaseInventory
|
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
__all__ = [
|
2019-03-22 08:52:27 +00:00
|
|
|
'JMSInventory', 'JMSCustomInventory',
|
2017-12-10 16:29:25 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2019-03-22 08:52:27 +00:00
|
|
|
class JMSBaseInventory(BaseInventory):
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2018-03-28 11:37:29 +00:00
|
|
|
def convert_to_ansible(self, asset, run_as_admin=False):
|
|
|
|
info = {
|
|
|
|
'id': asset.id,
|
2018-12-10 02:11:54 +00:00
|
|
|
'hostname': asset.hostname,
|
2018-03-28 11:37:29 +00:00
|
|
|
'ip': asset.ip,
|
2019-06-13 10:58:43 +00:00
|
|
|
'port': asset.ssh_port,
|
2018-03-28 11:37:29 +00:00
|
|
|
'vars': dict(),
|
|
|
|
'groups': [],
|
|
|
|
}
|
|
|
|
if asset.domain and asset.domain.has_gateway():
|
|
|
|
info["vars"].update(self.make_proxy_command(asset))
|
|
|
|
if run_as_admin:
|
|
|
|
info.update(asset.get_auth_info())
|
2019-06-13 10:58:43 +00:00
|
|
|
if asset.is_unixlike():
|
|
|
|
info["become"] = asset.admin_user.become_info
|
2018-03-28 11:37:29 +00:00
|
|
|
for node in asset.nodes.all():
|
|
|
|
info["groups"].append(node.value)
|
2019-06-13 10:58:43 +00:00
|
|
|
if asset.is_windows():
|
|
|
|
info["vars"].update({
|
|
|
|
"ansible_connection": "ssh",
|
|
|
|
"ansible_shell_type": "cmd",
|
|
|
|
})
|
2018-03-28 11:37:29 +00:00
|
|
|
for label in asset.labels.all():
|
|
|
|
info["vars"].update({
|
|
|
|
label.name: label.value
|
|
|
|
})
|
|
|
|
if asset.domain:
|
|
|
|
info["vars"].update({
|
|
|
|
"domain": asset.domain.name,
|
|
|
|
})
|
|
|
|
info["groups"].append("domain_"+asset.domain.name)
|
|
|
|
return info
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_proxy_command(asset):
|
|
|
|
gateway = asset.domain.random_gateway()
|
|
|
|
proxy_command_list = [
|
|
|
|
"ssh", "-p", str(gateway.port),
|
2018-06-08 10:28:15 +00:00
|
|
|
"-o", "StrictHostKeyChecking=no",
|
2018-03-28 11:37:29 +00:00
|
|
|
"{}@{}".format(gateway.username, gateway.ip),
|
|
|
|
"-W", "%h:%p", "-q",
|
|
|
|
]
|
|
|
|
|
|
|
|
if gateway.password:
|
|
|
|
proxy_command_list.insert(
|
2018-07-06 02:34:16 +00:00
|
|
|
0, "sshpass -p '{}'".format(gateway.password)
|
2018-03-28 11:37:29 +00:00
|
|
|
)
|
|
|
|
if gateway.private_key:
|
|
|
|
proxy_command_list.append("-i {}".format(gateway.private_key_file))
|
|
|
|
|
2018-06-08 10:40:31 +00:00
|
|
|
proxy_command = "'-o ProxyCommand={}'".format(
|
2018-03-28 11:37:29 +00:00
|
|
|
" ".join(proxy_command_list)
|
|
|
|
)
|
|
|
|
return {"ansible_ssh_common_args": proxy_command}
|
2019-03-22 08:52:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JMSInventory(JMSBaseInventory):
|
|
|
|
"""
|
|
|
|
JMS Inventory is the manager with jumpserver assets, so you can
|
|
|
|
write you own manager, construct you inventory,
|
|
|
|
user_info is obtained from admin_user or asset_user
|
|
|
|
"""
|
|
|
|
def __init__(self, assets, run_as_admin=False, run_as=None, become_info=None):
|
|
|
|
"""
|
2019-06-13 10:58:43 +00:00
|
|
|
:param assets: assets
|
2019-03-22 08:52:27 +00:00
|
|
|
:param run_as_admin: True 是否使用管理用户去执行, 每台服务器的管理用户可能不同
|
|
|
|
:param run_as: 用户名(添加了统一的资产用户管理器之后AssetUserManager加上之后修改为username)
|
|
|
|
:param become_info: 是否become成某个用户去执行
|
|
|
|
"""
|
|
|
|
self.assets = assets
|
|
|
|
self.using_admin = run_as_admin
|
|
|
|
self.run_as = run_as
|
|
|
|
self.become_info = become_info
|
|
|
|
|
|
|
|
host_list = []
|
|
|
|
|
|
|
|
for asset in assets:
|
2019-06-13 10:58:43 +00:00
|
|
|
host = self.convert_to_ansible(asset, run_as_admin=run_as_admin)
|
|
|
|
if run_as:
|
2019-03-22 08:52:27 +00:00
|
|
|
run_user_info = self.get_run_user_info(host)
|
|
|
|
host.update(run_user_info)
|
2019-06-13 10:58:43 +00:00
|
|
|
if become_info and asset.is_unixlike():
|
2019-03-22 08:52:27 +00:00
|
|
|
host.update(become_info)
|
2019-06-13 10:58:43 +00:00
|
|
|
host_list.append(host)
|
|
|
|
|
2019-03-22 08:52:27 +00:00
|
|
|
super().__init__(host_list=host_list)
|
|
|
|
|
|
|
|
def get_run_user_info(self, host):
|
2019-06-19 03:31:38 +00:00
|
|
|
from assets.backends import AssetUserManager
|
2019-03-22 08:52:27 +00:00
|
|
|
|
|
|
|
if not self.run_as:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
asset = self.assets.get(id=host.get('id'))
|
2019-06-19 03:31:38 +00:00
|
|
|
manager = AssetUserManager()
|
|
|
|
run_user = manager.get(self.run_as, asset)
|
2019-03-22 08:52:27 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(e, exc_info=True)
|
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
return run_user._to_secret_json()
|
|
|
|
|
|
|
|
|
|
|
|
class JMSCustomInventory(JMSBaseInventory):
|
|
|
|
"""
|
|
|
|
JMS Custom Inventory is the manager with jumpserver assets,
|
|
|
|
user_info is obtained from custom parameter
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, assets, username, password=None, public_key=None, private_key=None):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
self.assets = assets
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
self.public_key = public_key
|
|
|
|
self.private_key = private_key
|
|
|
|
|
|
|
|
host_list = []
|
|
|
|
|
|
|
|
for asset in assets:
|
2019-06-13 10:58:43 +00:00
|
|
|
host = self.convert_to_ansible(asset)
|
2019-03-22 08:52:27 +00:00
|
|
|
run_user_info = self.get_run_user_info()
|
|
|
|
host.update(run_user_info)
|
2019-06-13 10:58:43 +00:00
|
|
|
host_list.append(host)
|
2019-03-22 08:52:27 +00:00
|
|
|
|
|
|
|
super().__init__(host_list=host_list)
|
|
|
|
|
|
|
|
def get_run_user_info(self):
|
|
|
|
return {
|
|
|
|
'username': self.username,
|
|
|
|
'password': self.password,
|
|
|
|
'public_key': self.public_key,
|
|
|
|
'private_key': self.private_key
|
|
|
|
}
|