def _add_host(self, host_info, iterator): ''' Helper function to add a new host to inventory based on a task result. ''' host_name = host_info.get('host_name') # Check if host in inventory, add if not new_host = self._inventory.get_host(host_name) if not new_host: new_host = Host(name=host_name) self._inventory._hosts_cache[host_name] = new_host self._inventory.get_host_vars(new_host) allgroup = self._inventory.get_group('all') allgroup.add_host(new_host) # Set/update the vars for this host new_host.vars = combine_vars(new_host.vars, self._inventory.get_host_vars(new_host)) new_host.vars = combine_vars(new_host.vars, host_info.get('host_vars', dict())) new_groups = host_info.get('groups', []) for group_name in new_groups: if not self._inventory.get_group(group_name): new_group = Group(group_name) self._inventory.add_group(new_group) self._inventory.get_group_vars(new_group) new_group.vars = self._inventory.get_group_variables( group_name) else: new_group = self._inventory.get_group(group_name) new_group.add_host(new_host) # add this host to the group cache if self._inventory.groups is not None: if group_name in self._inventory.groups: if new_host not in self._inventory.get_group( group_name).hosts: self._inventory.get_group(group_name).hosts.append( new_host.name) # clear pattern caching completely since it's unpredictable what # patterns may have referenced the group self._inventory.clear_pattern_cache() # clear cache of group dict, which is used in magic host variables self._inventory.clear_group_dict_cache() # also clear the hostvar cache entry for the given play, so that # the new hosts are available if hostvars are referenced self._variable_manager.invalidate_hostvars_cache(play=iterator._play)
def _add_host(self, host_info, iterator): ''' Helper function to add a new host to inventory based on a task result. ''' host_name = host_info.get('host_name') # Check if host in inventory, add if not new_host = self._inventory.get_host(host_name) if not new_host: new_host = Host(name=host_name) self._inventory._hosts_cache[host_name] = new_host self._inventory.get_host_vars(new_host) allgroup = self._inventory.get_group('all') allgroup.add_host(new_host) # Set/update the vars for this host new_host.vars = combine_vars(new_host.vars, self._inventory.get_host_vars(new_host)) new_host.vars = combine_vars(new_host.vars, host_info.get('host_vars', dict())) new_groups = host_info.get('groups', []) for group_name in new_groups: if not self._inventory.get_group(group_name): new_group = Group(group_name) self._inventory.add_group(new_group) self._inventory.get_group_vars(new_group) new_group.vars = self._inventory.get_group_variables(group_name) else: new_group = self._inventory.get_group(group_name) new_group.add_host(new_host) # add this host to the group cache if self._inventory.groups is not None: if group_name in self._inventory.groups: if new_host not in self._inventory.get_group(group_name).hosts: self._inventory.get_group(group_name).hosts.append(new_host.name) # clear pattern caching completely since it's unpredictable what # patterns may have referenced the group self._inventory.clear_pattern_cache() # clear cache of group dict, which is used in magic host variables self._inventory.clear_group_dict_cache() # also clear the hostvar cache entry for the given play, so that # the new hosts are available if hostvars are referenced self._variable_manager.invalidate_hostvars_cache(play=iterator._play)
def _create_implicit_localhost(self, pattern): if self.localhost: new_host = self.localhost else: new_host = Host(pattern) # use 'all' vars but not part of all group new_host.vars = self.groups['all'].get_vars() new_host.address = "127.0.0.1" new_host.implicit = True if "ansible_python_interpreter" not in new_host.vars: py_interp = sys.executable if not py_interp: # sys.executable is not set in some cornercases. #13585 py_interp = '/usr/bin/python' display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. ' 'You can correct this by setting ansible_python_interpreter for localhost') new_host.set_variable("ansible_python_interpreter", py_interp) if "ansible_connection" not in new_host.vars: new_host.set_variable("ansible_connection", 'local') self.localhost = new_host return new_host
def _create_implicit_localhost(self, pattern): new_host = Host(pattern) new_host.address = "127.0.0.1" new_host.vars = self.get_host_vars(new_host) new_host.set_variable("ansible_connection", "local") if "ansible_python_interpreter" not in new_host.vars: new_host.set_variable("ansible_python_interpreter", sys.executable) self.get_group("ungrouped").add_host(new_host) return new_host
def _create_implicit_localhost(self, pattern='localhost'): new_host = Host(pattern) new_host.address = "127.0.0.1" new_host.implicit = True new_host.vars = self.get_host_vars(new_host) new_host.set_variable("ansible_connection", "local") if "ansible_python_interpreter" not in new_host.vars: py_interp = sys.executable if not py_interp: # sys.executable is not set in some cornercases. #13585 display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. You can correct this by setting ansible_python_interpreter for localhost') py_interp = '/usr/bin/python' new_host.set_variable("ansible_python_interpreter", py_interp) self.get_group("ungrouped").add_host(new_host) return new_host
def _add_host(self, host_info): ''' Helper function to add a new host to inventory based on a task result. ''' host_name = host_info.get('host_name') # Check if host in cache, add if not if host_name in self._inventory._hosts_cache: new_host = self._inventory._hosts_cache[host_name] else: new_host = Host(name=host_name) self._inventory._hosts_cache[host_name] = new_host allgroup = self._inventory.get_group('all') allgroup.add_host(new_host) # Set/update the vars for this host # FIXME: probably should have a set vars method for the host? new_vars = host_info.get('host_vars', dict()) new_host.vars = self._inventory.get_host_vars(new_host) new_host.vars.update(new_vars) new_groups = host_info.get('groups', []) for group_name in new_groups: if not self._inventory.get_group(group_name): new_group = Group(group_name) self._inventory.add_group(new_group) new_group.vars = self._inventory.get_group_variables( group_name) else: new_group = self._inventory.get_group(group_name) new_group.add_host(new_host) # add this host to the group cache if self._inventory.groups is not None: if group_name in self._inventory.groups: if new_host not in self._inventory.get_group( group_name).hosts: self._inventory.get_group(group_name).hosts.append( new_host.name) # clear pattern caching completely since it's unpredictable what # patterns may have referenced the group # FIXME: is this still required? self._inventory.clear_pattern_cache()
def _add_host(self, host_info): ''' Helper function to add a new host to inventory based on a task result. ''' host_name = host_info.get('host_name') # Check if host in cache, add if not if host_name in self._inventory._hosts_cache: new_host = self._inventory._hosts_cache[host_name] else: new_host = Host(name=host_name) self._inventory._hosts_cache[host_name] = new_host allgroup = self._inventory.get_group('all') allgroup.add_host(new_host) # Set/update the vars for this host # FIXME: probably should have a set vars method for the host? new_vars = host_info.get('host_vars', dict()) new_host.vars = self._inventory.get_host_vars(new_host) new_host.vars.update(new_vars) new_groups = host_info.get('groups', []) for group_name in new_groups: if not self._inventory.get_group(group_name): new_group = Group(group_name) self._inventory.add_group(new_group) new_group.vars = self._inventory.get_group_variables(group_name) else: new_group = self._inventory.get_group(group_name) new_group.add_host(new_host) # add this host to the group cache if self._inventory.groups is not None: if group_name in self._inventory.groups: if new_host not in self._inventory.get_group(group_name).hosts: self._inventory.get_group(group_name).hosts.append(new_host.name) # clear pattern caching completely since it's unpredictable what # patterns may have referenced the group # FIXME: is this still required? self._inventory.clear_pattern_cache()
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): if self.runner.noop_on_check(inject): return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module')) args = {} if complex_args: args.update(complex_args) args.update(parse_kv(module_args)) if not 'hostname' in args and not 'name' in args: raise ae("'name' is a required argument.") result = {} # Parse out any hostname:port patterns new_name = args.get('name', args.get('hostname', None)) vv("creating host via 'add_host': hostname=%s" % new_name) if ":" in new_name: new_name, new_port = new_name.split(":") args['ansible_ssh_port'] = new_port # redefine inventory and get group "all" inventory = self.runner.inventory allgroup = inventory.get_group('all') # check if host in cache, add if not if new_name in inventory._hosts_cache: new_host = inventory._hosts_cache[new_name] else: new_host = Host(new_name) # only groups can be added directly to inventory inventory._hosts_cache[new_name] = new_host allgroup.add_host(new_host) groupnames = args.get('groupname', args.get('groups', args.get('group', ''))) # add it to the group if that was specified if groupnames: for group_name in groupnames.split(","): group_name = group_name.strip() if not inventory.get_group(group_name): new_group = Group(group_name) inventory.add_group(new_group) new_group.vars = inventory.get_group_variables(group_name, vault_password=inventory._vault_password) grp = inventory.get_group(group_name) grp.add_host(new_host) # add this host to the group cache if inventory._groups_list is not None: if group_name in inventory._groups_list: if new_host.name not in inventory._groups_list[group_name]: inventory._groups_list[group_name].append(new_host.name) vv("added host to group via add_host module: %s" % group_name) result['new_groups'] = groupnames.split(",") # actually load host vars new_host.vars = combine_vars(new_host.vars, inventory.get_host_variables(new_name, update_cached=True, vault_password=inventory._vault_password)) # Add any passed variables to the new_host for k in args.keys(): if not k in [ 'name', 'hostname', 'groupname', 'groups' ]: new_host.set_variable(k, args[k]) result['new_host'] = new_name # clear pattern caching completely since it's unpredictable what # patterns may have referenced the group inventory.clear_pattern_cache() return ReturnData(conn=conn, comm_ok=True, result=result)
def _get_delegated_vars(self, play, task, existing_variables): # we unfortunately need to template the delegate_to field here, # as we're fetching vars before post_validate has been called on # the task that has been passed in vars_copy = existing_variables.copy() templar = Templar(loader=self._loader, variables=vars_copy) items = [] if task.loop is not None: if task.loop in lookup_loader: try: loop_terms = listify_lookup_plugin_terms(terms=task.loop_args, templar=templar, loader=self._loader, fail_on_undefined=True, convert_bare=False) items = lookup_loader.get(task.loop, loader=self._loader, templar=templar).run(terms=loop_terms, variables=vars_copy) except AnsibleUndefinedVariable: # This task will be skipped later due to this, so we just setup # a dummy array for the later code so it doesn't fail items = [None] else: raise AnsibleError("Unexpected failure in finding the lookup named '%s' in the available lookup plugins" % task.loop) else: items = [None] delegated_host_vars = dict() for item in items: # update the variables with the item value for templating, in case we need it if item is not None: vars_copy['item'] = item templar.set_available_variables(vars_copy) delegated_host_name = templar.template(task.delegate_to, fail_on_undefined=False) if delegated_host_name is None: raise AnsibleError(message="Undefined delegate_to host for task:", obj=task._ds) if delegated_host_name in delegated_host_vars: # no need to repeat ourselves, as the delegate_to value # does not appear to be tied to the loop item variable continue # a dictionary of variables to use if we have to create a new host below # we set the default port based on the default transport here, to make sure # we use the proper default for windows new_port = C.DEFAULT_REMOTE_PORT if C.DEFAULT_TRANSPORT == 'winrm': new_port = 5986 new_delegated_host_vars = dict( ansible_delegated_host=delegated_host_name, ansible_host=delegated_host_name, # not redundant as other sources can change ansible_host ansible_port=new_port, ansible_user=C.DEFAULT_REMOTE_USER, ansible_connection=C.DEFAULT_TRANSPORT, ) # now try to find the delegated-to host in inventory, or failing that, # create a new host on the fly so we can fetch variables for it delegated_host = None if self._inventory is not None: delegated_host = self._inventory.get_host(delegated_host_name) # try looking it up based on the address field, and finally # fall back to creating a host on the fly to use for the var lookup if delegated_host is None: if delegated_host_name in C.LOCALHOST: delegated_host = self._inventory.localhost else: for h in self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True): # check if the address matches, or if both the delegated_to host # and the current host are in the list of localhost aliases if h.address == delegated_host_name: delegated_host = h break else: delegated_host = Host(name=delegated_host_name) delegated_host.vars = combine_vars(delegated_host.vars, new_delegated_host_vars) else: delegated_host = Host(name=delegated_host_name) delegated_host.vars = combine_vars(delegated_host.vars, new_delegated_host_vars) # now we go fetch the vars for the delegated-to host and save them in our # master dictionary of variables to be used later in the TaskExecutor/PlayContext delegated_host_vars[delegated_host_name] = self.get_vars( play=play, host=delegated_host, task=task, include_delegate_to=False, include_hostvars=False, ) return delegated_host_vars
def _get_delegated_vars(self, play, task, existing_variables): if not hasattr(task, 'loop'): # This "task" is not a Task, so we need to skip it return {}, None # we unfortunately need to template the delegate_to field here, # as we're fetching vars before post_validate has been called on # the task that has been passed in vars_copy = existing_variables.copy() self._templar.available_variables = vars_copy items = [] has_loop = True if task.loop_with is not None: if task.loop_with in lookup_loader: try: loop_terms = listify_lookup_plugin_terms( terms=task.loop, templar=self._templar, loader=self._loader, fail_on_undefined=True, convert_bare=False) items = lookup_loader.get(task.loop_with, loader=self._loader, templar=self._templar).run( terms=loop_terms, variables=vars_copy) except AnsibleTemplateError: # This task will be skipped later due to this, so we just setup # a dummy array for the later code so it doesn't fail items = [None] else: raise AnsibleError( "Failed to find the lookup named '%s' in the available lookup plugins" % task.loop_with) elif task.loop is not None: try: items = self._templar.template(task.loop) except AnsibleTemplateError: # This task will be skipped later due to this, so we just setup # a dummy array for the later code so it doesn't fail items = [None] else: has_loop = False items = [None] delegated_host_vars = dict() item_var = getattr(task.loop_control, 'loop_var', 'item') cache_items = False for item in items: # update the variables with the item value for templating, in case we need it if item is not None: vars_copy[item_var] = item self._templar.available_variables = vars_copy delegated_host_name = self._templar.template( task.delegate_to, fail_on_undefined=False) if delegated_host_name != task.delegate_to: cache_items = True if delegated_host_name is None: raise AnsibleError( message="Undefined delegate_to host for task:", obj=task._ds) if not isinstance(delegated_host_name, string_types): raise AnsibleError( message= "the field 'delegate_to' has an invalid type (%s), and could not be" " converted to a string type." % type(delegated_host_name), obj=task._ds) if delegated_host_name in delegated_host_vars: # no need to repeat ourselves, as the delegate_to value # does not appear to be tied to the loop item variable continue # a dictionary of variables to use if we have to create a new host below # we set the default port based on the default transport here, to make sure # we use the proper default for windows new_port = C.DEFAULT_REMOTE_PORT if C.DEFAULT_TRANSPORT == 'winrm': new_port = 5986 new_delegated_host_vars = dict( ansible_delegated_host=delegated_host_name, ansible_host= delegated_host_name, # not redundant as other sources can change ansible_host ansible_port=new_port, ansible_user=C.DEFAULT_REMOTE_USER, ansible_connection=C.DEFAULT_TRANSPORT, ) # now try to find the delegated-to host in inventory, or failing that, # create a new host on the fly so we can fetch variables for it delegated_host = None if self._inventory is not None: delegated_host = self._inventory.get_host(delegated_host_name) # try looking it up based on the address field, and finally # fall back to creating a host on the fly to use for the var lookup if delegated_host is None: if delegated_host_name in C.LOCALHOST: delegated_host = self._inventory.localhost else: for h in self._inventory.get_hosts( ignore_limits=True, ignore_restrictions=True): # check if the address matches, or if both the delegated_to host # and the current host are in the list of localhost aliases if h.address == delegated_host_name: delegated_host = h break else: delegated_host = Host(name=delegated_host_name) delegated_host.vars = combine_vars( delegated_host.vars, new_delegated_host_vars) else: delegated_host = Host(name=delegated_host_name) delegated_host.vars = combine_vars(delegated_host.vars, new_delegated_host_vars) # now we go fetch the vars for the delegated-to host and save them in our # master dictionary of variables to be used later in the TaskExecutor/PlayContext delegated_host_vars[delegated_host_name] = self.get_vars( play=play, host=delegated_host, task=task, include_delegate_to=False, include_hostvars=False, ) _ansible_loop_cache = None if has_loop and cache_items: # delegate_to templating produced a change, so we will cache the templated items # in a special private hostvar # this ensures that delegate_to+loop doesn't produce different results than TaskExecutor # which may reprocess the loop _ansible_loop_cache = items return delegated_host_vars, _ansible_loop_cache
def handle(): host1 = Host("127.0.0.1") host1.vars = dict(ansible_port=22, ansible_user="******", ansible_ssh_private_key_file="../tmp/127.0.0.1") # none variable variable_manager = VariableManager() g = Group("group_wb") g.add_host(host1) # target ip list inventory = Inventory(loader=loader, variable_manager=variable_manager) inventory.add_group(g) # other options options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=1, remote_user='******', ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user="******", verbosity=None, check=False) tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None ) # test tasks task0 = dict(action=dict(module='shell', args='sleep 6')) task1 = dict(action=dict(module='shell', args='ls')) task2 = dict(action=dict(module='shell', args='echo "2333"')) play_source = dict( name="Test Play", hosts="group_wb", gather_facts='no', tasks=[ task0, task1, task2 ] ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) try: result = tqm.run(play) print("Result code: " + str(result)) print("Type: " + str(type(result))) # close queue manager finally: if tqm is not None: tqm.cleanup()
def run(self): # insert node for ip in self.hosts: self._node_map[ip] = Service.new_node(self.task_id, ip) variable_manager = VariableManager() Logger.debug("start write ssh_key for task: {} global_id : {}".format( self.task_id, self.global_id)) key_files = [] group = Group(self.task_id) for h in self.hosts: # get ssh_key content key_content = _get_ssh_key(h) Logger.debug("read ssh_key for host: {} global_id: {}".format( h, self.global_id)) # write ssh private key key_path = _write_ssh_key(h, key_content) #key_path="./tmp/97" Logger.debug("write ssh_key for host: {} global_id: {}".format( h, self.global_id)) host_vars = dict(ansible_port=22, ansible_user=self.user, ansible_ssh_private_key_file="./" + key_path) Logger.debug("key_path: {} global_id: {}".format( key_path, self.global_id)) key_files.append(key_path) host = Host(h) host.vars = host_vars group.add_host(host) # add params to each host if self.params is not None and isinstance(self.params, dict): for h in group.hosts: for key in self.params.keys(): variable_manager.set_host_variable(h, key, self.params[key]) Logger.debug("success write ssh_key for task: {} global_id: {}".format( self.task_id, self.global_id)) # other options ssh_args = '-oControlMaster=auto -oControlPersist=60s -oStrictHostKeyChecking=no' options = _Options(connection='ssh', module_path='./ansible/library', forks=self.forks, timeout=10, remote_user=None, private_key_file=None, ssh_common_args=ssh_args, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False) if self.tasktype == "ansible_task": Logger.debug( "ansible tasks set******************* global_id: {}".format( self.global_id)) play_source = dict(name=self.task_id, hosts=self.task_id, gather_facts='yes', tasks=self.tasks) else: Logger.debug( "ansible role set******************* global_id: {}".format( self.global_id)) play_source = dict(name=self.task_id, hosts=self.task_id, gather_facts='yes', roles=self.tasks) Logger.debug("start load play for task: {} global_id: {}".format( self.task_id, self.global_id)) # make playbook playbook = Play().load(play_source, variable_manager=variable_manager, loader=_Loader) inventory = Inventory(loader=_Loader, variable_manager=variable_manager) inventory.add_group(group) call_back = SyncCallbackModule(debug=True, step_callback=self._step_callback, global_id=self.global_id, source=self.source, tag_hosts=self.hosts) Logger.debug("success load play for task: {} global_id: {}".format( self.task_id, self.global_id)) # task queue tqm = TaskQueueManager(inventory=inventory, variable_manager=variable_manager, loader=_Loader, options=options, passwords=None, stdout_callback=call_back) try: back = tqm.run(playbook) Logger.info("back: {} global_id : {}".format( str(back), self.global_id)) if back != 0: raise Exception("playbook run failed") return back finally: if tqm is not None: tqm.cleanup() _rm_tmp_key(key_files)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): if self.runner.noop_on_check(inject): return ReturnData( conn=conn, comm_ok=True, result=dict(skipped=True, msg="check mode not supported for this module") ) args = {} if complex_args: args.update(complex_args) args.update(parse_kv(module_args)) if not "hostname" in args and not "name" in args: raise ae("'name' is a required argument.") result = {} # Parse out any hostname:port patterns new_name = args.get("name", args.get("hostname", None)) vv("creating host via 'add_host': hostname=%s" % new_name) if ":" in new_name: new_name, new_port = new_name.split(":") args["ansible_ssh_port"] = new_port # redefine inventory and get group "all" inventory = self.runner.inventory allgroup = inventory.get_group("all") # check if host in cache, add if not if new_name in inventory._hosts_cache: new_host = inventory._hosts_cache[new_name] else: new_host = Host(new_name) # only groups can be added directly to inventory inventory._hosts_cache[new_name] = new_host allgroup.add_host(new_host) groupnames = args.get("groupname", args.get("groups", args.get("group", ""))) # add it to the group if that was specified if groupnames: for group_name in groupnames.split(","): group_name = group_name.strip() if not inventory.get_group(group_name): new_group = Group(group_name) inventory.add_group(new_group) new_group.vars = inventory.get_group_variables(group_name, vault_password=inventory._vault_password) grp = inventory.get_group(group_name) grp.add_host(new_host) # add this host to the group cache if inventory._groups_list is not None: if group_name in inventory._groups_list: if new_host.name not in inventory._groups_list[group_name]: inventory._groups_list[group_name].append(new_host.name) vv("added host to group via add_host module: %s" % group_name) result["new_groups"] = groupnames.split(",") # actually load host vars new_host.vars = combine_vars( new_host.vars, inventory.get_host_variables(new_name, update_cached=True, vault_password=inventory._vault_password), ) # Add any passed variables to the new_host for k in args.keys(): if not k in ["name", "hostname", "groupname", "groups"]: new_host.set_variable(k, args[k]) result["new_host"] = new_name # clear pattern caching completely since it's unpredictable what # patterns may have referenced the group inventory.clear_pattern_cache() return ReturnData(conn=conn, comm_ok=True, result=result)