class AnsibleInventory(object): def __init__(self, inventory_generator): """ :type inventory_generator: ops.inventory.generator.InventoryGenerator """ self.inventory_generator = inventory_generator self.generated_path, self.ssh_config_path = inventory_generator.generate( ) # clean up variables cache for tests ansible_vars.VARIABLE_CACHE = dict() ansible_vars.HOSTVARS_CACHE = dict() ansible_inventory.HOSTS_PATTERNS_CACHE = dict() self.variable_manager = VariableManager() loader = DataLoader() self.inventory = Inventory(loader=loader, variable_manager=self.variable_manager, host_list=self.generated_path) self.variable_manager.set_inventory(self.inventory) self.inventory.set_playbook_basedir(self.generated_path) def get_hosts(self, limit): return self.inventory.get_hosts(limit) def get_host(self, host): return self.inventory.get_host(unicode(host)) def get_vars(self, host): return self.inventory.get_vars(unicode(host)) def get_ssh_config(self): return self.ssh_config_path
def create_ansible_objects(inventory_file, extra_vars, forks=50): """Create the default ansible objects needed to run a playbook. :param inventory_file: The path to the inventory file :param extra_vars: The dictionary containing the collection status of the optional products. :param forks: number of forks to run with, default of 50 :returns: tuple of (options, variable_manager, loader, inventory) """ named_options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check' ]) options = named_options(connection='ssh', module_path=C.DEFAULT_MODULE_PATH, forks=forks, become=False, become_method='sudo', become_user='******', check=False) variable_manager = VariableManager() loader = DataLoader() loader.set_vault_password(settings.SECRET_KEY) # create inventory and extra vars and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_file) variable_manager.extra_vars = extra_vars variable_manager.set_inventory(inventory) return options, variable_manager, loader, inventory
class AnsibleContext(object): def __init__(self, **kwargs): self.variable_manager = VariableManager() self.loader = DataLoader() self.options = Options(**kwargs) self.default_results_callback = DefaultResultCallback() self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list='localhost') self.variable_manager.set_inventory(self.inventory) def execute_play(self, play_source): play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader) tqm = None try: tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords={}, stdout_callback=self.default_results_callback) result = tqm.run(play) print('executed playbook, terminated with code %d.' % result) finally: if tqm is not None: tqm.cleanup()
def call_ansible(yaml_file, become=False, tag=None): """Call Ansible with a playbook.""" variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader, variable_manager) variable_manager.set_inventory(inventory) Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'tags']) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='', module_path='', forks=100, remote_user='', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=become, become_method='sudo', become_user='******', verbosity=2, check=False, tags=tag) pbex = PlaybookExecutor(playbooks=[yaml_file], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={}) logger.debug("Calling Ansible with yaml file: {}".format(yaml_file)) result = pbex.run() if result: logger.error("An error occured whilst executing the Ansible Playbook.")
class BaseCall(object): """Base class for play and playbooks.""" def __init__(self, user_dir): """Constructor. :param user_dir: user directory """ self.loader = DataLoader() self.inventory_file = os.path.join(user_dir, ANSIBLE_INVENTORY) self.callback = PawsCallback() self.inventory = None self.var_mgr = None def _set_inventory(self): """Set inventory class req. by ansible api.""" try: # < 2.4 self.var_mgr = VariableManager() self.inventory = Inventory( loader=self.loader, variable_manager=self.var_mgr, host_list=self.inventory_file ) except TypeError: # > 2.4 self.var_mgr = VariableManager(loader=self.loader) self.inventory = Inventory( loader=self.loader, sources=self.inventory_file ) finally: self.var_mgr.set_inventory(self.inventory)
def _provision_node(self, node_name, ip_address, playbook_name): # playbook_path = '/etc/ansible/install_git.yml' user = None password = None playbook_path = playbook_name host_name = ip_address # if not os.path.exists(playbook_path): # raise Exception('The playbook does not exist') variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[host_name]) variable_manager.set_inventory(inventory) options = Options(connection='smart', become_user=user) passwords = {} host = inventory.get_host(host_name) variable_manager.set_host_variable(host, 'connection', 'ssh') variable_manager.set_host_variable(host, 'ansible_ssh_user', 'root') variable_manager.set_host_variable(host, 'ansible_ssh_pass', 'root') pbex = PlaybookExecutor( playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) try: pbex.run() except Exception as ex: click.echo(ex.message)
class PlayBook(object): def __init__(self, inventory='/etc/ansible/hosts', extra_vars=None, private_key_file=None): """ :param playbook: playbook.yml :param inventory: inventory file or script :type param extra_vars: dict :param private_key_file: ssh private key """ self.pbex = None self.options = Options(private_key_file=private_key_file, connection='smart', forks=10, timeout=10, verbosity=0, check=False, listtasks=False, listhosts=False, syntax=False, subset=None, module_path=None, become=None, become_user=None, become_method='sudo') # initialize needed objects self.loader = DataLoader() self.variable_manager = VariableManager() self.variable_manager.extra_vars = extra_vars self.variable_manager.options_vars = {'ansible_check_mode': self.options.check} self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=inventory) self.variable_manager.set_inventory(self.inventory) # Limits inventory results to a subset of inventory that matches a given pattern self.inventory._subset = self.options.subset def run_playbook(self, playbook): self.pbex = PlaybookExecutor(playbooks=[playbook], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords={'conn_pass': None, 'become_pass': None}) self.pbex._tqm._stdout_callback = ResultCallback() return self.pbex.run() def run_play(self, play): pass
def exec_keys(vars_state,pub_list,host_list,passwords=''): Options = namedtuple('Options',['connection','remote_user','ask_sudo_pass','verbosity','ack_pass','host_key_checking','module_path','forks','become','become_method', 'become_user','check','listhosts','listtasks','listtags','syntax','sudo_user','sudo']) #初始化需要的对象 variable_manager = VariableManager() #管理变量的类,包括主机,组,扩展等变量,之前版本是在 inventory中的 loader = DataLoader() #用来加载解析yaml文件或JSON内容,并且支持vault的解密 options = Options(connection='smart',remote_user='******',ack_pass=None,host_key_checking=False,sudo_user='******',forks=50,sudo='yes',ask_sudo_pass=False,verbosity=5,module_path=None, become=True,become_method='sudo',become_user='******',check=None,listhosts=None,listtasks=None,listtags=None,syntax=None) passwords=dict(conn_pass=passwords) #设置密码,必须是dict类型。如果ansible主机对服务器有密钥认证,则不需要密码 inventory = Inventory( loader=loader, variable_manager=variable_manager, host_list=host_list ) #根据inventory加载对应变量,这里host_list可以是文件也可以是IP列表 variable_manager.set_inventory(inventory) extra_vars = {} #额外的参数,key.yml以及模板中的参数,对应ansible-playbook key.yml --extra-vars pub=xxx state=present/absent extra_vars["pub_list"] = pub_list extra_vars["state"] = vars_state variable_manager.extra_vars = extra_vars #playbooks填写yml文件路径,可以写多个,是个列表 playbook = PlaybookExecutor( playbooks=['/var/www/html/CMDB/CMDB/key.yml'], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords ) playbook.run()
def executor(tmpdir_factory, request): playbooks = request.node.callspec.params.get('playbook') playbook_files = [] for name, playbook in playbooks.items(): filename = str(tmpdir_factory.mktemp('data').join(name)) with open(filename, 'w') as f: f.write(playbook) playbook_files.append(filename) cli = PlaybookCLI(['', 'playbook.yml']) cli.parse() options = cli.parser.parse_args(['-v'])[0] loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=['localhost']) variable_manager.set_inventory(inventory) return PlaybookExecutor(playbooks=playbook_files, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={})
def playbook_run(playbook_path, host_inventory): loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_inventory) variable_manager.set_inventory(inventory) passwords = dict(become_pass='') Options = namedtuple('Options', [ 'connection', 'forks', 'remote_user', 'ack_pass', 'sudo_user', 'sudo', 'ask_sudo_pass', 'verbosity', 'module_path', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', ]) options = Options(connection='smart', forks=100, remote_user='******', ack_pass=None, sudo_user='******', sudo='yes', ask_sudo_pass=True, verbosity=5, module_path=None, become=True, become_method='sudo', become_user='******', check=None, listhosts=None, listtasks=None, listtags=None, syntax=None) # 多个yaml文件则以列表形式 playbook = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results_callback = CallbackModule() playbook._tqm._stdout_callback = results_callback result = playbook.run() write_task_info_to_redis(results_callback.host_ok)
def __init__(self, **kwargs): super(Ansible_playbook, self).__init__(**kwargs) self.task_file = kwargs.get('task_file', None) self.sudo = kwargs.get('sudo', False) self.sudo_user = kwargs.get('sudo_user', False) self.sudo_password = kwargs.get('sudo_password', False) # check if parameters have been provided if self._is_parameters_ok(): variable_manager = VariableManager() loader = DataLoader() options = self._get_options() passwords = {'become_pass': self.sudo_password} inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list="localhost") variable_manager.set_inventory(inventory) playbooks = [self.task_file] executor = PlaybookExecutor( playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) executor.run()
def runPlaybook(palyname, yml_file, myvars, forks): # initialize needed objects variable_manager = VariableManager() loader = DataLoaderV2(palyname) Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check']) pb_options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=forks, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method='sudo', become_user='******', verbosity=None, check=False) passwords = {} # create inventory and pass to var manager inventory = Inventory( loader=loader, variable_manager=variable_manager) variable_manager.set_inventory(inventory) variable_manager.extra_vars = myvars pbex = PlaybookExecutorV2(playbooks=[yml_file], inventory=inventory, variable_manager=variable_manager, loader=loader, options=pb_options, passwords=passwords) # pbex._tqm._stdout_callback.reset_output() rc = pbex.run() return {'rc': rc, 'detail': pbex._tqm._stdout_callback.std_lines}
def ansible_task(playbooks): Options = namedtuple('Options', \ ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax']) options = Options( connection = 'ssh', module_path = '/opt/ansible/modules', forks = 100, become = True, become_method = 'sudo', become_user = '******', check = False, listhosts = None, listtasks = None, listtags = None, syntax = None ) loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/opt/ansible/inventory') variable_manager.set_inventory(inventory) pb = PlaybookExecutor(playbooks=[playbooks], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) pb.run() stats = pb._tqm._stats ips = stats.processed.keys() return [ {ip : stats.summarize(ip)} for ip in ips ]
def playbook_api(self,yml_fp): loader = DataLoader() variable_manager = VariableManager() inventory = Inventory( loader=loader, variable_manager=variable_manager, host_list=self.ansible_host_list ) variable_manager.set_inventory(inventory) playbooks = ["%s" % yml_fp] pbex = PlaybookExecutor( playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=self.passwords ) callback = AnsiCallBack() pbex._tqm._stdout_callback = callback pbex.run() return self.evaluate_results(callback)
def run(self, _tasks): Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'remote_user', 'remote_pass', 'private_key_file', 'no_log', 'ssh_common_args', 'sftp_extra_args', 'scp_extra_args', 'ssh_extra_args', 'become', 'become_method', 'become_user', 'check', 'verbosity']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='smart', module_path=self.module_path, forks=10, remote_user=self.user, remote_pass=None, private_key_file=self.key, no_log=None, ssh_common_args=None, sftp_extra_args=None, scp_extra_args=None, ssh_extra_args=None, become=None, become_method=None, become_user=None, check=None, verbosity=None) # Instantiate our ResultsCollector for handling results as they come in results_callback = ResultsCollector() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.host) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict(name = "play", hosts = self.host, gather_facts = 'no', tasks = _tasks) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None try: # actually run it tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None, stdout_callback=results_callback, ) tqm.run(play) finally: if tqm is not None: tqm.cleanup() return results_callback.get()
def ansible_task(playbooks): Options = namedtuple('Options', \ ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax']) options = Options(connection='ssh', module_path='/opt/ansible/modules', forks=100, become=True, become_method='sudo', become_user='******', check=False, listhosts=None, listtasks=None, listtags=None, syntax=None) loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/opt/ansible/inventory') variable_manager.set_inventory(inventory) pb = PlaybookExecutor(playbooks=[playbooks], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) pb.run() stats = pb._tqm._stats ips = stats.processed.keys() return [{ip: stats.summarize(ip)} for ip in ips]
def AnsibleTask(task_list,host_list,user): '''ansible python api 2.0''' loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) task_dict = [] for i in task_list: task_dict.append({"action": {"module": i[0], "args": i[1] }}) variable_manager.extra_vars = {"ansible_ssh_user": user, "ansible_ssh_pass": ""} play_source = {"name" : "Ansible PlayBook Run", "hosts": host_list[0], "gather_facts": "no","tasks": task_dict} play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None try: tqm = TaskQueueManager( inventory = inventory, variable_manager = variable_manager, loader = loader, options = options, passwords = None, stdout_callback = 'minimal', run_tree = False, ) result = tqm.run(play) except Exception,e: result = e
def runPlaybook(hosts, playbook, tags=[], private_key_file=private_key_file): variable_manager = VariableManager() loader = DataLoader() options = Options(connection='ssh', private_key_file=private_key_file, module_path='', forks=100, become=True, become_method='sudo', become_user='******', check=False, tags=tags) passwords = dict(vault_pass='') results_callback = ResultCallback() host_file = NamedTemporaryFile(delete=False) host_file.write(b'[servers]\n') for i, h in enumerate(hosts): host_file.write(bytes('{0} num={1}\n'.format(h, i), encoding='utf-8')) host_file.close() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_file.name) variable_manager.set_inventory(inventory) pbex = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) pbex._tqm._stdout_callback = results_callback result = pbex.run() stats = pbex._tqm._stats outputs = { 0: 'Deployment successful', 1: 'Error occurred during deployment', 2: 'One or more hosts failed', 4: 'One or more hosts unreachable', 255: 'Unknown error occurred during deployment' } run_success = True hosts = sorted(stats.processed.keys()) for h in hosts: t = stats.summarize(h) if t['unreachable'] > 0 or t['failures'] > 0: run_success = False os.remove(host_file.name) try: out = outputs[result] except KeyError: out = 'Unrecognised error code' return result, out
def __init__(self, task_file, **kwargs): super(Ansible_playbook, self).__init__(**kwargs) Options = namedtuple('Options', ['connection', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'module_path']) variable_manager = VariableManager() loader = DataLoader() options = Options(connection='local', forks=100, become=None, become_method=None, become_user=None, check=False, listhosts=False, listtasks=False, listtags=False, syntax=False, module_path="") passwords = dict(vault_pass='******') inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost') variable_manager.set_inventory(inventory) playbooks = [task_file] executor = PlaybookExecutor( playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) executor.run()
def execute_playbook(playbook, host, *args, **kwargs): playbook = os.path.join(playbook_store_path,playbook) Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'remote_user', 'listhosts', 'listtasks', 'listtags', 'syntax', 'verbose']) #needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='ssh', module_path='./modules', forks=100, become=None, become_method=None, become_user=None, check=False, remote_user='******', listhosts=None, listtasks=None, listtags=None, syntax=None, verbose=None) passwords = dict(vault_pass='******') #Create inventory and pass to manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[host]) variable_manager.set_inventory(inventory) hostobj = inventory.get_host(host) for varname, value in kwargs.iteritems(): variable_manager.set_host_variable(hostobj, varname, value) #Should execute fine executor = PlaybookExecutor(playbooks=[ playbook ], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) #Forcing our callback object into the executor executor._tqm._stdout_callback = "json" results = executor.run() pprint.pprint(results) report_tasks = executor._tqm._stdout_callback.results[0]['tasks'] report = executor._tqm._stdout_callback.results report_stats = executor._tqm._stats.summarize(host) results_output = get_output(report_tasks, host) return dict(stats = report_stats, results = results_output)
def _run_playbook(playbook, service, node): # Unfortunately, there's no good way to get the options instance # with proper defaults since it's generated by argparse inside # PlaybookCLI. Due to the fact that the options can't be empty # and must contain proper values we have not choice but extract # them from PlaybookCLI instance. playbook_cli = PlaybookCLI(['to-be-stripped', playbook]) playbook_cli.parse() options = playbook_cli.options # Get others required options. loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader, variable_manager) variable_manager.set_inventory(inventory) variable_manager.extra_vars = _get_user_settings(loader) # OpenStack Ansible deploys control plane services in LXC containers, # and use those as native hosts in its inventory. However, from # Kostyor's POV we are interested in baremetal node-by-node upgrade, # so we need to limit playbook execution only to baremetal node under # upgrade and its service's containers. inventory.subset([ host.get_vars()['inventory_hostname'] for host in _get_component_hosts_on_node(inventory, service, node) ]) # Finally, we can create a playbook executor and run the playbook. executor = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={}) executor.run()
def execute_action(self, action, args): """ Execute the requested operation """ C.DEFAULT_ROLES_PATH = [os.path.join(ROLESDIR, str(action))] i3xfce.loggers.ROOTLOGGER.debug("Executing the %s action", action) # Get the real user behind the sudo username = os.getenv("SUDO_USER") if username is None: i3xfce.loggers.ROOTLOGGER.debug("Unable to get SUDO_USER environment variable. This means i3-xfce has not been \ started using sudo") raise Exception("This program must be ran using sudo ") i3xfce.loggers.ROOTLOGGER.debug("Creating the option tuple") options_tuple = namedtuple('Options', ['connection', 'forks', 'module_path', 'become_user', 'become', 'become_method', 'check', 'verbosity']) try: # initialize needed objects variable_manager = VariableManager() variable_manager.extra_vars = dict(action=str(action), remote_user=username) loader = DataLoader() i3xfce.loggers.ROOTLOGGER.debug("Creating option to count number of tasks to execute") options = options_tuple(connection=None, module_path=None, forks=1, become_user=None, become=None, become_method=None, verbosity=0, check=True) tasks_count_callback = TaskCountCallback() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=None) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name="Ansible Play", hosts='localhost', gather_facts='no', ignore_errors="yes", roles=args.parts ) CmdLine._execute_play(play_source, inventory, variable_manager, loader, options, tasks_count_callback) i3xfce.loggers.ROOTLOGGER.debug("%i tasks are going to be executed", tasks_count_callback.get_total_tasks_num()) play_source["ignore_errors"] = "no" options = options_tuple(connection=None, module_path=None, forks=1, become_user=None, become=None, become_method=None, verbosity=0, check=args.dryrun) self._results_callback = PlaybookExecutionCallback(tasks_count_callback.get_total_tasks_num(), tasks_count_callback.get_task_name_max_len()) CmdLine._execute_play(play_source, inventory, variable_manager, loader, options, self._results_callback) self._results_callback.get_progress_bar().stop() self._results_callback.get_progress_bar().join() if self._results_callback.get_task_failed() is True: raise TaskExecutionException("") except TaskExecutionException as exc: raise except Exception as exc: raise TaskExecutionException(str(exc))
def run(host, user, passwd, tasks): Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='smart', module_path='none', forks=100, remote_user=user, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method="sudo", become_user="******", verbosity=None, check=False) passwords = {'conn_pass': passwd, 'become_pass': ""} inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host) variable_manager.set_inventory(inventory) host_list = [] for h in host: host_list.append(h.split(":")[0]) play_source = dict(name="Ansible Play", hosts=host_list, gather_facts='no', tasks=tasks) print(play_source) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None callback = ResultsCollector() try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) tqm._stdout_callback = callback tqm.run(play) finally: if tqm is not None: tqm.cleanup() return callback.host_ok.items(), callback.host_failed.items( ), callback.host_unreachable.items()
def execute(self, module_name, ansible_patt, ansible_args=None): loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.ansible_host_list) variable_manager.set_inventory(inventory) play_source = {} if ansible_args: play_source = { 'name': "AnsiApi Play", 'hosts': ansible_patt, 'gather_facts': 'no', 'tasks': [{ 'action': { 'module': module_name, 'args': ansible_args } }] } else: play_source = { 'name': "AnsiApi Play", 'hosts': ansible_patt, 'gather_facts': 'no', 'tasks': [{ 'action': { 'module': module_name } }] } play = Play.load(play_source, variable_manager=variable_manager, loader=loader) task_queue_manager = None callback = AnsiCallBack() try: task_queue_manager = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=self.passwords, stdout_callback=callback) task_queue_manager.run(play) finally: if task_queue_manager is not None: task_queue_manager.cleanup() return self.evaluate_results(callback)
class TaskRun(object): def __init__(self): self.variable_manager = VariableManager() self.loader = DataLoader() self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager) self.variable_manager.set_inventory(self.inventory) self.passwords = {} Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) self.options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=20, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user=None, verbosity=None, check=False) self.play_source = dict( name="Ansible Play", hosts=['10.58.56.211', '10.58.56.202'], gather_facts='no', tasks=[dict(action=dict(module='shell', args='ifconfig'))]) def shell_run(self): play = Play().load(self.play_source, variable_manager=self.variable_manager, loader=self.loader) tqm = None try: tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords, stdout_callback=results_callback, ) tqm.run(play) finally: if tqm is not None: tqm.cleanup() return results_callback.host_ok
class Runner: def __init__(self, resource): self.options = Options() self.options.connection = 'ssh' # Need a connection type "smart" or "ssh" self.options.become = True self.options.become_method = 'sudo' self.options.become_user = '******' # Become Pass Needed if not logging in as user root (do not support now) passwords = {'become_pass': ''} # Gets data from YAML/JSON files self.loader = DataLoader() # All the variables from all the various places self.variable_manager = VariableManager() # Set inventory, using most of above objects self.inventory = MyInventory(resource=resource, loader=self.loader, variable_manager=self.variable_manager) self.variable_manager.set_inventory(self.inventory) # set callback object self.results_callback = CallbackModule() # playbook self.tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=passwords, stdout_callback=self.results_callback, ) def run( self, module_name='shell', module_args='', ): play_source = dict( name='Ansible Play', hosts='*', gather_facts='no', tasks=[ dict(action=dict(module=module_name, args=module_args)), ]) self.play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader) try: ret = self.tqm.run(self.play) print(ret) return ret, self.results_callback.result finally: self.tqm.cleanup()
def execute_tasks(play_name, tasks, hosts, host_list=os.path.join(DIRNAME, 'ansible_hosts.py'), callback="default"): # NOTICE: Here is a trick. The host we acquired must in the host list # everytime. However I can't get the host list in advance. So I add the # hosts into the host list eveytime if it doesn't exist. if hosts not in ansible_hosts.hosts["all"]["hosts"]: ansible_hosts.hosts["all"]["hosts"].append(hosts) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict(name=play_name, hosts=hosts, gather_facts='no', tasks=tasks) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check' ]) options = Options(connection=None, module_path=None, forks=10, become=None, become_method=None, become_user=None, check=False) passwords = dict() # actually run it tqm = None try: tqm = TaskQueueManager(inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=callback) return tqm.run(play) finally: if tqm is not None: tqm.cleanup() pass
class Runner(object): def __init__(self, playbook, display, hosts=None, options={}, passwords={}, vault_pass=None): self.options = Options() for k, v in options.iteritems(): setattr(self.options, k, v) self.display = display self.display.verbosity = self.options.verbosity # executor has its own verbosity setting playbook_executor.verbosity = self.options.verbosity # gets data from YAML/JSON files self.loader = DataLoader() if vault_pass is not None: self.loader.set_vault_password(vault_pass) elif 'VAULT_PASS' in os.environ: self.loader.set_vault_password(os.environ['VAULT_PASS']) # all the variables from all the various places self.variable_manager = VariableManager() if self.options.python_interpreter is not None: self.variable_manager.extra_vars = { 'ansible_python_interpreter': self.options.python_interpreter } # set inventory, using most of above objects self.inventory = Inventory( loader=self.loader, variable_manager=self.variable_manager, host_list=hosts) if len(self.inventory.list_hosts()) == 0: self.display.error("Provided hosts list is empty.") sys.exit(1) self.inventory.subset(self.options.subset) if len(self.inventory.list_hosts()) == 0: self.display.error("Specified limit does not match any hosts.") sys.exit(1) self.variable_manager.set_inventory(self.inventory) # setup playbook executor, but don't run until run() called self.pbex = playbook_executor.PlaybookExecutor( playbooks=[playbook], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=passwords) def run(self): # run playbook and get stats self.pbex.run() stats = self.pbex._tqm._stats return stats
def exec_ansible(module, args, host, passwords=''): Options = namedtuple( 'Options', [ 'connection', 'remote_user', 'module_path', 'forks', 'become', # 'private_key_file', 'become_method', 'become_user', 'check' ]) variable_manager = VariableManager() loader = DataLoader() options = Options( connection='smart', remote_user='******', module_path=None, forks=5, become=True, # private_key_file='/var/www/html/CMDB/CMDB/id_rsa', become_method='sudo', become_user='******', check=None) passwords = dict(conn_pass=passwords) results_callback = ResultCallback() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts') variable_manager.set_inventory(inventory) play_source = dict(name="ansible test", hosts=host, gather_facts='no', tasks=[ dict(action=dict(module=module, args=args), register='shell_out'), ]) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=results_callback, ) result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() return json.loads(results_callback.data)
def main(): host_list = ['localhost', 'www.example.com', 'www.google.com'] Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='smart', module_path='/usr/share/ansible', forks=100, remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False) passwords = dict() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name="Ansible Play", hosts=host_list, gather_facts='no', tasks=[dict(action=dict(module='command', args=dict(cmd='/usr/bin/uptime')))] ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None callback = ResultsCollector() try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) tqm._stdout_callback = callback result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() print("UP ***********") for host, result in callback.host_ok.items(): print('{} >>> {}'.format(host, result._result['stdout'])) print("FAILED *******") for host, result in callback.host_failed.items(): print('{} >>> {}'.format(host, result._result['msg'])) print("DOWN *********") for host, result in callback.host_unreachable.items(): print('{} >>> {}'.format(host, result._result['msg']))
def main(args): # Options definition # Custom tuple to store playbook options Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check' ]) # Object initialization variable_manager = VariableManager() loader = DataLoader() options = Options(connection='ssh', module_path='library', forks=100, become=None, become_method=None, become_user=None, check=False) passwords = {} # Dinamyc inventory inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=args) # Inventory assignation variable_manager.set_inventory(inventory) # Play creation with tasks play_source = dict( name="Ansible Play", hosts=args, gather_facts='no', tasks=[ dict(action=dict(module='shell', args='hostname -f'), register='shell_out'), dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))) ]) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # Running it tqm = None try: tqm = TaskQueueManager(inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback='default') result = tqm.run(play) finally: if tqm is not None: tqm.cleanup()
def main(): host_list = ['localhost', 'www.example.com', 'www.google.com'] Options = namedtuple('Options', ['connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='smart', module_path='/usr/share/ansible', forks=100, remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False) passwords = dict() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name = "Ansible Play", hosts = host_list, gather_facts = 'no', tasks = [ dict(action=dict(module='command', args=dict(cmd='/usr/bin/uptime'))) ] ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None callback = ResultsCollector() try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) tqm._stdout_callback = callback result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() print "UP ***********" for host, result in callback.host_ok.items(): print '{} >>> {}'.format(host, result._result['stdout']) print "FAILED *******" for host, result in callback.host_failed.items(): print '{} >>> {}'.format(host, result._result['msg']) print "DOWN *********" for host, result in callback.host_unreachable.items(): print '{} >>> {}'.format(host, result._result['msg'])
def __launch_ansible_playbook(list_ip, playbook, extra_variable=None): """ Applies an Ansible playbook :param playbook: the playboo to be applied """ print extra_variable if not os.path.isfile(playbook): raise Exception('Requested playbook is not found - ' + playbook) if not playbook: logger.warn('Unable to find playbook - ' + playbook) variable_manager = VariableManager() if extra_variable is not None: variable_manager.extra_vars = extra_variable logger.info(extra_variable) else: logger.info('NO EXTRA VARS') loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=list_ip) variable_manager.set_inventory(inventory) options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'sftp_extra_args', 'scp_extra_args']) ansible_opts = options(listtags=None, listtasks=None, listhosts=None, syntax=False, connection='ssh', module_path=None, forks=100, remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, become=True, become_method='sudo', become_user=None, verbosity=11111, check=False, sftp_extra_args=None, scp_extra_args=None) logger.debug( 'Setting up Ansible Playbook Executor for playbook - ' + playbook) executor = PlaybookExecutor( playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=ansible_opts, passwords=None) logger.info('Executing Ansible Playbook - ' + playbook) ret = executor.run() if ret != 0: raise Exception('Error Executing Ansible Playbook - ' + playbook)
class PlayBook(object): def __init__(self, inventory='/etc/ansible/hosts', extra_vars=None, private_key_file=None): """ :param playbook: playbook.yml :param inventory: inventory file or script :type param extra_vars: dict :param private_key_file: ssh private key """ self.pbex = None self.options = Options(private_key_file=private_key_file, connection='smart', forks=10, timeout=10, verbosity=0, check=False, listtasks=False, listhosts=False, syntax=False, subset=None, module_path=None, become=None, become_user=None, become_method='sudo') # initialize needed objects self.loader = DataLoader() self.variable_manager = VariableManager() self.variable_manager.extra_vars = extra_vars self.variable_manager.options_vars = { 'ansible_check_mode': self.options.check } self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=inventory) self.variable_manager.set_inventory(self.inventory) # Limits inventory results to a subset of inventory that matches a given pattern self.inventory._subset = self.options.subset def run_playbook(self, playbook): self.pbex = PlaybookExecutor(playbooks=[playbook], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords={ 'conn_pass': None, 'become_pass': None }) self.pbex._tqm._stdout_callback = ResultCallback() return self.pbex.run() def run_play(self, play): pass
def ansible_run_api(inventory_in, tasks_list, input_options, input_passwd_dict, is_gather_facts): """ Ansible api to other method to call :param is_gather_facts: :param inventory_in: IP to connect to run task :param tasks_list: tasks to run :param input_options: ansible options, such as become,forks,remote_user etc :param input_passwd_dict: password for target :return: """ host_list = [inventory_in] app.logger.debug("ansible run\ninventory_in: {0}".format(inventory_in)) app.logger.debug(tasks_list) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = input_options passwords = input_passwd_dict # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict(name='ansible run', hosts='all', gather_facts=is_gather_facts, tasks=tasks_list) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None results_callback = ResultCallback() try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=results_callback, ) result = tqm.run(play) host_ok_result = tqm._stdout_callback.get_host_ok() finally: if tqm is not None: tqm.cleanup() return host_ok_result
def get_inventory(inventory='hosts'): if HAS_ANSIBLE2: loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager) variable_manager.set_inventory(inventory) else: inventory_filename = "%s/%s" % (ANSIBLE_DIR, inventory) inventory = Inventory(host_list=inventory_filename) return inventory
def main(args): # Options definition # Custom tuple to store playbook options Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check']) # Object initialization variable_manager = VariableManager() loader = DataLoader() options = Options(connection='ssh',module_path='library', forks=100, become=None, become_method=None, become_user=None, check=False) passwords = {} # Dinamyc inventory inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=args) # Inventory assignation variable_manager.set_inventory(inventory) # Play creation with tasks play_source = dict( name="Ansible Play", hosts=args, gather_facts='no', tasks=[ dict(action=dict(module='shell', args='hostname -f'), register='shell_out'), dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))) ] ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # Running it tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback='default' ) result = tqm.run(play) finally: if tqm is not None: tqm.cleanup()
def main(argv=sys.argv[1:]): # # initialize needed objects # variable_manager = VariableManager() loader = DataLoader() # https://pymotw.com/2/collections/namedtuple.html Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check']) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='local', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='******', verbosity=None, check=False) passwords = {} inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='./ls_hosts') variable_manager.set_inventory(inventory) playbook_path = './ls.yml' if not os.path.exists(playbook_path): print '[INFO] The playbook does not exist' sys.exit() # This can accomodate various other command line arguments.` # variable_manager.extra_vars = {'hosts': 'mywebserver'} pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) # cb = ResultAccumulator() # pbex._tqm._stdout_callback = cb results = pbex.run() if results != 0: print "ERROR"
class Runner(object): def __init__(self, hostnames, playbook, private_key_file, run_data, become_pass=None, verbosity=0, callback=None, subset_pattern=None): self.hostnames = hostnames self.playbook = os.path.join(playbooks_dir, playbook) self.run_data = run_data self.options = Options(subset=subset_pattern, private_key_file=private_key_file, verbosity=verbosity) self.display = Display() self.display.verbosity = verbosity playbook_executor.verbosity = verbosity passwords = {'become_pass': None} # Gets data from YAML/JSON files self.loader = DataLoader() self.loader.set_vault_password(os.environ.get('VAULT_PASS')) self.variable_manager = VariableManager() self.variable_manager.extra_vars = self.run_data self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=self.hostnames) self.variable_manager.set_inventory(self.inventory) self.pbex = playbook_executor.PlaybookExecutor( playbooks=[self.playbook], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=passwords) if callback: self.pbex._tqm._stdout_callback = callback def run(self): self.pbex.run() stats = self.pbex._tqm._stats run_success = True hosts = sorted(stats.processed.keys()) for h in hosts: t = stats.summarize(h) if t['unreachable'] > 0 or t['failures'] > 0: run_success = False return run_success
def main(argv=sys.argv[1:]): Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='local', module_path='/path/to/mymodules', forks=100, become=None, become_method=None, become_user=None, check=False) passwords = dict(vault_pass='******') # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost') variable_manager.set_inventory(inventory) # create play with tasks play_source = dict(name="Ansible Play", hosts='localhost', gather_facts='no', tasks=[ dict(action=dict(module='shell', args='uname -a'), register='shell_out'), dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))) ] ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None try: tqm = TaskQueueManager(inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback='default', ) result = tqm.run(play) print result finally: if tqm is not None: tqm.cleanup()
class AnsibleTask(object): def __init__(self, hosts='127.0.0.1', module='shell', args='ls -l', options=DEFAULT_OPTIONS, name='Ansible Play'): self.hosts = hosts self.module = module self.args = args self.options = options self.name = name self.task = dict(action=dict( module=self.module, args=self.args ), register='shell_out') self.tasks = [self.task] # initialize needed objects self.variable_manager = VariableManager() self.loader = DataLoader() self.passwords = dict() self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=[self.hosts]) self.variable_manager.set_inventory(self.inventory) self.play_source = dict( name=self.name, hosts=self.hosts, gather_facts='no', tasks=self.tasks ) self.play = Play().load(self.play_source, loader=self.loader, variable_manager=self.variable_manager) self.results_callback = ResultCallback() self.return_results = {} setattr(self.results_callback, 'task_obj', self) def ansible_play(self): # run it tqm = None try: tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords, stdout_callback=self.results_callback, # stdout_callback='default', ) result = tqm.run(self.play) finally: if tqm is not None: tqm.cleanup() #self.inventory.clear_pattern_cache() if result != 0: raise ValueError('SSH Command Failed, resturn: %s' % result) return self.return_results
def test_base(self): test_inv_dir = 'test/inventory' for inv in os.listdir(test_inv_dir): print "Processing ", inv res = dynlxc.main(os.path.join(test_inv_dir, inv), '') variable_manager = VariableManager() loader = DataLoader() self.mock_rv.communicate.return_value = [ json.dumps(res), 'mocked_err'] try: inventory = Inventory( loader=loader, variable_manager=variable_manager, host_list='inventory/dynlxc.py' ) except Exception as err: raise Exception("Inventory file {0} processing result '{1}' " "failed with {2}".format(inv, res, err)) variable_manager.set_inventory(inventory) play_source = dict(name="Ansible Play", hosts='localhost', gather_facts='no') playbook = os.path.abspath(os.path.join(test_inv_dir, '../playbooks', inv)) if os.path.isfile(playbook): with open(playbook) as fh: real_playbook = yaml.load(fh)[0] play_source.update(real_playbook) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=None, stdout_callback='default', ) result = tqm.run(play) assert result == 0, ("Ansible playbook exitcode " "different from 0") finally: if tqm is not None: tqm.cleanup()
def hosts_for_tag(cluster_config, tag): if not os.path.isfile(cluster_config): print("Hostfile does not exist {}".format(cluster_config)) sys.exit(1) variable_manager = VariableManager() loader = DataLoader() i = ansible.inventory.Inventory(loader=loader, variable_manager=variable_manager, host_list=cluster_config) variable_manager.set_inventory(i) group = i.get_group(tag) if group is None: return [] hosts = group.get_hosts() return [host.get_vars() for host in hosts]
def main(host_list,module,args): Options = namedtuple('Options', ['connection','module_path', 'forks', 'remote_user', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='smart', module_path='xxx', forks=50, remote_user='******', ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False) passwords = dict(sshpass=None, becomepass=None) # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name = "Ansible Play", hosts = host_list, gather_facts = 'no', tasks = [ dict(action=dict(module=module, args=args)) ] ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None callback = ResultsCollector() try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) tqm._stdout_callback = callback tqm.run(play) return callback.results[0]['tasks'][0]['hosts'] finally: if tqm is not None: tqm.cleanup()
def execute_tasks(play_name, tasks, hosts, host_list=os.path.join(DIRNAME, 'ansible_hosts.py'), callback="default"): # NOTICE: Here is a trick. The host we acquired must in the host list # everytime. However I can't get the host list in advance. So I add the # hosts into the host list eveytime if it doesn't exist. if hosts not in ansible_hosts.hosts["all"]["hosts"]: ansible_hosts.hosts["all"]["hosts"].append(hosts) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name=play_name, hosts=hosts, gather_facts='no', tasks=tasks) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check']) options = Options(connection=None, module_path=None, forks=10, become=None, become_method=None, become_user=None, check=False) passwords = dict() # actually run it tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=callback) return tqm.run(play) finally: if tqm is not None: tqm.cleanup() pass
def run(experimentid, playbook_path, host_list=C.DEFAULT_HOST_LIST): # print self.file_name # print self.path # print self.file.read() Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'host_key_checking']) variable_manager = VariableManager() loader = DataLoader() options = Options(connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user='******', verbosity=None, check=False, listhosts=None, listtasks=None, listtags=None, syntax=None, host_key_checking=False) passwords = dict(vault_pass='******') # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) #print host_list #print inventory.split_host_pattern(host_list) #inventory.parse_inventory(host_list) #print inventory.host_list #print inventory.get_hosts() variable_manager.set_inventory(inventory) # create the playbook executor, which manages running the plays via a task queue manager # playbook_path = self.file_name pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) # pbex._tqm._callback_plugins.append(C.DEFAULT_STDOUT_CALLBACK) # pbex._tqm._callback_plugins.append('default') # pbex._tqm._callback_plugins.append('default') # pbex._tqm._callback_plugins.append('default') pbex._tqm._callback_plugins.append(AnsibleCallbackModule(experimentid)) # pbex._tqm._callback_plugins.append(TreeCallbackModule()) pbex._playbooks results = pbex.run() # dont forget to clear host cache after finish, otherwise will raise exception when host_list changes in the new round inventory.clear_pattern_cache()
def hosts_for_tag(tag): hostfile = "provisioning_config" if not os.path.isfile(hostfile): print("File 'provisioning_config' not found at {}".format(os.getcwd())) sys.exit(1) variable_manager = VariableManager() loader = DataLoader() i = ansible.inventory.Inventory(loader=loader, variable_manager=variable_manager, host_list=hostfile) variable_manager.set_inventory(i) group = i.get_group(tag) if group is None: return [] hosts = group.get_hosts() return [host.get_vars() for host in hosts]
def playbook(**kwargs): group_vars = kwargs['group_vars'] groups = kwargs['groups'] host_list = kwargs['host_list'] playbook_basedir = os.sep.join([ANSIBLE_PLAYBOOK_PATH, kwargs['playbook_basedir']]) playbooks = [] for pb in kwargs['playbooks']: playbooks.append(os.sep.join([playbook_basedir, pb])) job_id = kwargs['job_id'] loader = DataLoader() vars = VariableManager() # 指定inventory为一个目录,设置所有主机,包含group和host invertory = Inventory(loader, vars, host_list=host_list) invertory.set_playbook_basedir(playbook_basedir) for group_name, hosts in groups.items(): t_group = Group(group_name) for host in hosts: t_host = Host(host) t_group.add_host(t_host) invertory.add_group(t_group) vars.set_inventory(invertory) display = LogDisplay(logname=job_id) callback = CALLBACKMODULE[CALLBACK](display=display) Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'timeout', 'remote_user', 'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'ask_value_pass', 'verbosity', 'check', 'listhosts', 'listtags', 'listtasks', 'syntax']) options = Options(connection='smart', module_path='/usr/share/ansible', forks=100, timeout=10, remote_user='******', ask_pass=False, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user='******', ask_value_pass=False, verbosity=None, check=False, listhosts=None, listtags=None, listtasks=None, syntax=None) passwords = dict() pb_executor = PlaybookExecutor(playbooks, invertory, vars, loader, options, passwords) pb_executor._tqm._stdout_callback = callback pb_executor.run() return display.get_log_json()
def __init__(self, inventory_file, module_name, module_args): loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_file) variable_manager.set_inventory(inventory) hosts = [x.name for x in inventory.get_hosts()] play_source = { "name": "Ansible Play", "hosts": hosts, "gather_facts": "no", "tasks": [{ "action": { "module": module_name, "args": module_args } }] } logging.info(play_source) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check']) options = Options(connection='local', module_path='', forks=100, become=None, become_method=None, become_user=None, check=False) self.inventory = inventory self.variable_manager = variable_manager self.loader = loader self.play = play self.options = options self.passwords = {"vault_pass": '******'}
class PlayBook(object): def __init__(self, playbook, inventory="inventory.py", extra_vars=None): self.playbook = "%s/%s" % (os.path.dirname(__file__), playbook) self.options = Options(inventory, extra_vars) self.loader = DataLoader() self.variable_manager = VariableManager() self.variable_manager.set_inventory(self.options.inventory) self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=self.options.inventory) self.pbex = PlaybookExecutor(playbooks=[self.playbook], inventory=self.inventory, loader=self.loader, variable_manager=self.variable_manager, options=self.options, passwords={'become_pass': None}) def run(self): self.pbex.run() stats = self.pbex._tqm._stats self.pbex._tqm.send_callback('human_log') return stats
def _execute_playbook(playbook_name): """ Execute a playbook stored in the *share_dir*. """ install_dir = os.path.dirname(os.path.dirname(sys.executable)) share_dir = os.path.join(install_dir, 'share', 'dws') playbook_path = os.path.join(share_dir, 'playbooks', playbook_name) if not os.path.exists(playbook_path): # When running directly from within src_dir. share_dir = os.path.join(install_dir, 'share') playbook_path = os.path.join(share_dir, 'playbooks', playbook_name) sysconf_dir = os.path.join(install_dir, 'etc') Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check']) options = Options(connection='local', module_path=os.path.dirname(tero.__file__), forks=100, become=None, become_method=None, become_user=None, check=False) passwords = dict(vault_pass='******') loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=os.path.join(sysconf_dir, 'ansible', 'hosts')) variable_manager.set_inventory(inventory) playbook = Playbook.load(playbook_path, variable_manager=variable_manager, loader=loader) tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) for play in playbook.get_plays(): result = tqm.run(play) finally: if tqm is not None: tqm.cleanup()
def run(self, hosts, username, password, playbook): Options = namedtuple('Options', ['connection', 'forks', 'module_path', 'ssh_common_args', 'sftp_extra_args', 'private_key_file', 'become', 'become_method', 'become_user', 'remote_user', 'ssh_extra_args', 'scp_extra_args', 'verbosity', 'check']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='smart', forks=20, module_path=None, ssh_common_args=None, sftp_extra_args=None, private_key_file='~/.ssh/id_rsa', become=True, become_method=None, become_user=username, remote_user=username, ssh_extra_args=None, scp_extra_args=None, verbosity=1, check=False) passwords = dict(conn_pass=password, become_pass=password) # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts) variable_manager.set_inventory(inventory) play = Play().load(playbook, variable_manager=variable_manager, loader=loader) # actually run it tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback='minimal' ) result = tqm.run(play) # print (result) finally: if tqm is not None: tqm.cleanup()
def launch_playbook_v2(self): ''' run ansible-playbook operations v2.X''' # create parser for CLI options parser = CLI.base_parser( usage="%prog playbook.yml", connect_opts=True, meta_opts=True, runas_opts=True, subset_opts=True, check_opts=True, inventory_opts=True, runtask_opts=True, vault_opts=True, fork_opts=True, module_opts=True, ) options, _ = parser.parse_args([]) sshpass = None if not options.become_user: options.become_user = "******" if self.pk_file: options.private_key_file = self.pk_file else: sshpass = self.passwd passwords = {'conn_pass': sshpass, 'become_pass': sshpass} if self.user: options.remote_user = self.user if not os.path.exists(self.playbook_file): raise errors.AnsibleError( "the playbook: %s could not be found" % self.playbook_file) if not os.path.isfile(self.playbook_file): raise errors.AnsibleError( "the playbook: %s does not appear to be a file" % self.playbook_file) variable_manager = VariableManager() variable_manager.extra_vars = self.extra_vars if self.inventory_file: options.inventory = self.inventory_file options.forks = self.threads loader = DataLoader() # Add this to avoid the Ansible bug: no host vars as host is not in inventory # In version 2.0.1 it must be fixed ansible.inventory.HOSTS_PATTERNS_CACHE = {} inventory = ansible.inventory.Inventory( loader=loader, variable_manager=variable_manager, host_list=options.inventory) variable_manager.set_inventory(inventory) if self.host: inventory.subset(self.host) # let inventory know which playbooks are using so it can know the # basedirs inventory.set_playbook_basedir(os.path.dirname(self.playbook_file)) num_retries = 0 return_code = 4 results = None while return_code != 0 and num_retries < self.retries: time.sleep(5 * num_retries) num_retries += 1 return_code = 0 try: # create the playbook executor, which manages running the plays # via a task queue manager pbex = IMPlaybookExecutor(playbooks=[self.playbook_file], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, output=self.output) return_code = pbex.run() except errors.AnsibleError, e: display("ERROR: %s" % e, output=self.output) return_code = 1 if return_code != 0: display("ERROR executing playbook (%s/%s)" % (num_retries, self.retries), output=self.output)
class Transport: """ Transport using Ansible. """ def __init__(self): """ Creates an instance of the Transport. """ self.logger = logging.getLogger('transport') self.Options = namedtuple( 'Options', ['connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check']) # initialize needed objects self.variable_manager = VariableManager() self.loader = DataLoader() self.passwords = {} def _run(self, ip, key_file, play_source, expected_results=[0]): """ Common code used for each run. :param ip: IP address to check. :type ip: str :param key_file: Full path the the file holding the private SSH key. :type key_file: string :param play_source: Ansible play. :type play_source: dict :param expected_results: List of expected return codes. Default: [0] :type expected_results: list :returns: Ansible exit code :type: int """ ssh_args = ('-o StrictHostKeyChecking=no -o ' 'ControlMaster=auto -o ControlPersist=60s') options = self.Options( connection='ssh', module_path=None, forks=1, remote_user='******', private_key_file=key_file, ssh_common_args=ssh_args, ssh_extra_args=ssh_args, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False) # create inventory and pass to var manager inventory = Inventory( loader=self.loader, variable_manager=self.variable_manager, host_list=ip) # TODO: Fix this ... weird but works group = Group(ip) group.add_host(Host(ip, 22)) inventory.groups.update({ip: group}) # --- self.variable_manager.set_inventory(inventory) play = Play().load( play_source, variable_manager=self.variable_manager, loader=self.loader) # actually run it tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=self.variable_manager, loader=self.loader, options=options, passwords=self.passwords, stdout_callback=LogForward(), ) result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() if result in expected_results: self.logger.debug('{0}: Good result {1}'.format(ip, result)) fact_cache = self.variable_manager._fact_cache.get(ip, {}) return (result, fact_cache) # TODO: Do something :-) self.logger.debug('{0}: Bad result {1}'.format(ip, result)) raise Exception('Can not run for {0}'.format(ip)) def upgrade(self, ip, key_file, oscmd): """ Upgrades a host via ansible. :param ip: IP address to upgrade. :type ip: str :param key_file: Full path the the file holding the private SSH key. :param oscmd: OSCmd instance to use :type oscmd: commissaire.oscmd.OSCmdBase :type key_file: str :returns: tuple -- (exitcode(int), facts(dict)). """ # TODO: Use ansible to do multiple hosts... play_source = { 'name': 'upgrade', 'hosts': ip, 'gather_facts': 'no', 'tasks': [{ 'action': { 'module': 'command', 'args': " ".join(oscmd.upgrade()) } }] } return self._run(ip, key_file, play_source) def restart(self, ip, key_file, oscmd): """ Restarts a host via ansible. :param ip: IP address to reboot. :type ip: str :param key_file: Full path the the file holding the private SSH key. :type key_file: str :param oscmd: OSCmd instance to use :type oscmd: commissaire.oscmd.OSCmdBase :returns: tuple -- (exitcode(int), facts(dict)). """ # TODO: Use ansible to do multiple hosts... play_source = { 'name': 'reboot', 'hosts': ip, 'gather_facts': 'no', 'tasks': [{ 'action': { 'module': 'command', 'args': " ".join(oscmd.restart()) } }] } return self._run(ip, key_file, play_source, [0, 2]) def get_info(self, ip, key_file): """ Get's information from the host via ansible. :param ip: IP address to check. :type ip: str :param key_file: Full path the the file holding the private SSH key. :type key_file: str :returns: tuple -- (exitcode(int), facts(dict)). """ # create play with tasks play_source = { 'name': 'gather', 'hosts': ip, 'gather_facts': 'yes', 'tasks': [] } result, fact_cache = self._run(ip, key_file, play_source) facts = {} facts['os'] = fact_cache['ansible_distribution'].lower() facts['cpus'] = fact_cache['ansible_processor_cores'] facts['memory'] = fact_cache['ansible_memory_mb']['real']['total'] space = 0 for x in fact_cache['ansible_mounts']: space += x['size_total'] facts['space'] = space # Special case for atomic: Since Atomic doesn't advertise itself and # instead calls itself 'redhat' we need to check for 'atomicos' # in other ansible_cmdline facts if facts['os'] == 'redhat': boot_image = fact_cache.get( 'ansible_cmdline', {}).get('BOOT_IMAGE', '') root_mapper = fact_cache.get('ansible_cmdline', {}).get('root', '') if (boot_image.startswith('/ostree/rhel-atomic-host') or 'atomicos' in root_mapper): facts['os'] = 'atomic' return (result, facts) def bootstrap(self, ip, key_file, config, oscmd): """ Bootstraps a host via ansible. :param ip: IP address to reboot. :type ip: str :param key_file: Full path the the file holding the private SSH key. :type key_file: str :param config: Configuration information. :type config: commissaire.config.Config :param oscmd: OSCmd instance to useS :type oscmd: commissaire.oscmd.OSCmdBase :returns: tuple -- (exitcode(int), facts(dict)). """ # TODO: Use ansible to do multiple hosts... self.logger.debug('Using {0} as the oscmd class for {1}'.format( oscmd.os_type, ip)) # TODO: I'd love to use ansibles "template" but it, as well as copy # always fails when used in tasks in 2.0.0.2. # Fill out templates tpl_loader = jinja2.loaders.FileSystemLoader( resource_filename('commissaire', 'data/templates/')) tpl_vars = { 'bootstrap_ip': ip, 'kubernetes_api_server_host': config.kubernetes['uri'].hostname, 'kubernetes_api_server_port': config.kubernetes['uri'].port, 'kubernetes_bearer_token': config.kubernetes['token'], 'docker_registry_host': '127.0.0.1', # TODO: Where do we get this? 'docker_registry_port': 8080, # TODO: Where do we get this? 'etcd_host': config.etcd['uri'].hostname, 'etcd_port': config.etcd['uri'].port, 'flannel_key': '/atomic01/network' # TODO: Where do we get this? } tpl_env = jinja2.Environment() configs = {} for tpl_name in ( 'docker', 'flanneld', 'kubelet', 'kube_config', 'kubeconfig'): f = tempfile.NamedTemporaryFile(prefix=tpl_name, delete=False) f.write(tpl_loader.load(tpl_env, tpl_name).render(tpl_vars)) f.close() configs[tpl_name] = f.name # --- play_source = { 'name': 'bootstrap', 'hosts': ip, 'gather_facts': 'no', 'tasks': [ { 'name': 'Install Flannel', 'action': { 'module': 'command', 'args': " ".join(oscmd.install_flannel()), } }, { 'name': 'Configure Flannel', 'action': { 'module': 'synchronize', 'args': { 'dest': oscmd.flanneld_config, 'src': configs['flanneld'], } } }, { 'name': 'Enable and Start Flannel', 'action': { 'module': 'service', 'args': { 'name': oscmd.flannel_service, 'enabled': 'yes', 'state': 'started', } } }, { 'name': 'Install Docker', 'action': { 'module': 'command', 'args': " ".join(oscmd.install_docker()), } }, { 'name': 'Configure Docker', 'action': { 'module': 'synchronize', 'args': { 'dest': oscmd.docker_config, 'src': configs['docker'], } } }, { 'name': 'Enable and Start Docker', 'action': { 'module': 'service', 'args': { 'name': oscmd.docker_service, 'enabled': 'yes', 'state': 'started', } } }, { 'name': 'Install Kubernetes Node', 'action': { 'module': 'command', 'args': " ".join(oscmd.install_kube()), } }, { 'name': 'Configure Kubernetes Node', 'action': { 'module': 'synchronize', 'args': { 'dest': oscmd.kubernetes_config, 'src': configs['kube_config'], } } }, { 'name': 'Add Kubernetes kubeconfig', 'action': { 'module': 'synchronize', 'args': { 'dest': oscmd.kubernetes_kubeconfig, 'src': configs['kubeconfig'], } } }, { 'name': 'Configure Kubernetes kubelet', 'action': { 'module': 'synchronize', 'args': { 'dest': oscmd.kubelet_config, 'src': configs['kubelet'], } } }, { 'name': 'Enable and Start Kubelet', 'action': { 'module': 'service', 'args': { 'name': oscmd.kubelet_service, 'enabled': 'yes', 'state': 'started', } } }, { 'name': 'Enable and Start Kube Proxy', 'action': { 'module': 'service', 'args': { 'name': oscmd.kubelet_proxy_service, 'enabled': 'yes', 'state': 'started', } } }, ] } results = self._run(ip, key_file, play_source, [0]) # Clean out the temporary configs map(os.unlink, configs.values()) return results
Options = namedtuple('Options', ['connection', 'forks', 'module_path', 'ssh_common_args', 'sftp_extra_args', 'private_key_file', 'become', 'become_method', 'become_user', 'remote_user', 'ssh_extra_args', 'scp_extra_args', 'verbosity', 'check']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connecton='smart', forks=20, module_path=None, ssh_common_args=None, sftp_extra_args=None, private_key_file='~/.ssh/id_rsa', become=True, become_method=None, become_user=username, remote_user=username, ssh_extra_args=None, scp_extra_args=None, verbosity=1, check=False) passwords = dict(conn_pass=password, become_pass=password) # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name="Bin Push to All Hosts", hosts=hosts, gather_facts='no', remote_user=username, tasks=[ dict(action=dict(module='authorized_key', args=dict(user=username, state='present', key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"))), dict(action=dict(module='synchronize', args=dict(mode='push', checksum='yes', src=src, dest=dest, rsync_opts=['--exclude=refx86.sh']))) ] )
import json,sys,os from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.playbook.play import Play from ansible.executor.task_queue_manager import TaskQueueManager from ansible.executor.playbook_executor import PlaybookExecutor #from ansible.plugins.callback import CallbackBase,call_json #,log_plays BASE_DIR = os.path.dirname(os.path.abspath(__file__)) File_PATH = os.path.join(BASE_DIR,'out.txt') loader = DataLoader() # 用来加载解析yaml文件或JSON内容,并且支持vault的解密 variable_manager = VariableManager() # 管理变量的类,包括主机,组,扩展等变量,之前版本是在 inventory 中的 inventory = Inventory(loader=loader, variable_manager=variable_manager) variable_manager.set_inventory(inventory) # 根据 inventory 加载对应变量 #初始化Options Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check','listhosts','listtasks','listtags','syntax']) options = Options(connection='smart', module_path='/usr/lib/python2.7/site-packages/ansible', forks=100, become=None, become_method=None, become_user='******', check=False,listhosts=None,listtasks=None,listtags=None,syntax=None) def my_runner(host_list,module_name,module_args): variable_manager.extra_vars={} # 增加外部变量 # 构建pb, 这里很有意思, 新版本运行ad-hoc或playbook都需要构建这样的pb, 只是最后调用play的类不一样 # :param name: 任务名,类似playbook中tasks中的name # :param hosts: playbook中的hosts # :param tasks: playbook中的tasks, 其实这就是playbook的语法, 因为tasks的值是个列表,因此可以写入多个task play_source = {"name":"Ansible Ad-Hoc","hosts":host_list,"gather_facts":"no","tasks":[{"action":{"module":module_name,"args":module_args}}]} play = Play().load(play_source,variable_manager=variable_manager,loader=loader) tqm = None # results_callback = call_json.CallbackModule()