コード例 #1
0
ファイル: script.py プロジェクト: BenoitDherin/collaboratool
    def _parse(self, err):

        all_hosts = {}
        self.raw  = utils.parse_json(self.data)
        all       = Group('all')
        groups    = dict(all=all)
        group     = None


        if 'failed' in self.raw:
            sys.stderr.write(err + "\n")
            raise errors.AnsibleError("failed to parse executable inventory script results: %s" % self.raw)

        for (group_name, data) in self.raw.items():
 
            # in Ansible 1.3 and later, a "_meta" subelement may contain
            # a variable "hostvars" which contains a hash for each host
            # if this "hostvars" exists at all then do not call --host for each
            # host.  This is for efficiency and scripts should still return data
            # if called with --host for backwards compat with 1.2 and earlier.

            if group_name == '_meta':
                if 'hostvars' in data:
                    self.host_vars_from_top = data['hostvars']
                    continue

            group = groups[group_name] = Group(group_name)
            host = None

            if not isinstance(data, dict):
                data = {'hosts': data}
            elif not any(k in data for k in ('hosts','vars')):
                data = {'hosts': [group_name], 'vars': data}

            if 'hosts' in data:

                for hostname in data['hosts']:
                    if not hostname in all_hosts:
                        all_hosts[hostname] = Host(hostname)
                    host = all_hosts[hostname]
                    group.add_host(host)

            if 'vars' in data:
                for k, v in data['vars'].iteritems():
                    if group.name == all.name:
                        all.set_variable(k, v)
                    else:
                        group.set_variable(k, v)
            if group.name != all.name:
                all.add_child_group(group)

        # Separate loop to ensure all groups are defined
        for (group_name, data) in self.raw.items():
            if group_name == '_meta':
                continue
            if isinstance(data, dict) and 'children' in data:
                for child_name in data['children']:
                    if child_name in groups:
                        groups[group_name].add_child_group(groups[child_name])
        return groups
コード例 #2
0
    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
コード例 #3
0
ファイル: ini.py プロジェクト: broferek/ansible
    def _parse_base_groups(self):
        # FIXME: refactor

        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 = shlex.split(line)
                if len(tokens) == 0:
                    continue
                hostname = tokens[0]
                port = C.DEFAULT_REMOTE_PORT
                # Two cases to check:
                # 0. A hostname that contains a range pesudo-code and a port
                # 1. A hostname that contains just a port
                if (hostname.find("[") != -1 and
                    hostname.find("]") != -1 and
                    hostname.find(":") != -1 and
                    (hostname.rindex("]") < hostname.rindex(":")) or
                    (hostname.find("]") == -1 and hostname.find(":") != -1)):
                        tokens2  = hostname.rsplit(":", 1)
                        hostname = tokens2[0]
                        port     = tokens2[1]

                host = None
                _all_hosts = []
                if hostname in self.hosts:
                    host = self.hosts[hostname]
                    _all_hosts.append(host)
                else:
                    if detect_range(hostname):
                        _hosts = expand_hostname_range(hostname)
                        for _ in _hosts:
                            host = Host(name=_, port=port)
                            self.hosts[_] = host
                            _all_hosts.append(host)
                    else:
                        host = Host(name=hostname, port=port)
                        self.hosts[hostname] = host
                        _all_hosts.append(host)
                if len(tokens) > 1:
                    for t in tokens[1:]:
                        (k,v) = t.split("=")
                        host.set_variable(k,v)
                for _ in _all_hosts:
                    self.groups[active_group_name].add_host(_)
コード例 #4
0
ファイル: script.py プロジェクト: Minione/iwct
    def _parse(self):
        all_hosts = {}

        self.raw = utils.parse_json(self.data)
        all=Group('all')
        groups = dict(all=all)
        group = None
        for (group_name, data) in self.raw.items():
            group = groups[group_name] = Group(group_name)
            host = None
            if not isinstance(data, dict):
                data = {'hosts': data}
            if 'hosts' in data:
                for hostname in data['hosts']:
                    if not hostname in all_hosts:
                        all_hosts[hostname] = Host(hostname)
                    host = all_hosts[hostname]
                    group.add_host(host)
            if 'vars' in data:
                for k, v in data['vars'].iteritems():
                    group.set_variable(k, v)
            all.add_child_group(group)
        # Separate loop to ensure all groups are defined
        for (group_name, data) in self.raw.items():
            if isinstance(data, dict) and 'children' in data:
                for child_name in data['children']:
                    if child_name in groups:
                        groups[group_name].add_child_group(groups[child_name])
        return groups
コード例 #5
0
ファイル: stac-prepare-nodes.py プロジェクト: ekuric/svt
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=(',', ': '))
コード例 #6
0
ファイル: ansible_api.py プロジェクト: hatmen/pystudy
    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)
コード例 #7
0
ファイル: ansible_api.py プロジェクト: jinrenlab/MagicStack
    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)
コード例 #8
0
ファイル: test_group.py プロジェクト: awiddersheim/ansible
 def test_depth_update(self):
     A = Group('A')
     B = Group('B')
     Z = Group('Z')
     A.add_child_group(B)
     A.add_child_group(Z)
     self.assertEqual(A.depth, 0)
     self.assertEqual(Z.depth, 1)
     self.assertEqual(B.depth, 1)
コード例 #9
0
ファイル: __init__.py プロジェクト: davismathew/stableansible
    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))
コード例 #10
0
ファイル: test_group.py プロジェクト: awiddersheim/ansible
 def test_ancestors_recursive_loop_safe(self):
     '''
     The get_ancestors method may be referenced before circular parenting
     checks, so the method is expected to be stable even with loops
     '''
     A = Group('A')
     B = Group('B')
     A.parent_groups.append(B)
     B.parent_groups.append(A)
     # finishes in finite time
     self.assertEqual(A.get_ancestors(), set([A, B]))
コード例 #11
0
ファイル: test_group.py プロジェクト: awiddersheim/ansible
 def test_loop_detection(self):
     A = Group('A')
     B = Group('B')
     C = Group('C')
     A.add_child_group(B)
     B.add_child_group(C)
     with self.assertRaises(AnsibleError):
         C.add_child_group(A)
コード例 #12
0
ファイル: __init__.py プロジェクト: Minione/iwct
    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")
コード例 #13
0
ファイル: host.py プロジェクト: JaredPennella/DevOps_Script
    def deserialize(self, data):
        self.__init__()

        self.name    = data.get('name')
        self.vars    = data.get('vars', dict())
        self.address = data.get('address', '')

        groups = data.get('groups', [])
        for group_data in groups:
            g = Group()
            g.deserialize(group_data)
            self.groups.append(g)
コード例 #14
0
ファイル: host.py プロジェクト: ernstp/ansible
    def deserialize(self, data):
        self.__init__(gen_uuid=False)

        self.name = data.get('name')
        self.vars = data.get('vars', dict())
        self.address = data.get('address', '')
        self._uuid = data.get('uuid', None)
        self.implicit = data.get('implicit', False)

        groups = data.get('groups', [])
        for group_data in groups:
            g = Group()
            g.deserialize(group_data)
            self.groups.append(g)
コード例 #15
0
    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)
コード例 #16
0
ファイル: __init__.py プロジェクト: thebeefcake/masterless
    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))
コード例 #17
0
ファイル: network-test.py プロジェクト: jmencak/svt
 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()
コード例 #18
0
    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)
コード例 #19
0
    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))
コード例 #20
0
ファイル: script.py プロジェクト: Kafkamorph/n-repo
 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  
コード例 #21
0
ファイル: stac-prepare-nodes.py プロジェクト: ekuric/svt
    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()
コード例 #22
0
    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()
コード例 #23
0
ファイル: __init__.py プロジェクト: arizvisa/ansible
 def _create_implicit_localhost(self, pattern):
     new_host = Host(pattern)
     new_host._connection = 'local'
     new_host.set_variable("ansible_python_interpreter", sys.executable)
     ungrouped = self.get_group("ungrouped")
     if ungrouped is None:
         self.add_group(Group('ungrouped'))
         ungrouped = self.get_group('ungrouped')
         self.get_group('all').add_child_group(ungrouped)
     ungrouped.add_host(new_host)
     return new_host
コード例 #24
0
def add_host(inventory, group, host):
    if version_info < (2, 4):
        from ansible.inventory.host import Host
        from ansible.inventory.group import Group
        if group not in inventory.list_groups():
            inventory.add_group(Group(group))
        inventory.get_group(group).add_host(Host(host))
    else:
        if group not in inventory.list_groups():
            inventory.add_group(group)
        inventory.add_host(host, group)
コード例 #25
0
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
コード例 #26
0
ファイル: __init__.py プロジェクト: bopopescu/ansible-1
 def _create_implicit_localhost(self, pattern):
     # 如果pattern不在all礼拜中,则创建新的local Host,并添加到ungrouped组(没有则创建),然后返回Host对象
     new_host = Host(pattern)
     new_host.set_variable("ansible_python_interpreter", sys.executable)
     new_host.set_variable("ansible_connection", "local")
     ungrouped = self.get_group("ungrouped")
     if ungrouped is None:
         self.add_group(Group('ungrouped'))
         ungrouped = self.get_group('ungrouped')
         self.get_group('all').add_child_group(ungrouped)
     ungrouped.add_host(new_host)
     return new_host
コード例 #27
0
    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.items():
                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.items():
                if key not in ['hostname', 'port', 'username', 'password']:
                    my_host.set_variable(key, value)
            # add to group
            my_group.add_host(my_host)

        self.add_group(my_group)
コード例 #28
0
ファイル: ini.py プロジェクト: Kafkamorph/n-repo
    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)
コード例 #29
0
    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
コード例 #30
0
    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
コード例 #31
0
    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))
コード例 #32
0
    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)
コード例 #33
0
ファイル: __init__.py プロジェクト: Kafkamorph/n-repo
    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()
コード例 #34
0
ファイル: __init__.py プロジェクト: kentfrazier/ansible
    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)]
コード例 #35
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

        # 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) ]
コード例 #36
0
ファイル: inventory.py プロジェクト: leowmjw/bang
def get_ansible_groups(group_map):
    """
    Constructs a list of :class:`ansible.inventory.group.Group` objects from a
    map of lists of host strings.

    """
    # Much of this logic is cribbed from
    # ansible.inventory.script.InventoryScript
    all_hosts = {}
    group_all = Group('all')
    group_all.add_host(Host('127.0.0.1'))
    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
コード例 #37
0
    def _parse(self):

        all_hosts = {}
        self.raw  = utils.parse_json(self.data)
        all       = Group('all')
        groups    = dict(all=all)
        group     = None

        if 'failed' in self.raw:
            raise errors.AnsibleError("failed to parse executable inventory script results")

        for (group_name, data) in self.raw.items():

            group = groups[group_name] = Group(group_name)
            host = None

            if not isinstance(data, dict):
                data = {'hosts': data}

            if 'hosts' in data:

                for hostname in data['hosts']:
                    if not hostname in all_hosts:
                        all_hosts[hostname] = Host(hostname)
                    host = all_hosts[hostname]
                    group.add_host(host)

            if 'vars' in data:
                for k, v in data['vars'].iteritems():
                    group.set_variable(k, v)
            all.add_child_group(group)

        # Separate loop to ensure all groups are defined
        for (group_name, data) in self.raw.items():
            if isinstance(data, dict) and 'children' in data:
                for child_name in data['children']:
                    if child_name in groups:
                        groups[group_name].add_child_group(groups[child_name])
        return groups
