def convert_hosts(self):
        """
        @summary:这个位置并没有考虑 添加主机组的情况,需要修改
        :return:
        """
        for host in self.resource_list:
            if host['hostname'] in self.inventory.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)
                self.inventory.hosts[hostname] = my_host
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
    def _create_implicit_localhost(self, pattern):

        if self.localhost:
            new_host = self.localhost
        else:
            new_host = Host(pattern)

            new_host.address = "127.0.0.1"
            new_host.implicit = True

            # set localhost defaults
            py_interp = sys.executable
            if not py_interp:
                # sys.executable is not set in some cornercases. see issue #13585
                py_interp = '/usr/bin/python'
                display.warning(
                    'Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. '
                    'You can correct this by setting ansible_python_interpreter for localhost'
                )
            new_host.set_variable("ansible_python_interpreter", py_interp)
            new_host.set_variable("ansible_connection", 'local')

            self.localhost = new_host

        return new_host
Beispiel #5
0
    def __init__(self, vars_manager, play, inventory, loader):
        self._lookup = {}
        self._loader = loader

        # temporarily remove the inventory filter restriction
        # so we can compile the variables for all of the hosts
        # in inventory
        restriction = inventory._restriction
        inventory.remove_restriction()
        hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)
        inventory.restrict_to_hosts(restriction)

        # check to see if localhost is in the hosts list, as we
        # may have it referenced via hostvars but if created implicitly
        # it doesn't sow up in the hosts list
        has_localhost = False
        for host in hosts:
            if host.name in C.LOCALHOST:
                has_localhost = True
                break

        # we don't use the method in inventory to create the implicit host,
        # because it also adds it to the 'ungrouped' group, and we want to
        # avoid any side-effects
        if not has_localhost:
            new_host =  Host(name='localhost')
            new_host.set_variable("ansible_python_interpreter", sys.executable)
            new_host.set_variable("ansible_connection", "local")
            new_host.ipv4_address = '127.0.0.1'
            hosts.append(new_host)

        for host in hosts:
            self._lookup[host.name] = vars_manager.get_vars(loader=loader, play=play, host=host, include_hostvars=False)
    def add_host(self, host_ip, add_to_root=True, group_list=[], var_list={}):
        """Add a host ip to the ansible host file
        This is a simple function to add hosts to
        add_to_root = Adds the host to the root group (unnamed)
        groups_list: List of groupnames where the host should appears.
        var_list: Variable list. see allowed_variables."""
        #root group in unnamed, but in the inventory object
        # is the 'ungrouped' group
        self.__dirty = True
        new_host = Host(host_ip)
        for key,value in var_list.iteritems():
            if self.is_allowed_variable(key):
                new_host.set_variable(key,value)
        if add_to_root:
            if 'ungrouped' not in group_list:
                group_list.append('ungrouped')

        #Check groups. The ansible inventory should contain each of the groups.
        for group in group_list:
            if not self.__inventory.get_group(group):
                new_group= Group(group)
                self.__inventory.add_group(new_group)
            grp = self.__inventory.get_group(group)
            host_names = [host.name for host in grp.get_hosts()]
            if new_host.name not in host_names:
                grp.add_host(new_host)
    def __init__(self, play, inventory, variable_manager, loader):
        self._lookup = dict()
        self._loader = loader
        self._play = play
        self._variable_manager = variable_manager

        hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)

        # check to see if localhost is in the hosts list, as we
        # may have it referenced via hostvars but if created implicitly
        # it doesn't sow up in the hosts list
        has_localhost = False
        for host in hosts:
            if host.name in C.LOCALHOST:
                has_localhost = True
                break

        if not has_localhost:
            new_host =  Host(name='localhost')
            new_host.set_variable("ansible_python_interpreter", sys.executable)
            new_host.set_variable("ansible_connection", "local")
            new_host.address = '127.0.0.1'
            hosts.append(new_host)

        for host in hosts:
            self._lookup[host.name] = host
Beispiel #8
0
    def _create_implicit_localhost(self, pattern):

        if self.localhost:
            new_host = self.localhost
        else:
            new_host = Host(pattern)

            # use 'all' vars but not part of all group
            new_host.vars = self.groups['all'].get_vars()

            new_host.address = "127.0.0.1"
            new_host.implicit = True

            if "ansible_python_interpreter" not in new_host.vars:
                py_interp = sys.executable
                if not py_interp:
                    # sys.executable is not set in some cornercases.  #13585
                    py_interp = '/usr/bin/python'
                    display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. '
                                    'You can correct this by setting ansible_python_interpreter for localhost')
                new_host.set_variable("ansible_python_interpreter", py_interp)

            if "ansible_connection" not in new_host.vars:
                new_host.set_variable("ansible_connection", 'local')

            self.localhost = new_host

        return new_host
Beispiel #9
0
    def __init__(self, play, inventory, variable_manager, loader):
        self._lookup = dict()
        self._loader = loader
        self._play = play
        self._variable_manager = variable_manager
        self._cached_result = dict()

        hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)

        # check to see if localhost is in the hosts list, as we
        # may have it referenced via hostvars but if created implicitly
        # it doesn't sow up in the hosts list
        has_localhost = False
        for host in hosts:
            if host.name in C.LOCALHOST:
                has_localhost = True
                break

        if not has_localhost:
            new_host = Host(name='localhost')
            new_host.set_variable("ansible_python_interpreter", sys.executable)
            new_host.set_variable("ansible_connection", "local")
            new_host.address = '127.0.0.1'
            hosts.append(new_host)

        for host in hosts:
            self._lookup[host.name] = host
Beispiel #10
0
    def _hosts_in_unenumerated_pattern(self, pattern):
        """ Get all host names matching the pattern """

        hosts = []
        hostnames = set()

        # ignore any negative checks here, this is handled elsewhere
        pattern = pattern.replace("!", "").replace("&", "")

        results = []
        groups = self.get_groups()
        for group in groups:
            for host in group.get_hosts():
                if pattern == 'all' or self._match(group.name,
                                                   pattern) or self._match(
                                                       host.name, pattern):
                    if host not in results and host.name not in hostnames:
                        results.append(host)
                        hostnames.add(host.name)

        if pattern in ["localhost", "127.0.0.1"] and len(results) == 0:
            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')

            ungrouped.add_host(new_host)
            results.append(new_host)
        return results
Beispiel #11
0
    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)
Beispiel #12
0
 def _create_implicit_localhost(self, pattern):
     new_host = Host(pattern)
     new_host.set_variable("ansible_python_interpreter", sys.executable)
     new_host.set_variable("ansible_connection", "local")
     new_host.address = '127.0.0.1'
     self.get_group("ungrouped").add_host(new_host)
     return new_host
Beispiel #13
0
    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(_)
Beispiel #14
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)
Beispiel #15
0
    def _create_implicit_localhost(self, pattern):

        if self.localhost:
            new_host = self.localhost
        else:
            new_host = Host(pattern)

            # use 'all' vars but not part of all group
            new_host.vars = self.groups['all'].get_vars()

            new_host.address = "127.0.0.1"
            new_host.implicit = True

            if "ansible_python_interpreter" not in new_host.vars:
                py_interp = sys.executable
                if not py_interp:
                    # sys.executable is not set in some cornercases.  #13585
                    py_interp = '/usr/bin/python'
                    display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. '
                                    'You can correct this by setting ansible_python_interpreter for localhost')
                new_host.set_variable("ansible_python_interpreter", py_interp)

            if "ansible_connection" not in new_host.vars:
                new_host.set_variable("ansible_connection", 'local')

            self.localhost = new_host

        return new_host
Beispiel #16
0
 def _create_implicit_localhost(self, pattern):
     new_host = Host(pattern)
     new_host.address = "127.0.0.1"
     new_host.vars = self.get_host_vars(new_host)
     new_host.set_variable("ansible_connection", "local")
     if "ansible_python_interpreter" not in new_host.vars:
         new_host.set_variable("ansible_python_interpreter", sys.executable)
     self.get_group("ungrouped").add_host(new_host)
     return new_host
Beispiel #17
0
 def _create_implicit_localhost(self, pattern='localhost'):
     new_host = Host(pattern)
     new_host.address = "127.0.0.1"
     new_host.implicit = True
     new_host.vars = self.get_host_vars(new_host)
     new_host.set_variable("ansible_connection", "local")
     if "ansible_python_interpreter" not in new_host.vars:
         new_host.set_variable("ansible_python_interpreter", sys.executable)
     self.get_group("ungrouped").add_host(new_host)
     return new_host
Beispiel #18
0
 def _create_implicit_localhost(self, pattern):
     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')
     ungrouped.add_host(new_host)
     return new_host
Beispiel #19
0
 def _create_implicit_localhost(self, pattern):
     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')
     ungrouped.add_host(new_host)
     return new_host
Beispiel #20
0
 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
Beispiel #21
0
 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
Beispiel #22
0
    def _create_implicit_localhost(self, pattern):
        new_host = Host(pattern)
        new_host.set_variable("ansible_python_interpreter", sys.executable)
        new_host.set_variable("ansible_connection", "local")
        new_host.ipv4_address = "127.0.0.1"

        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
Beispiel #23
0
    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)
 def _create_implicit_localhost(self, pattern='localhost'):
     new_host = Host(pattern)
     new_host.address = "127.0.0.1"
     new_host.implicit = True
     new_host.vars = self.get_host_vars(new_host)
     new_host.set_variable("ansible_connection", "local")
     if "ansible_python_interpreter" not in new_host.vars:
         py_interp = sys.executable
         if not py_interp:
             # sys.executable is not set in some cornercases.  #13585
             display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. You can correct this by setting ansible_python_interpreter for localhost')
             py_interp = '/usr/bin/python'
         new_host.set_variable("ansible_python_interpreter", py_interp)
     self.get_group("ungrouped").add_host(new_host)
     return new_host
Beispiel #25
0
 def _create_implicit_localhost(self, pattern='localhost'):
     new_host = Host(pattern)
     new_host.address = "127.0.0.1"
     new_host.implicit = True
     new_host.vars = self.get_host_vars(new_host)
     new_host.set_variable("ansible_connection", "local")
     if "ansible_python_interpreter" not in new_host.vars:
         py_interp = sys.executable
         if not py_interp:
             # sys.executable is not set in some cornercases.  #13585
             display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. You can correct this by setting ansible_python_interpreter for localhost')
             py_interp = '/usr/bin/python'
         new_host.set_variable("ansible_python_interpreter", py_interp)
     self.get_group("ungrouped").add_host(new_host)
     return new_host
Beispiel #26
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)
Beispiel #27
0
    def _parse_base_groups(self):

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

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

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

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

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

        for line in self.lines:
            if line.startswith("["):
                active_group_name = line.replace("[","").replace("]","").strip()
                if line.find(":vars") != -1 or line.find(":children") != -1:
                    active_group_name = None
                else:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    all.add_child_group(new_group)
            elif line.startswith("#") or line == '':
                pass
            elif active_group_name:
                tokens = line.split()
                if len(tokens) == 0:
                    continue
                hostname = tokens[0]
                port = C.DEFAULT_REMOTE_PORT
                if hostname.find(":") != -1:
                   tokens2  = hostname.split(":")
                   hostname = tokens2[0]
                   port     = tokens2[1]
                host = None
                if hostname in self.hosts:
                    host = self.hosts[hostname]
                else:
                    host = Host(name=hostname, port=port)
                    self.hosts[hostname] = host
                if len(tokens) > 1:
                   for t in tokens[1:]:
                      (k,v) = t.split("=")
                      host.set_variable(k,v)
                self.groups[active_group_name].add_host(host)
Beispiel #29
0
 def gen_host(host_name=None, host_vars=None):
     """
     Generate ansible Host object
     :param host_name: <string> ansible inventory hostname
     :param host_vars: <dict> host variables
     :return: Host object
     """
     if host_vars is None:
         host_vars = {}
     ssh_host = host_vars.get('ip', host_name)
     ssh_port = host_vars.get('port', ANS_CONS.DEFAULT_REMOTE_PORT)
     ssh_user = host_vars.get('username')
     ssh_pass = host_vars.get('password')
     ssh_fkey = host_vars.get('ssh_key')
     # init Host
     host = Host(name=host_name, port=ssh_port)
     host.set_variable('ansible_ssh_host', ssh_host)
     # shortcut variables
     ssh_user and host.set_variable('ansible_ssh_user', ssh_user)
     ssh_pass and host.set_variable('ansible_ssh_pass', ssh_pass)
     ssh_fkey and host.set_variable('ansible_private_key_file', ssh_fkey)
     # extra variables
     for key, value in host_vars.items():
         if key not in ['ip', 'port', 'username', 'password', 'ssh_key']:
             host.set_variable(key, value)
     # return Host object
     return host
Beispiel #30
0
def run_module(hosts, module_name, args, **complex_args):
    import ansible.inventory
    pattern = "all"
    inventory = ansible.inventory.Inventory(',')
    
    all_group = inventory.get_group("all")
    from ansible.inventory.host import Host
    for h in hosts:
        host = Host(h["host"], h.get("port"))
        for key, value in h["settings"].iteritems():
            host.set_variable(key, value)
    
        all_group.add_host(host)
    
    # в конструкторе кэш посчитался заранее
    inventory.clear_pattern_cache()
    
    # сам Ansible использует отдельный Runner для каждого действия в playbook,
    # так что вот готов отдельный кирпичик для системы развертывания
    import ansible.runner 
    runner = ansible.runner.Runner(
        module_name=module_name,
        module_args=args,
        complex_args=complex_args,
        pattern=pattern,
        inventory=inventory,
        
    )
    results = runner.run()
    
    # :COPY_N_PASTE_REALIZATION: bin/ansible
    if results['dark']:
        assert False, "Couldn't connect to %s" % results['dark']
    total = 0
    for result in results['contacted'].values():
        if 'failed' in result or result.get('rc', 0) != 0:
            assert False, result
        total += 1
    assert total == len(hosts), "The host was not connected to"
Beispiel #31
0
def run_module(hosts, module_name, args, **complex_args):
    import ansible.inventory
    pattern = "all"
    inventory = ansible.inventory.Inventory(',')

    all_group = inventory.get_group("all")
    from ansible.inventory.host import Host
    for h in hosts:
        host = Host(h["host"], h.get("port"))
        for key, value in h["settings"].iteritems():
            host.set_variable(key, value)

        all_group.add_host(host)

    # в конструкторе кэш посчитался заранее
    inventory.clear_pattern_cache()

    # сам Ansible использует отдельный Runner для каждого действия в playbook,
    # так что вот готов отдельный кирпичик для системы развертывания
    import ansible.runner
    runner = ansible.runner.Runner(
        module_name=module_name,
        module_args=args,
        complex_args=complex_args,
        pattern=pattern,
        inventory=inventory,
    )
    results = runner.run()

    # :COPY_N_PASTE_REALIZATION: bin/ansible
    if results['dark']:
        assert False, "Couldn't connect to %s" % results['dark']
    total = 0
    for result in results['contacted'].values():
        if 'failed' in result or result.get('rc', 0) != 0:
            assert False, result
        total += 1
    assert total == len(hosts), "The host was not connected to"
Beispiel #32
0
    def _create_implicit_localhost(self, pattern):

        if self.localhost:
            new_host = self.localhost
        else:
            new_host = Host(pattern)

            new_host.address = "127.0.0.1"
            new_host.implicit = True

            # set localhost defaults
            py_interp = sys.executable
            if not py_interp:
                # sys.executable is not set in some cornercases. see issue #13585
                py_interp = '/usr/bin/python'
                display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. '
                                'You can correct this by setting ansible_python_interpreter for localhost')
            new_host.set_variable("ansible_python_interpreter", py_interp)
            new_host.set_variable("ansible_connection", 'local')

            self.localhost = new_host

        return new_host
    def gen_hosts(hosts=None):
        """
        if host_vars exists then, generate hosts

        Args:
             hosts: <list> [<host variable dict>, <host variable dict>, ...]
        Returns:
             host_objs: <list> [<host object>, <host object>, ...]
        """
        assert isinstance(hosts, list), "the hosts must be a list"
        host_objs = []
        if hosts:
            for host in hosts:
                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)

                if password:
                    my_host.set_variable('ansible_ssh_pass', password)
                if ssh_key:
                    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", "ip", "ssh_key"]:
                        my_host.set_variable(key, value)
                host_objs.append(my_host)
        return host_objs
