mirror of https://github.com/jumpserver/jumpserver
fix set host vars and add set group vars
parent
0f8abb6026
commit
82286ea7ed
|
@ -13,11 +13,10 @@ from passlib.hash import sha512_crypt
|
|||
|
||||
from utils import get_rand_pass
|
||||
|
||||
|
||||
import os.path
|
||||
JPERM_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
ANSIBLE_DIR = os.path.join(JPERM_DIR, 'playbooks')
|
||||
|
||||
|
||||
API_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
ANSIBLE_DIR = os.path.join(API_DIR, 'playbooks')
|
||||
|
||||
|
||||
|
||||
|
@ -50,38 +49,48 @@ class MyInventory(object):
|
|||
"""
|
||||
def __init__(self, resource):
|
||||
"""
|
||||
resource :
|
||||
resource的数据格式是一个列表字典,比如
|
||||
{
|
||||
"group1": [{"hostname": "10.10.10.10", "port": "22",
|
||||
"username": "test", "password": "mypass"}, ...],
|
||||
"group2": [{"hostname": "10.10.10.10", "port": "22",
|
||||
"username": "test", "password": "mypass"}, ...]
|
||||
}
|
||||
如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
|
||||
[{"hostname": "10.10.10.10", "port": "22",
|
||||
"username": "test", "password": "mypass"}, ...]
|
||||
resource的数据格式是一个列表字典,比如
|
||||
{
|
||||
"group1": {
|
||||
"hosts": [{"hostname": "10.10.10.10", "port": "22", "username": "test", "password": "mypass"}, ...],
|
||||
"vars": {"var1": value1, "var2": value2, ...}
|
||||
}
|
||||
}
|
||||
|
||||
如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
|
||||
[{"hostname": "10.10.10.10", "port": "22", "username": "test", "password": "mypass"}, ...]
|
||||
"""
|
||||
self.resource = resource
|
||||
self.inventory = Inventory()
|
||||
self.gen_inventory()
|
||||
|
||||
def add_group(self, hosts, groupname):
|
||||
def add_group(self, hosts, groupname, groupvars=None):
|
||||
"""
|
||||
add hosts to a group
|
||||
"""
|
||||
my_group = Group(name=groupname)
|
||||
|
||||
# if group variables exists, add them to group
|
||||
if groupvars:
|
||||
for key, value in groupvars.iteritems():
|
||||
my_group.set_variable(key, value)
|
||||
|
||||
# add hosts to group
|
||||
for host in hosts:
|
||||
hostname = host.get("hostname")
|
||||
hostport = host.get("hostport")
|
||||
username = host.get("username")
|
||||
password = host.get("password")
|
||||
# set connection variables
|
||||
hostname = host.pop("hostname")
|
||||
hostport = host.pop("port")
|
||||
username = host.pop("username")
|
||||
password = host.pop("password")
|
||||
my_host = Host(name=hostname, port=hostport)
|
||||
my_host.set_variable('ansible_ssh_host', hostname)
|
||||
my_host.set_variable('ansible_ssh_port', hostport)
|
||||
my_host.set_variable('ansible_ssh_user', username)
|
||||
my_host.set_variable('ansible_ssh_pass', password)
|
||||
# set other variables
|
||||
for key, value in host.iteritems():
|
||||
my_host.set_variable(key, value)
|
||||
# add to group
|
||||
my_group.add_host(my_host)
|
||||
|
||||
self.inventory.add_group(my_group)
|
||||
|
@ -93,8 +102,8 @@ class MyInventory(object):
|
|||
if isinstance(self.resource, list):
|
||||
self.add_group(self.resource, 'my_group')
|
||||
elif isinstance(self.resource, dict):
|
||||
for groupname, hosts in self.resource.iteritems():
|
||||
self.add_group(hosts, groupname)
|
||||
for groupname, hosts_and_vars in self.resource.iteritems():
|
||||
self.add_group(hosts_and_vars.get("hosts"), groupname, hosts_and_vars.get("vars"))
|
||||
|
||||
|
||||
class Command(MyInventory):
|
||||
|
@ -316,12 +325,12 @@ class MyPlaybook(MyInventory):
|
|||
super(MyPlaybook, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
def run(self, playbook_relational_path):
|
||||
def run(self, playbook_relational_path, extra_vars=None):
|
||||
"""
|
||||
run ansible playbook,
|
||||
only surport relational path.
|
||||
"""
|
||||
stats = CustomAggregateStats()
|
||||
stats = callbacks.AggregateStats()
|
||||
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
|
||||
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
|
||||
playbook_path = os.path.join(ANSIBLE_DIR, playbook_relational_path)
|
||||
|
@ -332,7 +341,8 @@ class MyPlaybook(MyInventory):
|
|||
callbacks = playbook_cb,
|
||||
runner_callbacks = runner_cb,
|
||||
inventory = self.inventory,
|
||||
check=True)
|
||||
extra_vars = extra_vars,
|
||||
check=False)
|
||||
|
||||
self.results = pb.run()
|
||||
|
||||
|
@ -354,13 +364,14 @@ class App(MyPlaybook):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
resource = [{"hostname": "192.168.10.128", "port": "22", "username": "root", "password": "xxx"}]
|
||||
pass
|
||||
# resource = [{"hostname": "192.168.10.128", "port": "22", "username": "root", "password": "yusky0902"}]
|
||||
# playbook = MyPlaybook(resource)
|
||||
# playbook.run('test.yml')
|
||||
# print playbook.raw_results
|
||||
command = Command(resource)
|
||||
command.run("who")
|
||||
print command.stdout
|
||||
# command = Command(resource)
|
||||
# command.run("who")
|
||||
# print command.raw_results
|
||||
|
||||
|
||||
# task = Tasks(resource)
|
||||
|
@ -375,7 +386,3 @@ if __name__ == "__main__":
|
|||
# print task.del_init_users()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue