def setUp(self): 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"}, }] self.inventory = BaseInventory(host_list=host_list)
def p(): hosts = [{ "hostname": "manager2", "ip": "192.168.1.121", "port": 22, "username": "******", "password": "******", "become": { "method": "sudo", "user": "******", "pass": None, }, "groups": ["hadoop", "test"], "vars": {"sexy": "yes"}, }, { "hostname": "ubuntu", "ip": "192.168.1.238", "port": 22, "username": "******", "password": "******", "private_key": "/tmp/private_key", "become": { "method": "su", "user": "******", "pass": "******", }, "groups": ["hadoop", "test"], "vars": {"love": "yes"}, }] o = get_default_options(playbook_path='test1.yaml') p1 = PlayBookRunner(inventory=BaseInventory(hosts), options=o) p1.run() print(p1.results_callback.output)
def setUp(self): host_data = [ { "hostname": "testserver", "ip": "192.168.1.121", "port": 22, "username": "******", "password": "******", }, ] inventory = BaseInventory(host_data) self.runner = CommandRunner(inventory)
class TestJMSInventory(unittest.TestCase): def setUp(self): 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"}, }] self.inventory = BaseInventory(host_list=host_list) def test_hosts(self): print("#"*10 + "Hosts" + "#"*10) for host in self.inventory.hosts: print(host) def test_groups(self): print("#" * 10 + "Groups" + "#" * 10) for group in self.inventory.groups: print(group) def test_group_all(self): print("#" * 10 + "all group hosts" + "#" * 10) group = self.inventory.get_group('all') print(group.hosts)
def ansible_run(hosts, tasks): # host_data = [ # { # "hostname": "testserver", # "ip": "192.168.1.121", # "port": 22, # "username": "******", # "password": "******", # }, # ] # tasks = [ # {"action": {"module": "setup", "args": ""}, "name": "run_cmd"}, # {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"}, # ] hosts = hosts tasks = tasks inventory = BaseInventory(hosts) runner = AdHocRunner(inventory) ret = runner.run(tasks, "all") return ret.results_raw
def ansible_playbook(hosts, path): hosts = [{ "hostname": "manager2", "ip": "192.168.1.121", "port": 22, "username": "******", "password": "******", "become": { "method": "sudo", "user": "******", "pass": None, }, "groups": ["hadoop", "test"], "vars": { "sexy": "yes" }, }, { "hostname": "ubuntu", "ip": "192.168.1.238", "port": 22, "username": "******", "password": "******", "private_key": "/tmp/private_key", "become": { "method": "su", "user": "******", "pass": "******", }, "groups": ["hadoop", "test"], "vars": { "love": "yes" }, }] inventory = BaseInventory(hosts) options = get_default_options(playbook_path=path) play = PlayBookRunner(inventory=inventory, options=options) play.run() return play.results_callback.output