def Test(): """ 返回主机信息,组信息,组内主机信息 :return: """ host_list = [{ "hostname": "testserver1", "ip": "102.1.1.1", "port": 22, "username": "******", "password": "******", "private_key": "/tmp/private_key", "become": { "method": "sudo", "user": "******", "pass": None, }, "groups": ["group1", "group2"], "vars": {"sexy": "yes"}, }, { "hostname": "testserver2", "ip": "8.8.8.8", "port": 2222, "username": "******", "password": "******", "private_key": "/tmp/private_key", "become": { "method": "su", "user": "******", "pass": "******", }, "groups": ["group3", "group4"], "vars": {"love": "yes"}, }] res=[{'hostsname': '127.0.0.1', 'ip': '127.0.0.1', 'username': '******', 'port': '22', 'password': '******'}] inventory = BaseInventory(res) #print (inventory) print("#"*10 + "Hosts" + "#"*10) for host in inventory.hosts: print(host) print("#" * 10 + "Groups" + "#" * 10) for group in inventory.groups: print(group) print("#" * 10 + "all group hosts" + "#" * 10) group = inventory.get_group('all') print(group.hosts)
def Test(): """ 返回主机信息,组信息,组内主机信息 :return: """ host_list = [{ "hostname": "testserver1", "ip": "102.1.1.1", "port": 22, "username": "******", "password": "******", "private_key": "/tmp/private_key", "become": { "method": "sudo", "user": "******", "pass": None, }, "groups": ["group1", "group2"], "vars": {"sexy": "yes"}, }, { "hostname": "testserver2", "ip": "8.8.8.8", "port": 2222, "username": "******", "password": "******", "private_key": "/tmp/private_key", "become": { "method": "su", "user": "******", "pass": "******", }, "groups": ["group3", "group4"], "vars": {"love": "yes"}, }] inventory = BaseInventory(host_list=host_list) print("#" * 10 + "Hosts" + "#" * 10) for host in inventory.hosts: print(host) print("#" * 10 + "Groups" + "#" * 10) for group in inventory.groups: print(group) print("#" * 10 + "all group hosts" + "#" * 10) group = inventory.get_group('all') print(group.hosts)
def setUp(self): host_data = [ { "hostname": "demo-web1", "ip": "192.168.33.101", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web2", "ip": "192.168.33.102", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web3", "ip": "192.168.33.103", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web4", "ip": "192.168.33.104", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, ] inventory = BaseInventory(host_data) self.runner = CommandRunner(inventory)
def TestAdHocRunner(): """ 以yml的形式 执行多个命令 :return: """ host_data = [ { "hostname": "server", "ip": "192.168.5.224", "port": 22, "username": "******", "password": "", "private_key": '/root/.ssh/id_rsa', "become": { "method": "sudo", "user": "******", "pass": None, } }, ] inventory = BaseInventory(host_data) runner = AdHocRunner(inventory) tasks = [ {"action": {"module": "shell", "args": "hostname"}, "name": "run_whoami"}, ] ret = runner.run(tasks, "all") print(ret.results_summary) print(ret.results_raw)
def __init__(self, level, col_idx, row_idx, *, inventory_contents=[]): super().__init__(level, col_idx, row_idx) self.inventory = BaseInventory.of_size(self.inventory_slots) # inventory_contents should be a list of item classes. for item in inventory_contents: self.inventory.add_item(item(self.level.parent.player)) self.precache_entry = { "col": self.col_idx, "row": self.row_idx, "inventory": self.create_inventory_cache() } self.level.precache["tiles"].append(self.precache_entry)
def setUp(self): host_data = [ { "hostname": "testserver1", "ip": "192.168.33.101", "port": 22, "username": "******", "password": "******", }, ] inventory = BaseInventory(host_data) self.runner = AdHocRunner(inventory)
def set_inventory(self, ip, port, username, password): host_data = [ { "hostname": self.hostname, "ip": ip, "port": port, "username": username, "password": password, }, ] inventory = BaseInventory(host_data) return inventory
def __init__(self, inventory=None, options=None): # self.options = self.update_options(options) self.loader = DataLoader() if inventory: self.options = self.update_options(options) self.inventory = BaseInventory(inventory) else: self.inventory = InventoryManager(self.loader, sources='localhost,') self.options = self.update_options({"connection": 'local'}) self.variable_manager = self.variable_manager_class( loader=self.loader, inventory=self.inventory, )
def TestAdHocRunner(): """ 以yml的形式 执行多个命令 :return: """ host_data = [ { "hostname": "testserver", "ip": "192.168.10.93", "port": 22, "username": "******", "password": "******", }, ] inventory = BaseInventory(host_data) runner = AdHocRunner(inventory) tasks = [ { "action": { "module": "cron", "args": "name=\"sync time\" minute=\"*/3\" job=\"/usr/sbin/ntpdate time.nist.gov &> /dev/null\"" }, "name": "run_cmd" }, { "action": { "module": "shell", "args": "whoami" }, "name": "run_whoami" }, ] ret = runner.run(tasks, "all") print(ret.results_summary) print(ret.results_raw)
def TestCommandRunner(): """ 执行单个命令,返回结果 :return: """ host_data = [ { "hostname": "testserver", "ip": "192.168.10.93", "port": 22, "username": "******", "password": "******", }, ] inventory = BaseInventory(host_data) runner = CommandRunner(inventory) res = runner.execute('pwd', 'all') print(res.results_command) print(res.results_raw) print(res.results_command['testserver']['stdout'])
def TestAdHocRunner(): """ 以yml的形式 执行多个命令 :return: """ host_data = [ { "hostname": "server", "ip": "192.168.5.224", "port": 22, "username": "******", "password": "", "private_key": '/root/.ssh/id_rsa', "become": { "method": "sudo", "user": "******", "pass": None, } }, ] inventory = BaseInventory(host_data) runner = AdHocRunner(inventory) tasks = [ # {"action": {"module": "cron","args": "name=\"sync time\" minute=\"*/3\" job=\"/usr/sbin/ntpdate time.nist.gov &> /dev/null\"" }, "name": "run_cmd"}, { "action": { "module": "shell", "args": "hostname" }, "name": "run_whoami" }, ] ret = runner.run(tasks, "all") print(ret.results_summary) print(ret.results_raw)
from inventory import BaseInventory from runner import AdHocRunner host_data = [ { "hostname": "testserver", "ip": "10.20.15.105", "port": 22, "username": "******", "password": "******", } ] inventory = BaseInventory(host_data) runner = AdHocRunner(inventory) tasks = [ {"action": {"module": "shell", "args": "ls"}, "name": "run_cmd"}, {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"}, ] ret = runner.run(tasks, "all")