コード例 #38
0
ファイル: add_host.py プロジェクト: kentfrazier/ansible
    def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):

        if self.runner.check:
            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
        
        # create host and get inventory    
        new_host = Host(new_name)
        inventory = self.runner.inventory
        
        # Add any 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]) 
                
        
        # add the new host to the 'all' group
        allgroup = inventory.get_group('all')
        allgroup.add_host(new_host)
       
        groupnames = args.get('groupname', args.get('groups', '')) 
        # add it to the group if that was specified
        if groupnames != '':
            for group_name in groupnames.split(","):
                if not inventory.get_group(group_name):
                    new_group = Group(group_name)
                    inventory.add_group(new_group)
                grp = inventory.get_group(group_name)
                grp.add_host(new_host)
            vv("added host to group via add_host module: %s" % group_name)
            result['new_groups'] = groupnames.split(",")
            
        result['new_host'] = new_name
        
        return ReturnData(conn=conn, comm_ok=True, result=result)
コード例 #39
0
    def add_group(self, group):
        ''' adds a group to inventory if not there already '''

        if group:
            if group not in self.groups:
                g = Group(group)
                self.groups[group] = g
                self._groups_dict_cache = {}
                display.debug("Added group %s to inventory" % group)
            else:
                display.debug("group %s already in inventory" % group)
        else:
            raise AnsibleError("Invalid empty/false group name provided: %s" %
                               group)
コード例 #40
0
ファイル: __init__.py プロジェクト: victron/paramiko_ssh-i
    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)
コード例 #41
0
    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)
コード例 #42
0
    def __init__(self, filename=C.DEFAULT_HOST_LIST):
        self.filename = filename

        # Start with an empty host list and the default 'all' and
        # 'ungrouped' groups.

        self.hosts = {}
        self.patterns = {}
        self.groups = dict(all=Group(name='all'),
                           ungrouped=Group(name='ungrouped'))

        # Read in the hosts, groups, and variables defined in the
        # inventory file.

        with open(filename) as fh:
            self._parse(fh.readlines())

        # Finally, add all top-level groups (including 'ungrouped') as
        # children of 'all'.

        for group in self.groups.values():
            if group.depth == 0 and group.name != 'all':
                self.groups['all'].add_child_group(group)
コード例 #43
0
 def test_depth_update(self):
     A = Group('A')
     B = Group('B')
     Z = Group('Z')
     A.add_child_group(B)
     A.add_child_group(Z)
     self.assertEqual(A.depth, 0)
     self.assertEqual(Z.depth, 1)
     self.assertEqual(B.depth, 1)
コード例 #44
0
ファイル: data.py プロジェクト: zengjie/ansible
    def add_group(self, group):
        ''' adds a group to inventory if not there already '''

        if group:
            if not isinstance(group, string_types):
                raise AnsibleError("Invalid group name supplied, expected a string but got %s for %s" % (type(group), group))
            if group not in self.groups:
                g = Group(group)
                self.groups[group] = g
                self._groups_dict_cache = {}
                display.debug("Added group %s to inventory" % group)
            else:
                display.debug("group %s already in inventory" % group)
        else:
            raise AnsibleError("Invalid empty/false group name provided: %s" % group)
コード例 #45
0
ファイル: test_group.py プロジェクト: awiddersheim/ansible
 def test_depth_recursion(self):
     A = Group('A')
     B = Group('B')
     A.add_child_group(B)
     # hypothetical of adding B as child group to A
     A.parent_groups.append(B)
     B.child_groups.append(A)
     # can't update depths of groups, because of loop
     with self.assertRaises(AnsibleError):
         B._check_children_depth()
