def __init__(self, inventory=None, extra_vars=None): """Imitates Ansible Inventory Loader. Args: inventory (str): Path to Ansible Inventory files. extra_vars (dict): Extra Vars passed at run time. """ self.inventory = inventory self.loader = DataLoader() self.inv_mgr = InventoryManager(loader=self.loader, sources=self.inventory) self.var_mgr = VariableManager(loader=self.loader, inventory=self.inv_mgr) # TODO As of Ansible==2.8.0 the extra_vars property cannot be set to VariableManager # This needs to be investigated and fixed properly self.extra_vars = extra_vars or dict()
def __init__(self, group, key, callback): self.options = Options(connection='smart', module_path='', forks=100, become=None, become_method=None, become_user='******', check=False, private_key_file=key, diff=False) self.key = key self.make_inventory(group) self.stdout_callback = callback self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) self.play = []
def ansible_runner_24x(playbook_path, extra_vars, options=None, inventory_src='localhost', console=True): loader = DataLoader() variable_manager = VariableManager(loader=loader) variable_manager.extra_vars = extra_vars inventory = Inventory(loader=loader, sources=[inventory_src]) variable_manager.set_inventory(inventory) passwords = {} pbex = PlaybookExecutor([playbook_path], inventory, variable_manager, loader, options, passwords) return pbex
def test_basic_manager(self): fake_loader = DictDataLoader({}) mock_inventory = MagicMock() v = VariableManager(loader=fake_loader, inventory=mock_inventory) vars = v.get_vars(use_cache=False) # FIXME: not sure why we remove all and only test playbook_dir for remove in [ 'omit', 'vars', 'ansible_version', 'ansible_check_mode', 'ansible_playbook_python' ]: if remove in vars: del vars[remove] self.assertEqual(vars, dict(playbook_dir=os.path.abspath('.')))
def __init__(self): context.CLIARGS = ImmutableDict(connection="paramiko_ssh", remote_user="******", private_key_file="~/.ssh/id_rsa", timeout=3, forks=20, syntax=None, listhosts=None, listtags=None, listtasks=None) self.loader = DataLoader() self.inventory = InventoryManager( loader=self.loader, sources=[ANSIBLE_INVENTORY_DEFAULT_PATH]) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
def test_flush_cache(self): cli = PlaybookCLI( args=["ansible-playbook", "--flush-cache", "foobar.yml"]) cli.parse() self.assertTrue(cli.options.flush_cache) variable_manager = VariableManager() fake_loader = DictDataLoader({'foobar.yml': ""}) inventory = InventoryManager(loader=fake_loader, sources='testhost,') variable_manager.set_host_facts(inventory.get_host('testhost'), {'canary': True}) self.assertTrue('testhost' in variable_manager._fact_cache) cli._flush_cache(inventory, variable_manager) self.assertFalse('testhost' in variable_manager._fact_cache)
def reset(self): if self.tqm is not None: self.tqm.cleanup() self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) result_callback = ResultCallback() self.tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self, passwords=None, stdout_callback=result_callback, ) self.results = result_callback.results
def ansible_runner_2x(playbook_path, module_path, extra_var, inventory_src='localhost', console=True): variable_manager = VariableManager() loader = DataLoader() extra_var["ansible_python_interpreter"] = sys.executable variable_manager.extra_vars = extra_var inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_src) passwords = {} 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=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=False, become_method='sudo', become_user='******', verbosity=0, check=False) pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) return pbex
def run_play_book(palyname, yml_file, sources, forks, myvars): if ',' not in sources: # let it NOT be a filepath sources = sources + ',' # initialize needed objects 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', 'diff' ]) 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, diff=False) passwords = {} # create inventory and pass to var manager inventory = InventoryManager(loader=loader, sources=sources) variable_manager = VariableManager(loader=loader, 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) rc = pbex.run() return {'rc': rc, 'detail': pbex._tqm._stdout_callback.std_lines}
def __init__(self, sources='/etc/ansible/hosts', *args, **kwargs): self.sources = sources self.variable_manager = None self.loader = None self.options = None self.passwords = None self.callback = ResultCallback() self.results_raw = {} 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', 'listtasks', 'listtags', 'syntax', 'diff' ]) self.options = Options(connection='smart', module_path=None, 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=False, listtasks=False, listtags=False, syntax=False, diff=False) self.loader = DataLoader() self.passwords = dict(vault_pass='******') self.inventory = InventoryManager(self.loader, self.sources) # 把inventory传递给variable_manager管理 self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
def adhoc(sources=None, hosts=None, module=None, args=None): Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff' ]) options = Options(connection='smart', module_path=['/to/mymodules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False) loader = DataLoader() passwords = dict(vault_pass='******') inventory = InventoryManager(loader=loader, sources=sources) variable_manager = VariableManager(loader=loader, inventory=inventory) play_source = dict( name="Ansible Play", hosts=hosts, gather_facts='no', tasks=[ dict(action=dict(module=module, args=args), 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) tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) tqm.run(play) finally: if tqm is not None: tqm.cleanup() shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
def execute(playbooks: list, context: dict): ''' ''' assert playbooks os.environ["ANSIBLE_CONDITIONAL_BARE_VARS"] = "False" # ctx._init_global_context({}) # since the API is constructed for CLI it expects certain options to always be set in the context object ctx.CLIARGS = ImmutableDict(connection='local', module_path=['./playbooks/roles'], forks=1, become=False, become_method="sudo", syntax=False, start_at_task=None, diff=False, verbosity=0) # initialize needed objects loader = DataLoader( ) # Takes care of finding and reading yaml, json and ini files # passwords = dict(become_pass="******") passwords = dict() # Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets results_callback = ResultCallback() # create inventory, use path to host config file as source or hosts in a comma separated string inventory = InventoryManager(loader=loader, sources='localhost,') host = Host(name="localhost") # variable manager takes care of merging all the different sources to give you a unified view of variables available in each context variable_manager = VariableManager(loader=loader, inventory=inventory) variable_manager.set_host_variable(host, "current_directory", context.get("current_directory", "")) pbex = PlaybookExecutor(playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, passwords=passwords) result = pbex.run() post_command = variable_manager.get_vars()["hostvars"]["localhost"].get( "mondrik_post_command") return result, post_command
def fw_configure(self, fw_vnfr): fw_cpinput_ip = '10.30.0.2' fw_cpinput_netmask = '255.255.255.252' fw_cpinput_network = '10.30.0.2/30' # configure vm using ansible playbook loader = DataLoader() with tempfile.NamedTemporaryFile() as fp: fp.write(b'[firewallserver]\n') if self.is_running_in_emulator: fp.write(b'mn.vnf_fw') else: fp.write(mgmt_ip.encode('utf-8')) fp.flush() inventory = InventoryManager(loader=loader, sources=[fp.name]) variable_manager = VariableManager(loader=loader, inventory=inventory) playbook_path = os.path.abspath('./ansible/site.yml') LOG.debug('Targeting the ansible playbook: %s', playbook_path) if not os.path.exists(playbook_path): LOG.error('The playbook does not exist') return False 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', 'diff']) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, 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=True, become_method=None, become_user='******', verbosity=None, check=False, diff=True) options = options._replace(connection='docker', become=False) variable_manager.extra_vars = {'FW_CPINPUT_NETWORK': fw_cpinput_network, 'SON_EMULATOR': self.is_running_in_emulator } pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={}) results = pbex.run() return True
def exec_tasks(request): module_name = request.POST['module'] args_name = request.POST['args'] print(module_name, args_name) hostname = request.POST.get('host') Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff' ]) options = Options(connection='ssh', module_path=['/to/mymodules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False) loader = DataLoader() passwords = dict() inventory = InventoryManager(loader=loader, sources=['web_ansi/dhosts.py']) variable_manager = VariableManager(loader=loader, inventory=inventory) play_source = dict( name="Ansible Play", hosts=hostname, gather_facts='no', tasks=[ dict(action=dict(module=module_name, args=args_name)), ]) 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, ) result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() shutil.rmtree(C.DEFAULT_LOCAL_TMP, True) return redirect(tasks)
def __init__(self): self.options = { 'verbosity': 0, 'ask_pass': False, 'private_key_file': None, 'remote_user': None, 'connection': 'smart', 'timeout': 10, 'ssh_common_args': '', 'sftp_extra_args': '', 'scp_extra_args': '', 'ssh_extra_args': '', 'force_handlers': False, 'flush_cache': None, 'become': False, 'become_method': 'sudo', 'become_user': None, 'become_ask_pass': False, 'tags': ['all'], 'skip_tags': [], 'check': False, 'syntax': None, 'diff': False, 'inventory': '/etc/ansible/hosts', 'listhosts': None, 'subset': None, 'extra_vars': [], 'ask_vault_pass': False, 'vault_password_files': [], 'vault_ids': [], 'forks': 5, 'module_path': None, 'listtasks': None, 'listtags': None, 'step': None, 'start_at_task': None, 'args': ['fake'] } self.ops = Values(self.options) self.loader = DataLoader() self.passwords = dict() self.results_callback = ResultCallback() self.inventory = InventoryManager(loader=self.loader, sources=[self.options['inventory']]) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) self.variable_manager._extra_vars = {'hosts': 'ali'}
def exec_task(servers, mod, arg): Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff' ]) options = Options(connection='smart', module_path=['/to/mymodule'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False) loader = DataLoader() passwords = dict() inventory = InventoryManager(loader=loader, sources=['ansicfg/dhosts.py']) variable_manager = VariableManager(loader=loader, inventory=inventory) play_source = dict(name="Ansible Play", hosts=servers, gather_facts='no', tasks=[ dict(action=dict(module=mod, args=arg), 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, ) result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
def run_playbook(self, request_id, connection_type, inventory_path, playbook_path, lifecycle, all_properties): Options = namedtuple('Options', [ 'connection', 'forks', 'become', 'become_method', 'become_user', 'listhosts', 'listtasks', 'listtags', 'syntax', 'module_path', 'check', 'diff' ]) # initialize needed objects loader = DataLoader() options = Options(connection=connection_type, listhosts=None, listtasks=None, listtags=None, syntax=None, module_path=None, become=None, become_method='sudo', become_user='******', check=False, diff=False, forks=20) passwords = {'become_pass': ''} # create inventory and pass to var manager inventory = InventoryManager(loader=loader, sources=inventory_path) variable_manager = VariableManager(loader=loader, inventory=inventory) variable_manager.extra_vars = all_properties # Setup playbook executor, but don't run until run() called pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) callback = ResultCallback(self.ansible_properties, request_id, lifecycle) pbex._tqm._stdout_callback = callback logger.debug( "Running playbook {0} with properties {1}, system_properties {2}". format(playbook_path, all_properties['properties'].get_props(), all_properties['system_properties'].get_props())) pbex.run() logger.debug("Playbook finished {0}".format(playbook_path)) return callback
def __init__(self, hosts, extra_vars=None,timeout=None): Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'timeout', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'extra_vars', 'playbook_path', 'passwords', 'diff', 'gathering', 'remote_tmp', ]) self.hosts = hosts self._validate() self.hosts_file = None self.timeout = 100 if not timeout else timeout self.options = Options( listtags=False, listtasks=False, listhosts=False, syntax=False, timeout=self.timeout, connection='ssh', module_path='', forks=10, remote_user='******', private_key_file='/root/.ssh/id_rsa', ssh_common_args="", ssh_extra_args="", sftp_extra_args="", scp_extra_args="", become=None, become_method=None, become_user=None, verbosity=None, extra_vars=[], check=False, playbook_path='', passwords=None, diff=False, gathering='implicit', remote_tmp='/tmp/.ansible' ) self.loader = DataLoader() self.inventory = InventoryManager(loader=self.loader, sources=[','.join(self.hosts)+',']) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) if extra_vars: self.variable_manager.extra_vars = extra_vars logger.info(self.options)
def __init__(self): self.Options = namedtuple('Options', ['connection', 'remote_user', 'ask_sudo_pass', 'verbosity', 'ack_pass', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'sudo_user', 'sudo', 'diff']) self.ops = self.Options(connection='smart', remote_user='******', # remote_user=None, ack_pass=None, sudo_user=None, forks=5, sudo=None, ask_sudo_pass=False, verbosity=5, module_path=None, become=None, become_method=None, become_user=None, check=False, diff=False, listhosts=None, listtasks=None, listtags=None, syntax=None) self.loader = DataLoader() self.passwords = dict() self.results_callback = ResultCallback() self.inventory = InventoryManager(loader=self.loader, sources=['/usr/local/ansible/hosts']) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
def __init__(self, sources='conf/ansible/hosts', name="ansible Play", hosts=None, actions=None): ''' 创建参数,为保证每个参数都被设置,ansible使用可命名元组 ''' self.Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff' ]) self.options = self.Options(connection='ssh', module_path=['/to/mymodules'], forks=30, become=None, become_method=None, become_user=None, check=False, diff=False) '''初始化loader类''' self.loader = DataLoader() # 用于读取与解析yaml和json文件 self.passwords = dict(vault_pass='******') '''初始化结果回调方法,用于接收返回的结果''' self.results_callback = ResultCallback() '''指定inventory,即我们的ansible hosts文件,使用路径指定一个host文件,或者一个逗号分割host的字符串''' self.inventory = InventoryManager(loader=self.loader, sources=sources) '''合并所有不同的资源成一个统一的变量管理视图,这些由变量管理器完成''' self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) '''play_source 创建我们任务的数据结构,tasks里面就是我们定义的yaml文件所做的步骤''' if actions == None: print("no actions") raise AnsibleError if hosts == None: print("no hosts") raise AnsibleError self.play_source = dict( name=name, hosts=hosts, gather_facts='no', tasks=actions, #dict(action=dict(module='shell', args='ls'), register='shell_out'), #dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))) )
def __init__(self, resource=None, sources=None, sock=None, **kwargs): Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'timeout', 'remote_user', 'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'strategy', 'scp_extra_args', 'become', 'become_method', 'become_user', 'ask_value_pass', 'verbosity', 'retry_files_enabled', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'diff', 'gathering', 'roles_path' ]) self.options = Options( connection='smart', module_path=None, forks=50, timeout=10, remote_user=kwargs.get('remote_user', None), ask_pass=False, #private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, strategy='free', scp_extra_args=None, become=kwargs.get('become', None), become_method=kwargs.get('become_method', None), become_user=kwargs.get('become_user', None), ask_value_pass=False, verbosity=None, retry_files_enabled=False, check=False, listhosts=False, listtasks=False, listtags=False, syntax=False, diff=True, gathering='smart', roles_path=settings.ANSIBLE_ROLE_PATH, private_key_file='/home/aron/.ssh/id_rsa') self.loader = DataLoader() self.inventory = MyInventory(resource=resource, loader=self.loader, sources=sources) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) self.passwords = dict(sshpass=None, becomepass=None) self.callback = None self.sock = sock
def __init__(self, connection=C.DEFAULT_TRANSPORT, inventory=C.DEFAULT_HOST_LIST, basedir=None, forks=10, stdout_callback=C.DEFAULT_STDOUT_CALLBACK, remote_user=C.DEFAULT_REMOTE_USER): self.connection = connection self.remote_user = remote_user self.module_path = [] self.forks = forks self.become = None self.become_method = None self.become_user = None self.check = False self.diff = False self.loader = DataLoader() if basedir: self.loader.set_basedir(basedir) self.inventory = InventoryManager(loader=self.loader, sources=inventory) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) if basedir: self.variable_manager.safe_basedir = True if stdout_callback is None: stdout_callback = 'null' tqm = TaskQueueManager(inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self, passwords=None, stdout_callback=stdout_callback, run_additional_callbacks=False) result_callback = ResultCallback() tqm._callback_plugins.append(result_callback) @atexit.register def cleanup_tqm(): tqm.cleanup() self.tqm = tqm self.results = result_callback.results
def autocreate_publickey(host_list, password, jobid): C.HOST_KEY_CHECKING = False loader = DataLoader() options = Options(connection='smart', forks=5, ssh_common_args='-C -o ControlPersist=30s') passwords = dict() if len(host_list) <= 1: sources = host_list[0] + ',' else: sources = ','.join(host_list) inventory = InventoryManager(loader=loader, sources=sources) variable_manager = VariableManager(loader=loader, inventory=inventory) for host in host_list: host_info = Host(name=host, port=22) variable_manager.set_host_variable(host_info, 'ansible_ssh_user', 'root') variable_manager.set_host_variable(host_info, 'ansible_ssh_pass', password) play_source = dict( name='Generate Publickey', hosts=host_list, gather_facts='no', tasks=[ dict(action=dict( module='authorized_key', args=dict(user='******', key="{{ lookup('file','/root/.ssh/id_rsa.pub') }}"))) ]) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None callback = ResultsCollector(jobid) 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() return callback.result
def __init__(self): # Leverage the ansible python api # to run a playbook against a molecule host. # # see: ansible python api # https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html try: self._molecule_ephemeral_directory = \ Path(os.environ['MOLECULE_EPHEMERAL_DIRECTORY']) self._molecule_scenario_directory = \ Path(os.environ['MOLECULE_SCENARIO_DIRECTORY']) except KeyError: # return None if we can't access the molecule environment variables return None # create symlink in molecule ephemeral directory # to roles directory in project dir self._create_symlink_('roles') # use molecule managed inventory inventory_file = self._molecule_ephemeral_directory / \ 'inventory/ansible_inventory.yml' # FIXME: add TESTAID_EXTRA_VARS_FILES # inject extra_vars into ansible play with high weight self._extra_vars = dict() context.CLIARGS = ImmutableDict(connection='local', module_path=[''], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False) self._loader = DataLoader() self._inventory = InventoryManager(loader=self._loader, sources=str(inventory_file)) self._variable_manager = VariableManager(loader=self._loader, inventory=self._inventory) # use inventory host host = next(iter(self._inventory.hosts)) # create a Host object self._host = Host(name=host)
def playbook_execution(self, playbook, host_ip): LOG.info("Executing playbook: %s", playbook) loader = DataLoader() inventory = None with tempfile.NamedTemporaryFile() as fp: fp.write(host_ip.encode('utf-8')) fp.flush() inventory = InventoryManager(loader=loader, sources=[fp.name]) variable_manager = VariableManager(loader = loadder, inventory = inventory) if not os.path.exists(playbook): LOG.error('The playbook %s does not exist', playbook) return 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 = '******', 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) variable_manager.extra_vars = {'hosts': host_ip} passwords = {} pbex = PlaybookExecutor(playbooks = [playbook], inventory = inventory, variable_manager = variable_manager, loader = loader, options = options, passwords = passwords) results = pbex.run() return
def run_playbook(self, request_id, connection_type, inventory_path, playbook_path, lifecycle, all_properties): Options = namedtuple('Options', [ 'connection', 'forks', 'become', 'become_method', 'become_user', 'listhosts', 'listtasks', 'listtags', 'syntax', 'module_path', 'check', 'diff' ]) # initialize needed objects loader = DataLoader() context.CLIARGS = ImmutableDict(connection=connection_type, module_path=None, forks=20, become=None, become_method='sudo', become_user='******', check=False, diff=False, listhosts=None, listtasks=None, listtags=None, syntax=None, start_at_task=None, verbosity=1) passwords = {'become_pass': ''} # create inventory and pass to var manager inventory = InventoryManager(loader=loader, sources=inventory_path) variable_manager = VariableManager(loader=loader, inventory=inventory) variable_manager._extra_vars = all_properties # Setup playbook executor, but don't run until run() called pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, passwords=passwords) callback = ResultCallback(self.ansible_properties, request_id, lifecycle, self.event_logger) pbex._tqm._stdout_callback = callback pbex.run() logger.debug(f'Playbook finished {playbook_path}') return callback
def __init__(self, host, remote_user, connection, pipeline, profile, playbooks_path='playbooks'): def set_extra_vars(pipeline=None, profile=None): extra_vars = dict() if pipeline: extra_vars['pipeline_label'] = pipeline['label'] extra_vars['pipeline_url'] = pipeline['url'] extra_vars['pipeline_cache_path'] = os.path.join(cache_dir, pipeline['label']) if profile: for k, v in profile.items(): extra_vars[k] = v return extra_vars loader = DataLoader() Options = namedtuple('Options', ['connection', 'forks', 'remote_user', 'become', 'become_method', 'become_user', 'diff', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'module_path']) options = Options(connection=connection, forks=100, remote_user=remote_user, become=None, become_method=None, become_user=None, diff=False, check=False, listhosts=False, listtasks=False, listtags=False, syntax=False, module_path="") passwords = dict() inventory = Inventory(loader=loader, sources="{},".format(host)) variable_manager = VariableManager(loader=loader, inventory=inventory) here = os.path.abspath(os.path.dirname(__file__)) playbook_list = [os.path.join(here, playbooks_path, pipeline['playbook'])] variable_manager.extra_vars = set_extra_vars(pipeline=pipeline, profile=profile) self.pbex = PlaybookExecutor(playbooks=playbook_list, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
def ansible_runner_2x(playbook_path, extra_vars, options=None, inventory_src='localhost', console=True): variable_manager = VariableManager() loader = DataLoader() variable_manager.extra_vars = extra_vars inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_src) passwords = {} pbex = PlaybookExecutor([playbook_path], inventory, variable_manager, loader, options, passwords) return pbex
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() loader = DataLoader() loader.set_basedir(self.generated_path) self.inventory = InventoryManager(loader=loader, sources=[self.generated_path]) self.variable_manager = VariableManager(loader=loader, inventory=self.inventory)
def __init__( self, playbook, extra_vars={}, host_list='/etc/ansible/hosts', connection='ssh', become=False, become_user=None, module_path=None, fork=50, ansible_cfg=None, #os.environ["ANSIBLE_CONFIG"] = None passwords={}, check=False): self.playbook_path = playbook self.passwords = passwords self.extra_vars = extra_vars 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=connection, module_path=module_path, forks=fork, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=become, become_method=None, become_user=become_user, verbosity=None, check=check) if ansible_cfg != None: os.environ["ANSIBLE_CONFIG"] = ansible_cfg self.variable_manager = VariableManager() self.loader = DataLoader() self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=host_list)