Builds the inventory for the given cluster and returns its path

        :param cluster: cluster to build inventory for
        :type cluster: :py:class:`elasticluster.cluster.Cluster`

def _build_inventory(self, cluster):
        """
        Builds the inventory for the given cluster and returns its path

        :param cluster: cluster to build inventory for
        :type cluster: :py:class:`elasticluster.cluster.Cluster`
        """
        inventory_data = defaultdict(list)
        for node in cluster.get_all_nodes():
            if node.kind not in self.groups:
                # FIXME: should this raise a `ConfigurationError` instead?
                warn("Node kind `{0}` not defined in cluster!".format(node.kind))
                continue
            extra_vars = ['ansible_user=%s' % node.image_user]
            # check for nonstandard port, either IPv4 or IPv6
            if node.preferred_ip and ':' in node.preferred_ip:
                match = IPV6_RE.match(node.preferred_ip)
                if match:
                    host_port = match.groups()[1]
                else:
                    _, _, host_port = node.preferred_ip.partition(':')
                if host_port:
                    extra_vars.append('ansible_port=%s' % host_port)

            if node.kind in self.environment:
                extra_vars.extend('%s=%s' % (k, v) for k, v in
                                  self.environment[node.kind].items())
            for group in self.groups[node.kind]:
                connection_ip = node.preferred_ip
                if connection_ip:
                    inventory_data[group].append(
                        (node.name, connection_ip, str.join(' ', extra_vars)))

        if not inventory_data:
            elasticluster.log.info("No inventory file was created.")
            return None

        # create a temporary file to pass to ansible, since the
        # api is not stable yet...
        if self._storage_path_tmp:
            if not self._storage_path:
                self._storage_path = tempfile.mkdtemp()
            elasticluster.log.warning(
                "Writing inventory file to tmp dir `%s`", self._storage_path)

        inventory_path = os.path.join(
            self._storage_path, (cluster.name + '.inventory'))
        log.debug("Writing Ansible inventory to file `%s` ...", inventory_path)
        with open(inventory_path, 'w+') as inventory_file:
            for section, hosts in inventory_data.items():
                # Ansible throws an error "argument of type 'NoneType' is not
                # iterable" if a section is empty, so ensure we have something
                # to write in there
                if hosts:
                    inventory_file.write("\n[" + section + "]\n")
                    for host in hosts:
                        # don't want port, makes it look like ipv6
                        if ':' in host[1]:
                            match = IPV6_RE.match(node.preferred_ip)
                            if match:
                                host = (host[0], match.groups()[0], host[2])
                            else:
                                host = (host[0], host[1].partition(':')[0],
                                        host[2])
                        hostline = "%s ansible_host=%s %s\n" % host
                        inventory_file.write(hostline)
        return inventory_path