示例#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 _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
示例#3
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()
示例#4
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)
示例#5
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()
示例#6
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)