Beispiel #34
0
    def _host_model_to_Host(self, host_model):
        host = Host(host_model.ip, host_model.ssh_port)
        host.set_variable('ansible_ssh_port', host_model.ssh_port)
        host.set_variable('ansible_ssh_user', host_model.ssh_user)
        host.set_variable('ansible_ssh_pass', host_model.ssh_password)

        var_dic = json.loads(host_model.var)
        for key in var_dic:
            host.set_variable(key, var_dic.get(key))

        return host
Beispiel #35
0
    def _hostModel2Host(self, hostModel):
        h = Host(hostModel.name, hostModel.ssh_port)
        h.set_variable('ansible_ssh_user', hostModel.ssh_user)
        h.set_variable('ansible_ssh_pass', hostModel.ssh_password)
        h.address = hostModel.ip

        if hostModel.var is not None:
            var_dic = json.loads(hostModel.var)
            for key in var_dic:
                value = var_dic.get(key)
                h.set_variable(key, value)

        return h
Beispiel #36
0
    def my_add_group(self, hosts, groupname, groupvars=None):
        """
        add hosts to a group
        """
        self.add_group(groupname)
        group_dict = self.get_groups_dict()
        my_group = group_dict[groupname]
        # if group variables exists, add them to group
        if groupvars:
            for key in groupvars:
                value = groupvars.get(key)
                my_group.set_variable(key, value)

                # add hosts to group
        for host in hosts:
            # set connection variables
            host_ip = host.get('ip')
            host_port = '22'
            username = '******'
            if 'port' in host:
                host_port = host.get("port")
            if 'username' in host:
                username = host.get("username")
            password = host.get("password")
            my_host = Host(name=host_ip, port=host_port)
            my_host.set_variable('ansible_ssh_port', host_port)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)

            self.add_host(host_ip, group=groupname, port=host_port)
            self._inventory.set_variable(host_ip, 'ansible_ssh_port',
                                         host_port)
            self._inventory.set_variable(host_ip, 'ansible_ssh_user', username)
            self._inventory.set_variable(host_ip, 'ansible_ssh_pass', password)

            # set other variables
            for key in host:
                if key not in ["hostname", "port", "username", "password"]:
                    value = host.get(key)
                    my_host.set_variable(key, value)
                    self._inventory.set_variable(host_ip, key, value)
Beispiel #37
0
    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:
            line = utils.before_comment(line).strip()
            if line.startswith("[") and line.endswith("]"):
                active_group_name = line.replace("[", "").replace("]", "")
                if ":vars" in line or ":children" in line:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(
                            name=active_group_name)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    new_group = self.groups[active_group_name] = Group(
                        name=active_group_name)
            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
                # Three 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.count(":") > 1:
                    # Possible an IPv6 address, or maybe a host line with multiple ranges
                    # IPv6 with Port  XXX:XXX::XXX.port
                    # FQDN            foo.example.com
                    if hostname.count(".") == 1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif ("[" in hostname and "]" in hostname and ":" in hostname
                      and (hostname.rindex("]") < hostname.rindex(":"))
                      or ("]" not in hostname and ":" in hostname)):
                    (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k, v) = t.split("=", 1)
                            except ValueError, e:
                                raise errors.AnsibleError(
                                    "Invalid ini entry: %s - %s" % (t, str(e)))
                            host.set_variable(k, self._parse_value(v))
                    self.groups[active_group_name].add_host(host)
Beispiel #38
0
    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.split(" #")[0].replace("[","").replace("]","").strip()
                if line.find(":vars") != -1 or line.find(":children") != -1:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(name=active_group_name)
                        all.add_child_group(new_group)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    all.add_child_group(new_group)
            elif line.startswith("#") or line.startswith(";") or line == '':
                pass
            elif active_group_name:
                tokens = shlex.split(line.split(" #")[0])
                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]

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k,v) = t.split("=")
                            except ValueError, e:
                                raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e)))
                            try:
                                host.set_variable(k,ast.literal_eval(v))
                            except:
                                # most likely a string that literal_eval
                                # doesn't like, so just set it
                                host.set_variable(k,v)
                    self.groups[active_group_name].add_host(host)
Beispiel #39
0
    def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):

        if self.runner.noop_on_check(inject):
            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
        
        # redefine inventory and get group "all"
        inventory = self.runner.inventory
        allgroup = inventory.get_group('all')

        # check if host in cache, add if not
        if new_name in inventory._hosts_cache:
            new_host = inventory._hosts_cache[new_name]
        else:
            new_host = Host(new_name)
            # only groups can be added directly to inventory
            inventory._hosts_cache[new_name] = new_host
            allgroup.add_host(new_host)

        # 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]) 
                
        
        groupnames = args.get('groupname', args.get('groups', args.get('group', ''))) 
        # add it to the group if that was specified
        if groupnames != '':
            for group_name in groupnames.split(","):
                group_name = group_name.strip()
                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)

                # add this host to the group cache
                if inventory._groups_list is not None:
                    if group_name in inventory._groups_list:
                        if new_host.name not in inventory._groups_list[group_name]:
                            inventory._groups_list[group_name].append(new_host.name)

                vv("added host to group via add_host module: %s" % group_name)
            result['new_groups'] = groupnames.split(",")
            
        result['new_host'] = new_name

        # clear pattern caching completely since it's unpredictable what
        # patterns may have referenced the group
        inventory.clear_pattern_cache()
        
        return ReturnData(conn=conn, comm_ok=True, result=result)
Beispiel #40
0
    def run(self,
            conn,
            tmp,
            module_name,
            module_args,
            inject,
            complex_args=None,
            **kwargs):

        if self.runner.noop_on_check(inject):
            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

        # redefine inventory and get group "all"
        inventory = self.runner.inventory
        allgroup = inventory.get_group('all')

        # check if host in cache, add if not
        if new_name in inventory._hosts_cache:
            new_host = inventory._hosts_cache[new_name]
        else:
            new_host = Host(new_name)
            # only groups can be added directly to inventory
            inventory._hosts_cache[new_name] = new_host
            allgroup.add_host(new_host)

        # 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])

        groupnames = args.get('groupname',
                              args.get('groups', args.get('group', '')))
        # add it to the group if that was specified
        if groupnames != '':
            for group_name in groupnames.split(","):
                group_name = group_name.strip()
                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)

                # add this host to the group cache
                if inventory._groups_list is not None:
                    if group_name in inventory._groups_list:
                        if new_host.name not in inventory._groups_list[
                                group_name]:
                            inventory._groups_list[group_name].append(
                                new_host.name)

                vv("added host to group via add_host module: %s" % group_name)
            result['new_groups'] = groupnames.split(",")

        result['new_host'] = new_name

        # clear pattern caching completely since it's unpredictable what
        # patterns may have referenced the group
        inventory.clear_pattern_cache()

        return ReturnData(conn=conn, comm_ok=True, result=result)
Beispiel #41
0
    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:
            line = self._before_comment(line).strip()
            if line.startswith("[") and line.endswith("]"):
                active_group_name = line.replace("[","").replace("]","")
                if ":vars" in line or ":children" in line:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
            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
                # Three 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.count(":") > 1:
                    # Possible an IPv6 address, or maybe a host line with multiple ranges
                    # IPv6 with Port  XXX:XXX::XXX.port
                    # FQDN            foo.example.com
                    if hostname.count(".") == 1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif ("[" in hostname and
                    "]" in hostname and
                    ":" in hostname and
                    (hostname.rindex("]") < hostname.rindex(":")) or
                    ("]" not in hostname and ":" in hostname)):
                        (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k,v) = t.split("=", 1)
                            except ValueError, e:
                                raise AnsibleError("Invalid ini entry in %s: %s - %s" % (self.filename, t, str(e)))
                            if k == 'ansible_ssh_host':
                                host.ipv4_address = self._parse_value(v)
                            else:
                                host.set_variable(k, self._parse_value(v))
                    self.groups[active_group_name].add_host(host)
Beispiel #42
0
    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:
            line = line.split("#")[0].strip()
            if line.startswith("[") and line.endswith("]"):
                active_group_name = line.replace("[", "").replace("]", "")
                if line.find(":vars") != -1 or line.find(":children") != -1:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(
                            name=active_group_name)
                        all.add_child_group(new_group)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    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
                # Three 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.count(":") > 1:
                    # probably an IPv6 addresss, so check for the format
                    # XXX:XXX::XXX.port, otherwise we'll just assume no
                    # port is set
                    if hostname.find(".") != -1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif (hostname.find("[") != -1 and hostname.find("]") != -1
                      and hostname.find(":") != -1 and
                      (hostname.rindex("]") < hostname.rindex(":")) or
                      (hostname.find("]") == -1 and hostname.find(":") != -1)):
                    (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k, v) = t.split("=")
                            except ValueError, e:
                                raise errors.AnsibleError(
                                    "Invalid ini entry: %s - %s" % (t, str(e)))
                            try:
                                host.set_variable(k, ast.literal_eval(v))
                            except:
                                # most likely a string that literal_eval
                                # doesn't like, so just set it
                                host.set_variable(k, v)
                    self.groups[active_group_name].add_host(host)
Beispiel #43
0
    def _parse_base_groups(self):
        # FIXME: refactor,貌似在后面的版本中该函数会被重构
        # 这部分解析ini配置文件的代码却是写的很臃肿,急需要重构
        # 基础group解析,除了已经定义组名的group以外,还有ungrouped表示未分组的组名,all表示所有组的总和

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

        # 喜欢这种dict创建字典的方式,比用{}的方式好看多了
        self.groups = dict(all=all, ungrouped=ungrouped)
        active_group_name = 'ungrouped'

        # 遍历self.lines,self.lines是文件内容,
        # ini文件的解析大多可以用python内置的conparser类,不过ansible的inventory所支持的语法比较复杂,这里作者自己处理了
        for lineno in range(len(self.lines)):
            line = utils.before_comment(self.lines[lineno]).strip()
            # 如果某行以'['开头,以']'结尾则表名是一个section
            if line.startswith("[") and line.endswith("]"):
                # 将中括号replace掉获得组名
                active_group_name = line.replace("[","").replace("]","")
                # 如果组名中有:vars 或:children,则进行二次处理,比如[southeast:vars],在通过冒号分割得到southeast
                if ":vars" in line or ":children" in line:
                    active_group_name = active_group_name.rsplit(":", 1)[0] # rsplit(":", 1)表示右边开始以冒号为分隔符分割一次
                    # 如果组名未加到self.groups里面,则创建一个新的Group类,并添加到self.groups里面
                    # 如果组名已经存在与self.groups里面,跳过...
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    # 在这种情况下将active_group_name设置为None,用来表示这不是一个包含真正host的group
                    active_group_name = None
                # 这部分是组名中没有冒号的处理方式,和上面一样。
                elif active_group_name not in self.groups:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
            elif line.startswith(";") or line == '':
                # 如果改行以分号开始或为空行则跳过。
                pass
            elif active_group_name:
                # 这种情况表示当前行为非中括号打头的行,既包含真实host主机数据的行
                # 在section中包含:vars/:children的段并不包含真实主机
                # shlex模块实现了一个类来解析简单的类shell语法,可以用来编写领域特定的语言,或者解析加引号的字符串。
                tokens = shlex.split(line)
                if len(tokens) == 0:
                    continue
                hostname = tokens[0] # 获取主机名
                port = C.DEFAULT_REMOTE_PORT # 使用默认的SSH端口号
                # Three cases to check:
                # 0. A hostname that contains a range pesudo-code and a port,like badwol[a:f].example.com:5309
                # 1. A hostname that contains just a port ,like badwolf.example.com:5309
                # 对hostname需要进行以下的检测
                if hostname.count(":") > 1:
                    # IPV6格式的地址,端口号和hostname之间用"."表示。
                    # Possible an IPv6 address, or maybe a host line with multiple ranges
                    # IPv6 with Port  XXX:XXX::XXX.port
                    # FQDN            foo.example.com
                    if hostname.count(".") == 1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif ("[" in hostname and
                    "]" in hostname and
                    ":" in hostname and
                    (hostname.rindex("]") < hostname.rindex(":")) or
                    ("]" not in hostname and ":" in hostname)):
                        # 如果冒号在中括号外面,表示这个冒号后面是端口号,因此通过通过rsplit按照冒号分割一次获取端口号和hostname
                        (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                # 检测hostname是否是表示一个范围的hosts,如果是则将其扩展成一组host列表,否则加入空列表
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                # 遍历hostnames列表
                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        # 如果host不在self.hosts列表中,则创建一个Host基类
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    # len(tokens) > 1表示该行拥有变量,如:jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'): # 如果是注释则退出,在ini文件中仍然可以使用#作为注释标识。
                                break
                            try:
                                (k,v) = t.split("=", 1) # kv变量解析
                            except ValueError, e:
                                raise errors.AnsibleError("%s:%s: Invalid ini entry: %s - %s" % (self.filename, lineno + 1, t, str(e)))
                            host.set_variable(k, self._parse_value(v)) # 将该行解析的变量设置到该host下
                    self.groups[active_group_name].add_host(host) # 将该host加入到对应的group中。
Beispiel #44
0
    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:
            line = utils.before_comment(line).strip()
            if line.startswith("[") and line.endswith("]"):
                active_group_name = line.replace("[","").replace("]","")
                if ":vars" in line or ":children" in line:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(name=active_group_name)
                        all.add_child_group(new_group)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    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
                # Three 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.count(":") > 1:
                    # Possible an IPv6 address, or maybe a host line with multiple ranges
                    # IPv6 with Port  XXX:XXX::XXX.port
                    # FQDN            foo.example.com
                    if hostname.count(".") == 1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif ("[" in hostname and
                    "]" in hostname and
                    ":" in hostname and
                    (hostname.rindex("]") < hostname.rindex(":")) or
                    ("]" not in hostname and ":" in hostname)):
                        (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k,v) = t.split("=", 1)
                            except ValueError, e:
                                raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e)))

                            # If there is a hash in the value don't pass it through to ast at ast will split at the hash.
                            if "#" in v:
                                host.set_variable(k, v)
                            else:
                                try:
                                    host.set_variable(k,ast.literal_eval(v))
                                # Using explicit exceptions.
                                # Likely a string that literal_eval does not like. We wil then just set it.
                                except ValueError:
                                    # For some reason this was thought to be malformed.
                                    host.set_variable(k, v)
                                except SyntaxError:
                                    # Is this a hash with an equals at the end?
                                    host.set_variable(k, v)

                    self.groups[active_group_name].add_host(host)
Beispiel #45
0
    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:
            line = utils.before_comment(line).strip()
            if line.startswith("[") and line.endswith("]"):
                active_group_name = line.replace("[","").replace("]","")
                if line.find(":vars") != -1 or line.find(":children") != -1:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(name=active_group_name)
                        all.add_child_group(new_group)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    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
                # Three 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.count(":") > 1:
                    # Possible an IPv6 address, or maybe a host line with multiple ranges
                    # IPv6 with Port  XXX:XXX::XXX.port
                    # FQDN            foo.example.com
                    if hostname.count(".") == 1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif (hostname.find("[") != -1 and
                    hostname.find("]") != -1 and
                    hostname.find(":") != -1 and
                    (hostname.rindex("]") < hostname.rindex(":")) or
                    (hostname.find("]") == -1 and hostname.find(":") != -1)):
                        (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k,v) = t.split("=", 1)
                            except ValueError, e:
                                raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e)))

                            # If there is a hash in the value don't pass it through to ast at ast will split at the hash.
                            if "#" in v:
                                host.set_variable(k, v)
                            else:
                                try:
                                    host.set_variable(k,ast.literal_eval(v))
                                # Using explicit exceptions.
                                # Likely a string that literal_eval does not like. We wil then just set it.
                                except ValueError:
                                    # For some reason this was thought to be malformed.
                                    host.set_variable(k, v)
                                except SyntaxError:
                                    # Is this a hash with an equals at the end?
                                    host.set_variable(k, v)

                    self.groups[active_group_name].add_host(host)
Beispiel #46
0
# -*-coding:utf-8 -*-