示例#1
0
 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  
示例#2
0
    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()
示例#3
0
 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
示例#4
0
    def _parse_base_groups(self):

        ungrouped = Group(name='ungrouped')
        all = Group(name='all')
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)
        active_group_name = 'ungrouped'

        for line in self.lines:
            if line.startswith("["):
                active_group_name = line.replace("[","").replace("]","").strip()
                if line.find(":vars") != -1 or line.find(":children") != -1:
                    active_group_name = None
                else:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    all.add_child_group(new_group)
            elif line.startswith("#") or line == '':
                pass
            elif active_group_name:
                tokens = line.split()
                if len(tokens) == 0:
                    continue
                hostname = tokens[0]
                port = C.DEFAULT_REMOTE_PORT
                if hostname.find(":") != -1:
                   tokens2  = hostname.split(":")
                   hostname = tokens2[0]
                   port     = tokens2[1]
                host = None
                if hostname in self.hosts:
                    host = self.hosts[hostname]
                else:
                    host = Host(name=hostname, port=port)
                    self.hosts[hostname] = host
                if len(tokens) > 1:
                   for t in tokens[1:]:
                      (k,v) = t.split("=")
                      host.set_variable(k,v)
                self.groups[active_group_name].add_host(host)
示例#5
0
    def _parse_group_children(self):
        group = None

        for line in self.lines:
            line = line.strip()
            if line is None or line == '':
                continue
            if line.startswith("[") and line.find(":children]") != -1:
                line = line.replace("[","").replace(":children]","")
                group = self.groups.get(line, None)
                if group is None:
                    group = self.groups[line] = Group(name=line)
            elif line.startswith("#"):
                pass
            elif line.startswith("["):
                group = None
            elif group:
                kid_group = self.groups.get(line, None)
                if kid_group is None:
                    raise errors.AnsibleError("child group is not defined: (%s)" % line)
                else:
                    group.add_child_group(kid_group)
示例#6
0
    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()
示例#7
0
    def _parse(self, data):

        all = Group("all")
        ungrouped = Group("ungrouped")
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)

        yaml = utils.parse_yaml(data)
        for item in yaml:

            if type(item) in [str, unicode]:
                host = self._make_host(item)
                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)

            elif 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)
                    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)

                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)
示例#8
0
    def _parse(self, data):

        all = Group('all')
        ungrouped = Group('ungrouped')
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)

        yaml = utils.parse_yaml(data)
        for item in yaml:

            if type(item) in [str, unicode]:
                host = self._make_host(item)
                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)

            elif 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)
                    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)

                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)