Commit 82286ea7 authored by Zi Chuanxiu's avatar Zi Chuanxiu

fix set host vars and add set group vars

parent 0f8abb60
...@@ -13,11 +13,10 @@ from passlib.hash import sha512_crypt ...@@ -13,11 +13,10 @@ from passlib.hash import sha512_crypt
from utils import get_rand_pass 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')
import os.path
API_DIR = os.path.dirname(os.path.abspath(__file__))
ANSIBLE_DIR = os.path.join(API_DIR, 'playbooks')
...@@ -50,38 +49,48 @@ class MyInventory(object): ...@@ -50,38 +49,48 @@ class MyInventory(object):
""" """
def __init__(self, resource): def __init__(self, resource):
""" """
resource :
resource的数据格式是一个列表字典,比如 resource的数据格式是一个列表字典,比如
{ {
"group1": [{"hostname": "10.10.10.10", "port": "22", "group1": {
"username": "test", "password": "mypass"}, ...], "hosts": [{"hostname": "10.10.10.10", "port": "22", "username": "test", "password": "mypass"}, ...],
"group2": [{"hostname": "10.10.10.10", "port": "22", "vars": {"var1": value1, "var2": value2, ...}
"username": "test", "password": "mypass"}, ...] }
} }
如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
[{"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"}, ...]
""" """
self.resource = resource self.resource = resource
self.inventory = Inventory() self.inventory = Inventory()
self.gen_inventory() self.gen_inventory()
def add_group(self, hosts, groupname): def add_group(self, hosts, groupname, groupvars=None):
""" """
add hosts to a group add hosts to a group
""" """
my_group = Group(name=groupname) 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: for host in hosts:
hostname = host.get("hostname") # set connection variables
hostport = host.get("hostport") hostname = host.pop("hostname")
username = host.get("username") hostport = host.pop("port")
password = host.get("password") username = host.pop("username")
password = host.pop("password")
my_host = Host(name=hostname, port=hostport) my_host = Host(name=hostname, port=hostport)
my_host.set_variable('ansible_ssh_host', hostname) my_host.set_variable('ansible_ssh_host', hostname)
my_host.set_variable('ansible_ssh_port', hostport) my_host.set_variable('ansible_ssh_port', hostport)
my_host.set_variable('ansible_ssh_user', username) my_host.set_variable('ansible_ssh_user', username)
my_host.set_variable('ansible_ssh_pass', password) 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) my_group.add_host(my_host)
self.inventory.add_group(my_group) self.inventory.add_group(my_group)
...@@ -93,8 +102,8 @@ class MyInventory(object): ...@@ -93,8 +102,8 @@ class MyInventory(object):
if isinstance(self.resource, list): if isinstance(self.resource, list):
self.add_group(self.resource, 'my_group') self.add_group(self.resource, 'my_group')
elif isinstance(self.resource, dict): elif isinstance(self.resource, dict):
for groupname, hosts in self.resource.iteritems(): for groupname, hosts_and_vars in self.resource.iteritems():
self.add_group(hosts, groupname) self.add_group(hosts_and_vars.get("hosts"), groupname, hosts_and_vars.get("vars"))
class Command(MyInventory): class Command(MyInventory):
...@@ -316,12 +325,12 @@ class MyPlaybook(MyInventory): ...@@ -316,12 +325,12 @@ class MyPlaybook(MyInventory):
super(MyPlaybook, self).__init__(*args, **kwargs) super(MyPlaybook, self).__init__(*args, **kwargs)
def run(self, playbook_relational_path): def run(self, playbook_relational_path, extra_vars=None):
""" """
run ansible playbook, run ansible playbook,
only surport relational path. only surport relational path.
""" """
stats = CustomAggregateStats() stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
playbook_path = os.path.join(ANSIBLE_DIR, playbook_relational_path) playbook_path = os.path.join(ANSIBLE_DIR, playbook_relational_path)
...@@ -332,7 +341,8 @@ class MyPlaybook(MyInventory): ...@@ -332,7 +341,8 @@ class MyPlaybook(MyInventory):
callbacks = playbook_cb, callbacks = playbook_cb,
runner_callbacks = runner_cb, runner_callbacks = runner_cb,
inventory = self.inventory, inventory = self.inventory,
check=True) extra_vars = extra_vars,
check=False)
self.results = pb.run() self.results = pb.run()
...@@ -354,13 +364,14 @@ class App(MyPlaybook): ...@@ -354,13 +364,14 @@ class App(MyPlaybook):
if __name__ == "__main__": 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 = MyPlaybook(resource)
# playbook.run('test.yml') # playbook.run('test.yml')
# print playbook.raw_results # print playbook.raw_results
command = Command(resource) # command = Command(resource)
command.run("who") # command.run("who")
print command.stdout # print command.raw_results
# task = Tasks(resource) # task = Tasks(resource)
...@@ -375,7 +386,3 @@ if __name__ == "__main__": ...@@ -375,7 +386,3 @@ if __name__ == "__main__":
# print task.del_init_users() # print task.del_init_users()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment