def my_add_group(self, hosts, groupname, groupvars=None): my_group = Group(name=groupname) if groupvars: for key, value in groupvars.iteritems(): my_group.set_variable(key, value) for host in hosts: hostname = host.get('hostname') hostip = host.get('ip', hostname) hostport = host.get('port', 22) username = host.get('username', 'root') password = host.get('password') ssh_key = host.get("ssh_key") my_host = Host(name=hostname, port=hostport) my_host.set_variable('ansible_ssh_host', hostip) my_host.set_variable('ansible_ssh_port', hostport) my_host.set_variable('ansible_ssh_user', username) my_host.set_variable('ansible_ssh_pass', password) my_host.set_variable('ansible_ssh_private_key_file', ssh_key) for key, value in host.iteritems(): if key not in ['hostname', 'port', 'username', 'password']: my_host.set_variable(key, value) my_group.add_host(my_host) self.inventory.add_group(my_group)
class NetworkTest(object): def __init__(self, playbook): self.inventory = Inventory(host_list=[]) self.playbook = playbook self.stac_nodes_group = Group(name='stac_nodes') self.inventory.add_group(self.stac_nodes_group) self.inv_vars = dict() def set_inventory_vars(self, inv_vars): self.inv_vars.update(inv_vars) def add_stac_node(self, node): stac_node = Host(name=node) self.stac_nodes_group.add_host(stac_node) def run(self): stats = AggregateStats() playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY) runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) pb = playbook.PlayBook(playbook=self.playbook, stats=stats, callbacks=playbook_cb, runner_callbacks=runner_cb, inventory=self.inventory, extra_vars=self.inv_vars, check=False) pr = pb.run() print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
def _add_group(self, host, result_item): ''' Helper function to add a group (if it does not exist), and to assign the specified host to that group. ''' changed = False # the host here is from the executor side, which means it was a # serialized/cloned copy and we'll need to look up the proper # host object from the master inventory real_host = self._inventory.get_host(host.name) group_name = result_item.get('add_group') new_group = self._inventory.get_group(group_name) if not new_group: # create the new group and add it to inventory new_group = Group(name=group_name) self._inventory.add_group(new_group) new_group.vars = self._inventory.get_group_vars(new_group) # and add the group to the proper hierarchy allgroup = self._inventory.get_group('all') allgroup.add_child_group(new_group) changed = True if group_name not in host.get_groups(): new_group.add_host(real_host) changed = True return changed
def my_add_group(self, hosts, groupname, groupvars=None): """ add hosts to a group """ my_group = Group(name=groupname) # if group variables exists, add them to group if groupvars: for key, value in groupvars.iteritems(): my_group.set_variable(key, value) # add hosts to group for host in hosts: # set connection variables hostname = host.get("hostname") hostip = host.get('ip', hostname) hostport = host.get("port") username = host.get("username") password = host.get("password") ssh_key = host.get("ssh_key") my_host = Host(name=hostname, port=hostport) my_host.set_variable('ansible_ssh_host', hostip) my_host.set_variable('ansible_ssh_port', hostport) my_host.set_variable('ansible_ssh_user', username) my_host.set_variable('ansible_ssh_pass', password) my_host.set_variable('ansible_ssh_private_key_file', ssh_key) # set other variables for key, value in host.iteritems(): if key not in ["hostname", "port", "username", "password"]: my_host.set_variable(key, value) # add to group my_group.add_host(my_host) self.inventory.add_group(my_group)
def parse_inventory(self, host_list): if isinstance(host_list, string_types): if "," in host_list: host_list = host_list.split(",") host_list = [h for h in host_list if h and h.strip()] self.parser = None # Always create the 'all' and 'ungrouped' groups, even if host_list is # empty: in this case we will subsequently an the implicit 'localhost' to it. ungrouped = Group(name='ungrouped') all = Group('all') all.add_child_group(ungrouped) self.groups = dict(all=all, ungrouped=ungrouped) if host_list is None: pass elif isinstance(host_list, list): for h in host_list: try: (host, port) = parse_address(h, allow_ranges=False) except AnsibleError as e: display.vvv( "Unable to parse address from hostname, leaving unchanged: %s" % to_string(e)) host = h port = None all.add_host(Host(host, port)) elif self._loader.path_exists(host_list): #TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if self.is_directory(host_list): # Ensure basedir is inside the directory host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, groups=self.groups, filename=host_list) else: self.parser = get_file_parser(host_list, self.groups, self._loader) vars_loader.add_directory(self.basedir(), with_subdir=True) if not self.parser: # should never happen, but JIC raise AnsibleError( "Unable to parse %s as an inventory source" % host_list) self._vars_plugins = [x for x in vars_loader.all(self)] # get group vars from group_vars/ files and vars plugins for group in self.groups.values(): group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(): host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
def __init__(self, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list # caching to avoid repeated calculations, particularly with # external inventory scripts. self._vars_per_host = {} self._vars_per_group = {} self._hosts_cache = {} self._groups_list = {} # to be set by calling set_playbook_basedir by ansible-playbook self._playbook_basedir = None # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None self._also_restriction = None self._subset = None if isinstance(host_list, basestring): if "," in host_list: host_list = host_list.split(",") host_list = [h for h in host_list if h and h.strip()] if isinstance(host_list, list): self.parser = None all = Group('all') self.groups = [all] for x in host_list: if ":" in x: tokens = x.split(":", 1) all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.path.exists(host_list): if os.path.isdir(host_list): # Ensure basedir is inside the directory self.host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(filename=host_list) self.groups = self.parser.groups.values() elif utils.is_executable(host_list): self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() utils.plugins.vars_loader.add_directory(self.basedir(), with_subdir=True) else: raise errors.AnsibleError( "Unable to find an inventory file, specify one with -i ?") self._vars_plugins = [x for x in utils.plugins.vars_loader.all(self)]
def __init__(self, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list # caching to avoid repeated calculations, particularly with # external inventory scripts. self._vars_per_host = {} self._vars_per_group = {} self._hosts_cache = {} self._groups_list = {} # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None self._also_restriction = None self._subset = None if type(host_list) in [ str, unicode ]: if host_list.find(",") != -1: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] if type(host_list) == list: self.parser = None all = Group('all') self.groups = [ all ] for x in host_list: if x.find(":") != -1: tokens = x.split(":",1) all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.path.exists(host_list): if os.path.isdir(host_list): # Ensure basedir is inside the directory self.host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(filename=host_list) self.groups = self.parser.groups.values() elif utils.is_executable(host_list): self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: data = file(host_list).read() if not data.startswith("---"): self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() else: raise errors.AnsibleError("YAML inventory support is deprecated in 0.6 and removed in 0.7, see the migration script in examples/scripts in the git checkout") utils.plugins.vars_loader.add_directory(self.basedir(), with_subdir=True) else: raise errors.AnsibleError("Unable to find an inventory file, specify one with -i ?") self._vars_plugins = [ x for x in utils.plugins.vars_loader.all(self) ]
def parse_inventory(self, host_list): if isinstance(host_list, string_types): if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] self.parser = None # Always create the 'all' and 'ungrouped' groups, even if host_list is # empty: in this case we will subsequently an the implicit 'localhost' to it. ungrouped = Group('ungrouped') all = Group('all') all.add_child_group(ungrouped) self.groups = dict(all=all, ungrouped=ungrouped) if host_list is None: pass elif isinstance(host_list, list): for h in host_list: try: (host, port) = parse_address(h, allow_ranges=False) except AnsibleError as e: display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_unicode(e)) host = h port = None all.add_host(Host(host, port)) elif self._loader.path_exists(host_list): #TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if self.is_directory(host_list): # Ensure basedir is inside the directory host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, groups=self.groups, filename=host_list) else: self.parser = get_file_parser(host_list, self.groups, self._loader) vars_loader.add_directory(self.basedir(), with_subdir=True) if not self.parser: # should never happen, but JIC raise AnsibleError("Unable to parse %s as an inventory source" % host_list) else: display.warning("Host file not found: %s" % to_unicode(host_list)) self._vars_plugins = [ x for x in vars_loader.all(self) ] # set group vars from group_vars/ files and vars plugins for g in self.groups: group = self.groups[g] group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) # set host vars from host_vars/ files and vars plugins for host in self.get_hosts(): host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
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 __init__(self, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list # caching to avoid repeated calculations, particularly with # external inventory scripts. self._vars_per_host = {} self._vars_per_group = {} self._hosts_cache = {} self._groups_list = {} # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None self._also_restriction = None self._subset = None # whether the inventory file is a script self._is_script = False if type(host_list) in [ str, unicode ]: if host_list.find(",") != -1: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] else: utils.plugins.vars_loader.add_directory(self.basedir()) if type(host_list) == list: all = Group('all') self.groups = [ all ] for x in host_list: if x.find(":") != -1: tokens = x.split(":",1) all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif utils.is_executable(host_list): self._is_script = True self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: data = file(host_list).read() if not data.startswith("---"): self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() else: raise errors.AnsibleError("YAML inventory support is deprecated in 0.6 and removed in 0.7, see the migration script in examples/scripts in the git checkout")
def test_direct_host_ordering(self): """Hosts are returned in order they are added """ group = Group('A') # host names not added in alphabetical order host_name_list = ['z', 'b', 'c', 'a', 'p', 'q'] expected_hosts = [] for host_name in host_name_list: h = Host(host_name) group.add_host(h) expected_hosts.append(h) assert group.get_hosts() == expected_hosts
def __init__(self, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list # caching to avoid repeated calculations, particularly with # external inventory scripts. self._vars_per_host = {} self._vars_per_group = {} self._hosts_cache = {} self._groups_list = {} # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None self._also_restriction = None self._subset = None # whether the inventory file is a script self._is_script = False if type(host_list) in [str, unicode]: if host_list.find(",") != -1: host_list = host_list.split(",") host_list = [h for h in host_list if h and h.strip()] if type(host_list) == list: all = Group('all') self.groups = [all] for x in host_list: if x.find(":") != -1: tokens = x.split(":", 1) all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.access(host_list, os.X_OK): self._is_script = True self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: data = file(host_list).read() if not data.startswith("---"): self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() else: raise errors.AnsibleError( "YAML inventory support is deprecated in 0.6 and removed in 0.7, see the migration script in examples/scripts in the git checkout" )
def test_populates_descendant_hosts(self): A = Group('A') B = Group('B') C = Group('C') h = Host('h') C.add_host(h) A.add_child_group(B) # B is child of A B.add_child_group(C) # C is descendant of A A.add_child_group(B) self.assertEqual(set(h.groups), set([C, B, A])) h2 = Host('h2') C.add_host(h2) self.assertEqual(set(h2.groups), set([C, B, A]))
def test_sub_group_host_ordering(self): """With multiple nested groups, asserts that hosts are returned in deterministic order """ top_group = Group('A') expected_hosts = [] for name in ['z', 'b', 'c', 'a', 'p', 'q']: child = Group('group_{0}'.format(name)) top_group.add_child_group(child) host = Host('host_{0}'.format(name)) child.add_host(host) expected_hosts.append(host) assert top_group.get_hosts() == expected_hosts
def my_add_group(self, hosts_vars, group_name, group_vars=None): """ add hosts to group. use to generate a inventory. Args: hosts_vars: the hosts variables group_name: group name group_vars: group variables """ my_group = Group(name=group_name) self.add_group_vars(my_group, group_vars) for host in self.gen_hosts(hosts_vars): my_group.add_host(host) self.inventory.add_group(my_group)
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 parse_inventory(self, host_list): if isinstance(host_list, basestring): if "," in host_list: host_list = host_list.split(",") host_list = [h for h in host_list if h and h.strip()] if host_list is None: self.parser = None elif isinstance(host_list, list): self.parser = None all = Group('all') self.groups = [all] ipv6_re = re.compile('\[([a-f:A-F0-9]*[%[0-z]+]?)\](?::(\d+))?') for x in host_list: m = ipv6_re.match(x) if m: all.add_host(Host(m.groups()[0], m.groups()[1])) else: if ":" in x: tokens = x.rsplit(":", 1) # if there is ':' in the address, then this is an ipv6 if ':' in tokens[0]: all.add_host(Host(x)) else: all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.path.exists(host_list): #TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if os.path.isdir(host_list): # Ensure basedir is inside the directory host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, filename=host_list) else: self.parser = get_file_parser(host_list, self._loader) vars_loader.add_directory(self.basedir(), with_subdir=True) if self.parser: self.groups = self.parser.groups.values() else: # should never happen, but JIC raise AnsibleError( "Unable to parse %s as an inventory source" % host_list) self._vars_plugins = [x for x in vars_loader.all(self)] # FIXME: shouldn't be required, since the group/host vars file # management will be done in VariableManager # get group vars from group_vars/ files and vars plugins for group in self.groups: group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(): host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
def parse_inventory(self, host_list): if isinstance(host_list, string_types): if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] self.parser = None # Always create the 'all' and 'ungrouped' groups, even if host_list is # empty: in this case we will subsequently an the implicit 'localhost' to it. ungrouped = Group(name='ungrouped') all = Group('all') all.add_child_group(ungrouped) self.groups = dict(all=all, ungrouped=ungrouped) if host_list is None: pass elif isinstance(host_list, list): for h in host_list: (host, port) = parse_address(h, allow_ranges=False) all.add_host(Host(host, port)) elif self._loader.path_exists(host_list): #TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if self._loader.is_directory(host_list): # Ensure basedir is inside the directory host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, groups=self.groups, filename=host_list) else: self.parser = get_file_parser(host_list, self.groups, self._loader) vars_loader.add_directory(self.basedir(), with_subdir=True) if not self.parser: # should never happen, but JIC raise AnsibleError("Unable to parse %s as an inventory source" % host_list) self._vars_plugins = [ x for x in vars_loader.all(self) ] # FIXME: shouldn't be required, since the group/host vars file # management will be done in VariableManager # get group vars from group_vars/ files and vars plugins for group in self.groups.values(): group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(): host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
def _parse(self): groups = {} self.raw = utils.parse_json(self.data) all = Group('all') self.groups = dict(all=all) group = None for (group_name, hosts) in self.raw.items(): group = groups[group_name] = Group(group_name) host = None for hostname in hosts: host = Host(hostname) group.add_host(host) # FIXME: hack shouldn't be needed all.add_host(host) all.add_child_group(group) return groups
def _parse(self): groups = {} self.raw = utils.parse_json(self.data) all=Group('all') self.groups = dict(all=all) group = None for (group_name, hosts) in self.raw.items(): group = groups[group_name] = Group(group_name) host = None for hostname in hosts: host = Host(hostname) group.add_host(host) # FIXME: hack shouldn't be needed all.add_host(host) all.add_child_group(group) return groups
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 my_add_group(self, hosts, groupname, groupvars=None): """ add hosts to a group """ my_group = Group(name=groupname) # if group variables exists, add them to group if groupvars: for key, value in groupvars.iteritems(): my_group.set_variable(key, value) # add hosts to group for host in hosts: # set connection variables hostname = host.get("hostname") hostip = host.get('ip', hostname) hostport = host.get("port") # 临时增加的端口验证,如果22不通,则连接21987 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(1) try: sk.connect((hostip,22)) hostport = 22 except Exception: hostport = 21987 # END username = host.get("username") password = host.get("password") ssh_key = host.get("ssh_key") my_host = Host(name=hostname, port=hostport) my_host.set_variable('ansible_ssh_host', hostip) my_host.set_variable('ansible_ssh_port', hostport) my_host.set_variable('ansible_ssh_user', username) my_host.set_variable('ansible_ssh_pass', password) my_host.set_variable('ansible_ssh_private_key_file', ssh_key) # set other variables for key, value in host.iteritems(): if key not in ["hostname", "port", "username", "password"]: my_host.set_variable(key, value) # add to group my_group.add_host(my_host) self.inventory.add_group(my_group)
def parse_inventory(self, host_list): if isinstance(host_list, basestring): if "," in host_list: host_list = host_list.split(",") host_list = [h for h in host_list if h and h.strip()] if host_list is None: self.parser = None elif isinstance(host_list, list): self.parser = None all = Group("all") self.groups = [all] ipv6_re = re.compile("\[([a-f:A-F0-9]*[%[0-z]+]?)\](?::(\d+))?") for x in host_list: m = ipv6_re.match(x) if m: all.add_host(Host(m.groups()[0], m.groups()[1])) else: if ":" in x: tokens = x.rsplit(":", 1) # if there is ':' in the address, then this is an ipv6 if ":" in tokens[0]: all.add_host(Host(x)) else: all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif self._loader.path_exists(host_list): # TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if self._loader.is_directory(host_list): # Ensure basedir is inside the directory host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, filename=host_list) else: self.parser = get_file_parser(host_list, self._loader) vars_loader.add_directory(self.basedir(), with_subdir=True) if self.parser: self.groups = self.parser.groups.values() else: # should never happen, but JIC raise AnsibleError("Unable to parse %s as an inventory source" % host_list) self._vars_plugins = [x for x in vars_loader.all(self)] # FIXME: shouldn't be required, since the group/host vars file # management will be done in VariableManager # get group vars from group_vars/ files and vars plugins for group in self.groups: group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(): host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
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 parse_inventory(self, host_list): if isinstance(host_list, basestring): if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] self.parser = None if host_list is None: pass elif isinstance(host_list, list): all = Group('all') self.groups = [ all ] for h in host_list: (host, port) = parse_address(h, allow_ranges=False) all.add_host(Host(host, port)) elif self._loader.path_exists(host_list): #TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if self._loader.is_directory(host_list): # Ensure basedir is inside the directory host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, filename=host_list) else: self.parser = get_file_parser(host_list, self._loader) vars_loader.add_directory(self.basedir(), with_subdir=True) if self.parser: self.groups = self.parser.groups.values() else: # should never happen, but JIC raise AnsibleError("Unable to parse %s as an inventory source" % host_list) self._vars_plugins = [ x for x in vars_loader.all(self) ] # FIXME: shouldn't be required, since the group/host vars file # management will be done in VariableManager # get group vars from group_vars/ files and vars plugins for group in self.groups: group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(): host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
def _add_group(self, task, iterator): ''' Helper function to add a group (if it does not exist), and to assign the specified host to that group. ''' # the host here is from the executor side, which means it was a # serialized/cloned copy and we'll need to look up the proper # host object from the master inventory groups = {} changed = False for host in self._inventory.get_hosts(): original_task = iterator.get_original_task(host, task) all_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=original_task) templar = Templar(loader=self._loader, variables=all_vars) group_name = templar.template(original_task.args.get('key')) if task.evaluate_conditional(templar=templar, all_vars=all_vars): if group_name not in groups: groups[group_name] = [] groups[group_name].append(host) for group_name, hosts in iteritems(groups): new_group = self._inventory.get_group(group_name) if not new_group: # create the new group and add it to inventory new_group = Group(name=group_name) self._inventory.add_group(new_group) new_group.vars = self._inventory.get_group_vars(new_group) # and add the group to the proper hierarchy allgroup = self._inventory.get_group('all') allgroup.add_child_group(new_group) changed = True for host in hosts: if group_name not in host.get_groups(): new_group.add_host(host) changed = True return changed
def groups(self): groups = cache.get('inv_groups') if groups is not None: return groups groups = {} group_models = InvGroup.objects.all() for group_model in group_models: g = Group(str(group_model.id)) hosts = HostModel.objects.filter(gid=group_model.id) for host in hosts: g.add_host(self._hostModel2Host(host)) groups[str(group_model.id)] = g all_group = Group("all") ungrouped_group = Group("ungrouped") groups["all"] = all_group groups["ungrouped"] = ungrouped_group cache.set('inv_groups', groups, 60*10) return self.groups
def get_ansible_groups(group_map): """ Constructs a list of :class:`ansible.inventory.group.Group` objects from a map of lists of host strings. """ # Some of this logic is cribbed from # ansible.inventory.script.InventoryScript all_hosts = {} group_all = Group('all') groups = [group_all] for gname, hosts in group_map.iteritems(): g = Group(gname) for host in hosts: h = all_hosts.get(host, Host(host)) all_hosts[host] = h g.add_host(h) group_all.add_host(h) group_all.add_child_group(g) groups.append(g) return groups
def __init__(self, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None # whether the inventory file is a script self._is_script = False if type(host_list) in [ str, unicode ]: if host_list.find(",") != -1: host_list = host_list.split(",") if type(host_list) == list: all = Group('all') self.groups = [ all ] for x in host_list: if x.find(":") != -1: tokens = x.split(":",1) all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.access(host_list, os.X_OK): self._is_script = True self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: data = file(host_list).read() if not data.startswith("---"): self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() else: self.parser = InventoryParserYaml(filename=host_list) self.groups = self.parser.groups.values()
def __init__(self, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None # whether the inventory file is a script self._is_script = False if type(host_list) in [str, unicode]: if host_list.find(",") != -1: host_list = host_list.split(",") if type(host_list) == list: all = Group('all') self.groups = [all] for x in host_list: if x.find(":") != -1: tokens = x.split(":", 1) all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.access(host_list, os.X_OK): self._is_script = True self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: data = file(host_list).read() if not data.startswith("---"): self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() else: self.parser = InventoryParserYaml(filename=host_list) self.groups = self.parser.groups.values()
def _add_group(self, host, group_name): ''' Helper function to add a group (if it does not exist), and to assign the specified host to that group. ''' new_group = self._inventory.get_group(group_name) if not new_group: # create the new group and add it to inventory new_group = Group(group_name) self._inventory.add_group(new_group) # and add the group to the proper hierarchy allgroup = self._inventory.get_group('all') allgroup.add_child_group(new_group) # the host here is from the executor side, which means it was a # serialized/cloned copy and we'll need to look up the proper # host object from the master inventory actual_host = self._inventory.get_host(host.name) # and add the host to the group new_group.add_host(actual_host)
class NetworkTest(object): def __init__(self, playbook): self.inventory = Inventory() self.playbook = playbook self.sender_group = Group(name = 'sender') self.inventory.add_group(self.sender_group) self.receiver_group = Group(name = 'receiver') self.inventory.add_group(self.receiver_group) self.master_group = Group(name = 'master') self.inventory.add_group(self.master_group) self.inv_vars = dict() def set_inventory_vars(self, inv_vars): self.inv_vars.update(inv_vars) def add_sender(self, sender): sender_host = Host(name = sender) self.sender_group.add_host(sender_host) def add_receiver(self, receiver): receiver_host = Host(name = receiver) self.receiver_group.add_host(receiver_host) def add_master(self, master): master_host = Host(name = master) self.master_group.add_host(master_host) def run(self): stats = AggregateStats() playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY) runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) pb = playbook.PlayBook(playbook = self.playbook, stats = stats, callbacks = playbook_cb, runner_callbacks = runner_cb, inventory = self.inventory, extra_vars = self.inv_vars, check=False) pr = pb.run() print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
class StorageTest(object): def __init__(self, playbook): self.inventory = Inventory(host_list=[]) self.playbook = playbook self.sender_group = Group(name='sender') self.inventory.add_group(self.sender_group) self.receiver_group = Group(name='receiver') self.inventory.add_group(self.receiver_group) self.master_group = Group(name='master') self.inventory.add_group(self.master_group) self.inv_vars = dict() def set_inventory_vars(self, inv_vars): self.inv_vars.update(inv_vars) def add_sender(self, sender): sender_host = Host(name=sender) self.sender_group.add_host(sender_host) def add_receiver(self, receiver): receiver_host = Host(name=receiver) self.receiver_group.add_host(receiver_host) def add_master(self, master): master_host = Host(name=master) self.master_group.add_host(master_host) def run(self): stats = AggregateStats() playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY) runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) pb = playbook.PlayBook(playbook=self.playbook, stats=stats, callbacks=playbook_cb, runner_callbacks=runner_cb, inventory=self.inventory, extra_vars=self.inv_vars, check=False) pr = pb.run() print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
class NetworkTest(object): def __init__(self, playbook): self.inventory = Inventory(host_list=[]) self.playbook = playbook self.producer_group = Group(name='producer') self.inventory.add_group(self.producer_group) self.consumer_group = Group(name='consumer') self.inventory.add_group(self.consumer_group) self.orchestrator_group = Group(name='orchestrator') self.inventory.add_group(self.orchestrator_group) self.inv_vars = dict() def set_inventory_vars(self, inv_vars): self.inv_vars.update(inv_vars) def add_producer(self, producer): producer_host = Host(name=producer) self.producer_group.add_host(producer_host) def add_consumer(self, consumer): consumer_host = Host(name=consumer) self.consumer_group.add_host(consumer_host) def add_orchestrator(self, orchestrator): orchestrator_host = Host(name=orchestrator) self.orchestrator_group.add_host(orchestrator_host) def run(self): stats = AggregateStats() playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY) runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) pb = playbook.PlayBook(playbook=self.playbook, stats=stats, callbacks=playbook_cb, runner_callbacks=runner_cb, inventory=self.inventory, extra_vars=self.inv_vars, check=False) pr = pb.run() print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
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 __init__(self, host_list=C.DEFAULT_HOST_LIST, vault_password=None): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded # host_list 有很多中类型 self.host_list = host_list self._vault_password=vault_password # caching to avoid repeated calculations, particularly with # external inventory scripts. # 缓存一些数据以避免重复计算,特别是使用外部库存脚本。 self._vars_per_host = {} # 每个主机变量 self._vars_per_group = {} # 每个group 变量 self._hosts_cache = {} # 主机列表缓存 self._groups_list = {} # group 列表缓存 self._pattern_cache = {} # pattern 模式缓存 # to be set by calling set_playbook_basedir by playbook code self._playbook_basedir = None # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None self._also_restriction = None self._subset = None # 如果host_list是字符串,则根据逗号进行分隔成列表 if isinstance(host_list, basestring): if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] if host_list is None: self.parser = None # 如果host_list是列表,创建一个all group,并将host加入all group,支持IPV6 elif isinstance(host_list, list): self.parser = None all = Group('all') self.groups = [ all ] ipv6_re = re.compile('\[([a-f:A-F0-9]*[%[0-z]+]?)\](?::(\d+))?') for x in host_list: m = ipv6_re.match(x) if m: all.add_host(Host(m.groups()[0], m.groups()[1])) else: if ":" in x: tokens = x.rsplit(":", 1) # if there is ':' in the address, then this is an ipv6 if ':' in tokens[0]: all.add_host(Host(x)) else: all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) # 如果host_list是文件或文件夹 elif os.path.exists(host_list): if os.path.isdir(host_list): # Ensure basedir is inside the directory # 如果host_list是目录parser使用 InventoryDirectory 目录解析 self.host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(filename=host_list) self.groups = self.parser.groups.values() else: # check to see if the specified file starts with a # shebang (#!/), so if an error is raised by the parser # class we can show a more apropos error # 如果host_list是文件,则判断文件是否以shebang开头 shebang_present = False try: inv_file = open(host_list) first_line = inv_file.readlines()[0] inv_file.close() if first_line.startswith('#!'): shebang_present = True except: pass if utils.is_executable(host_list): try: # 如果host_list是可执行文件,parser使用InventoryScript解析 self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() except: if not shebang_present: raise errors.AnsibleError("The file %s is marked as executable, but failed to execute correctly. " % host_list + \ "If this is not supposed to be an executable script, correct this with `chmod -x %s`." % host_list) else: raise else: try: # 如果host_list是普通文本文件,parser使用 InventoryParser 解析 self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() except: if shebang_present: raise errors.AnsibleError("The file %s looks like it should be an executable inventory script, but is not marked executable. " % host_list + \ "Perhaps you want to correct this with `chmod +x %s`?" % host_list) else: raise utils.plugins.vars_loader.add_directory(self.basedir(), with_subdir=True) else: raise errors.AnsibleError("Unable to find an inventory file, specify one with -i ?") self._vars_plugins = [ x for x in utils.plugins.vars_loader.all(self) ] # 加载vars plugin,这里先跳过 # get group vars from group_vars/ files and vars plugins # 从group_vars/ 目录的文件和vars plugins中获取group 变量 for group in self.groups: group.vars = utils.combine_vars(group.vars, self.get_group_variables(group.name, vault_password=self._vault_password)) # get host vars from host_vars/ files and vars plugins # 从host_vars/ 目录的文件和vars plugins中获取host变量 for host in self.get_hosts(): host.vars = utils.combine_vars(host.vars, self.get_host_variables(host.name, vault_password=self._vault_password))
class NetworkTest(object): def __init__(self, playbook): self.variable_manager = VariableManager() self.loader = DataLoader() self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager,host_list=[]) self.variable_manager.set_inventory(self.inventory) self.playbook = playbook self.sender_group = Group(name = 'sender') self.inventory.add_group(self.sender_group) self.receiver_group = Group(name = 'receiver') self.inventory.add_group(self.receiver_group) self.master_group = Group(name = 'master') self.inventory.add_group(self.master_group) def set_inventory_vars(self, inv_vars): self.variable_manager.extra_vars = inv_vars def add_sender(self, sender): sender_host = Host(name = sender) self.sender_group.add_host(sender_host) def add_receiver(self, receiver): receiver_host = Host(name = receiver) self.receiver_group.add_host(receiver_host) def add_master(self, master): master_host = Host(name = master) self.master_group.add_host(master_host) def run(self): 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) passwords = {} pbex = PlaybookExecutor(playbooks=[self.playbook], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=options, passwords=passwords) results = pbex.run() print json.dumps(results, sort_keys=True, indent=4, separators=(',', ': '))
def __init__(self, loader, variable_manager, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list self._loader = loader self._variable_manager = variable_manager # caching to avoid repeated calculations, particularly with # external inventory scripts. self._vars_per_host = {} self._vars_per_group = {} self._hosts_cache = {} self._groups_list = {} self._pattern_cache = {} # to be set by calling set_playbook_basedir by playbook code self._playbook_basedir = None # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None self._also_restriction = None self._subset = None if isinstance(host_list, basestring): if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] if host_list is None: self.parser = None elif isinstance(host_list, list): self.parser = None all = Group('all') self.groups = [ all ] ipv6_re = re.compile('\[([a-f:A-F0-9]*[%[0-z]+]?)\](?::(\d+))?') for x in host_list: m = ipv6_re.match(x) if m: all.add_host(Host(m.groups()[0], m.groups()[1])) else: if ":" in x: tokens = x.rsplit(":", 1) # if there is ':' in the address, then this is an ipv6 if ':' in tokens[0]: all.add_host(Host(x)) else: all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.path.exists(host_list): if os.path.isdir(host_list): # Ensure basedir is inside the directory self.host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, filename=host_list) self.groups = self.parser.groups.values() else: # check to see if the specified file starts with a # shebang (#!/), so if an error is raised by the parser # class we can show a more apropos error shebang_present = False try: inv_file = open(host_list) first_line = inv_file.readlines()[0] inv_file.close() if first_line.startswith('#!'): shebang_present = True except: pass if is_executable(host_list): try: self.parser = InventoryScript(loader=self._loader, filename=host_list) self.groups = self.parser.groups.values() except: if not shebang_present: raise errors.AnsibleError("The file %s is marked as executable, but failed to execute correctly. " % host_list + \ "If this is not supposed to be an executable script, correct this with `chmod -x %s`." % host_list) else: raise else: try: self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() except: if shebang_present: raise errors.AnsibleError("The file %s looks like it should be an executable inventory script, but is not marked executable. " % host_list + \ "Perhaps you want to correct this with `chmod +x %s`?" % host_list) else: raise vars_loader.add_directory(self.basedir(), with_subdir=True) else: raise errors.AnsibleError("Unable to find an inventory file, specify one with -i ?") self._vars_plugins = [ x for x in vars_loader.all(self) ] # FIXME: shouldn't be required, since the group/host vars file # management will be done in VariableManager # get group vars from group_vars/ files and vars plugins for group in self.groups: # FIXME: combine_vars group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(): # FIXME: combine_vars host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
def parse_inventory(self, host_list): if isinstance(host_list, string_types): if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] self.parser = None # Always create the 'all' and 'ungrouped' groups, even if host_list is # empty: in this case we will subsequently an the implicit 'localhost' to it. ungrouped = Group('ungrouped') all = Group('all') all.add_child_group(ungrouped) self.groups = dict(all=all, ungrouped=ungrouped) if host_list is None: pass elif isinstance(host_list, list): for h in host_list: try: (host, port) = parse_address(h, allow_ranges=False) except AnsibleError as e: display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_text(e)) host = h port = None new_host = Host(host, port) if h in C.LOCALHOST: # set default localhost from inventory to avoid creating an implicit one. Last localhost defined 'wins'. if self.localhost is not None: display.warning("A duplicate localhost-like entry was found (%s). First found localhost was %s" % (h, self.localhost.name)) display.vvvv("Set default localhost to %s" % h) self.localhost = new_host all.add_host(new_host) elif self._loader.path_exists(host_list): # TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if self.is_directory(host_list): # Ensure basedir is inside the directory host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(loader=self._loader, groups=self.groups, filename=host_list) else: self.parser = get_file_parser(host_list, self.groups, self._loader) vars_loader.add_directory(self._basedir, with_subdir=True) if not self.parser: # should never happen, but JIC raise AnsibleError("Unable to parse %s as an inventory source" % host_list) else: display.warning("Host file not found: %s" % to_text(host_list)) self._vars_plugins = [ x for x in vars_loader.all(self) ] # set group vars from group_vars/ files and vars plugins for g in self.groups: group = self.groups[g] group.vars = combine_vars(group.vars, self.get_group_variables(group.name)) self.get_group_vars(group) # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(ignore_limits=True, ignore_restrictions=True): host.vars = combine_vars(host.vars, self.get_host_variables(host.name)) self.get_host_vars(host)
def __init__(self, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts # if a list, inventory data will NOT be loaded self.host_list = host_list # caching to avoid repeated calculations, particularly with # external inventory scripts. self._vars_per_host = {} self._vars_per_group = {} self._hosts_cache = {} self._groups_list = {} self._pattern_cache = {} # to be set by calling set_playbook_basedir by ansible-playbook self._playbook_basedir = None # the inventory object holds a list of groups self.groups = [] # a list of host(names) to contain current inquiries to self._restriction = None self._also_restriction = None self._subset = None if isinstance(host_list, basestring): if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] if host_list is None: self.parser = None elif isinstance(host_list, list): self.parser = None all = Group('all') self.groups = [ all ] ipv6_re = re.compile('\[([a-f:A-F0-9]*[%[0-z]+]?)\](?::(\d+))?') for x in host_list: m = ipv6_re.match(x) if m: all.add_host(Host(m.groups()[0], m.groups()[1])) else: if ":" in x: tokens = x.rsplit(":", 1) # if there is ':' in the address, then this is a ipv6 if ':' in tokens[0]: all.add_host(Host(x)) else: all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x)) elif os.path.exists(host_list): if os.path.isdir(host_list): # Ensure basedir is inside the directory self.host_list = os.path.join(self.host_list, "") self.parser = InventoryDirectory(filename=host_list) self.groups = self.parser.groups.values() elif utils.is_executable(host_list): self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() else: self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() utils.plugins.vars_loader.add_directory(self.basedir(), with_subdir=True) else: raise errors.AnsibleError("Unable to find an inventory file, specify one with -i ?") self._vars_plugins = [ x for x in utils.plugins.vars_loader.all(self) ]
# -*-coding:utf-8 -*-
def _parse(self, data): all = Group('all') ungrouped = Group('ungrouped') all.add_child_group(ungrouped) self.groups = dict(all=all, ungrouped=ungrouped) grouped_hosts = [] yaml = utils.parse_yaml(data) # first add all groups for item in yaml: if type(item) == dict and 'group' in item: group = Group(item['group']) for subresult in item.get('hosts',[]): if type(subresult) in [ str, unicode ]: host = self._make_host(subresult) group.add_host(host) grouped_hosts.append(host) elif type(subresult) == dict: host = self._make_host(subresult['host']) vars = subresult.get('vars',{}) if type(vars) == list: for subitem in vars: for (k,v) in subitem.items(): host.set_variable(k,v) elif type(vars) == dict: for (k,v) in subresult.get('vars',{}).items(): host.set_variable(k,v) else: raise errors.AnsibleError("unexpected type for variable") group.add_host(host) grouped_hosts.append(host) vars = item.get('vars',{}) if type(vars) == dict: for (k,v) in item.get('vars',{}).items(): group.set_variable(k,v) elif type(vars) == list: for subitem in vars: if type(subitem) != dict: raise errors.AnsibleError("expected a dictionary") for (k,v) in subitem.items(): group.set_variable(k,v) self.groups[group.name] = group all.add_child_group(group) # add host definitions for item in yaml: if type(item) in [ str, unicode ]: host = self._make_host(item) if host not in grouped_hosts: ungrouped.add_host(host) elif type(item) == dict and 'host' in item: host = self._make_host(item['host']) vars = item.get('vars', {}) if type(vars)==list: varlist, vars = vars, {} for subitem in varlist: vars.update(subitem) for (k,v) in vars.items(): host.set_variable(k,v) if host not in grouped_hosts: ungrouped.add_host(host)