コード例 #46
0
ファイル: dir.py プロジェクト: zsolt-erl/ansible
    def __init__(self, filename=C.DEFAULT_HOST_LIST):
        self.names = os.listdir(filename)
        self.names.sort()
        self.directory = filename
        self.parsers = []
        self.hosts = {}
        self.groups = {}

        for i in self.names:

            if i.endswith("~") or i.endswith(".orig") or i.endswith(".bak"):
                continue
            if i.endswith(".ini"):
                # configuration file for an inventory script
                continue
            if i.endswith(".retry"):
                # this file is generated on a failed playbook and should only be
                # used when run specifically
                continue
            # These are things inside of an inventory basedir
            if i in ("host_vars", "group_vars", "vars_plugins"):
                continue
            fullpath = os.path.join(self.directory, i)
            if os.path.isdir(fullpath):
                parser = InventoryDirectory(filename=fullpath)
            elif utils.is_executable(fullpath):
                parser = InventoryScript(filename=fullpath)
            else:
                parser = InventoryParser(filename=fullpath)
            self.parsers.append(parser)
            # This takes a lot of code because we can't directly use any of the objects, as they have to blend
            for name, group in parser.groups.iteritems():
                if name not in self.groups:
                    self.groups[name] = Group(name)
                for k, v in group.get_variables().iteritems():
                    self.groups[name].set_variable(k, v)
                for host in group.get_hosts():
                    if host.name not in self.hosts:
                        self.hosts[host.name] = Host(host.name)
                    for k, v in host.vars.iteritems():
                        self.hosts[host.name].set_variable(k, v)
                    self.groups[name].add_host(self.hosts[host.name])
            # This needs to be a second loop to ensure all the parent groups exist
            for name, group in parser.groups.iteritems():
                for ancestor in group.get_ancestors():
                    self.groups[ancestor.name].add_child_group(
                        self.groups[name])
コード例 #47
0
ファイル: script.py プロジェクト: ucxdvd/n-repo
 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
コード例 #48
0
ファイル: __init__.py プロジェクト: dcoutu/ansible
    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"
                )
コード例 #49
0
ファイル: network-test.py プロジェクト: hroyrh/svt
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=(',', ': '))
コード例 #50
0
ファイル: ini.py プロジェクト: ucxdvd/n-repo
    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)
コード例 #51
0
    def _parse(self, data):
        '''
        Populates self.groups from the given array of lines. Raises an error on
        any parse failure.
        '''

        self._compile_patterns()

        # We expect top level keys to correspond to groups, iterate over them
        # to get host, vars and subgroups (which we iterate over recursivelly)
        for group_name in data.keys():
            self._parse_groups(group_name, data[group_name])

        # Finally, add all top-level groups as children of 'all'.
        # We exclude ungrouped here because it was already added as a child of
        # 'all' at the time it was created.
        for group in self.groups.values():
            if group.depth == 0 and group.name not in ('all', 'ungrouped'):
                self.groups['all'].add_child_group(Group(group_name))
コード例 #52
0
ファイル: network-test.py プロジェクト: pmarhatha/svt
    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()
コード例 #53
0
ファイル: stac-test.py プロジェクト: pmarhatha/svt
    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()
コード例 #54
0
    def test_ancestor_example(self):
        # see docstring for Group._walk_relationship
        groups = {}
        for name in ['A', 'B', 'C', 'D', 'E', 'F']:
            groups[name] = Group(name)
        # first row
        groups['A'].add_child_group(groups['D'])
        groups['B'].add_child_group(groups['D'])
        groups['B'].add_child_group(groups['E'])
        groups['C'].add_child_group(groups['D'])
        # second row
        groups['D'].add_child_group(groups['E'])
        groups['D'].add_child_group(groups['F'])
        groups['E'].add_child_group(groups['F'])

        self.assertEqual(
            set(groups['F'].get_ancestors()),
            set([
                groups['A'], groups['B'], groups['C'], groups['D'], groups['E']
            ]))
コード例 #55
0
ファイル: network-test.py プロジェクト: zvonkok/svt
 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)
コード例 #56
0
ファイル: stac-test.py プロジェクト: pmarhatha/svt
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=(',', ': '))
コード例 #57
0
ファイル: network-test.py プロジェクト: pmarhatha/svt
class NetworkTest(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=(',', ': '))
コード例 #58
0
ファイル: ini.py プロジェクト: zakkak/ansible
    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 ":children]" in line:
                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("#") or line.startswith(";"):
                pass
            elif line.startswith("["):
                group = None
            elif group:
                kid_group = self.groups.get(line, None)
                if kid_group is None:
                    raise AnsibleError("child group is not defined: (%s)" % line)
                else:
                    group.add_child_group(kid_group)
コード例 #59
0
    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)
コード例 #60
0
ファイル: __init__.py プロジェクト: dataxu/ansible
    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))