import osimport sysimport ansiblefrom collections import namedtuple from ansible.parsing.dataloader import DataLoaderfrom ansible.vars import VariableManagerfrom ansible.inventory import Inventoryfrom ansible.executor.playbook_executor import PlaybookExecutor def playbook_action(playbook, hosts): variable_manager = VariableManager() loader = DataLoader() inventory = ansible.inventory.Inventory(loader=loader,variable_manager=variable_manager) inventory.clear_pattern_cache() 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=‘ssh‘, module_path=None, 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) variable_manager.extra_vars = {‘host‘: hosts} passwords = {} pbex = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) pbex.run()
时间: 2024-10-10 06:32:38