def add_instance()

in plugins/inventory/script/alicloud.py [0:0]


    def add_instance(self, instance, region):
        ''' Adds an instance to the inventory and index, as long as it is addressable '''

        if str.lower(instance.status) not in self.ecs_instance_states:
            return

        # Select the best destination address
        if self.destination_variable:
            if self.destination_variable == 'inner_ip_address':
                self.destination_variable = 'private_ip_address'
            elif self.destination_variable == 'eip_address':
                self.destination_variable = 'public_ip_address'

            dest = getattr(instance, self.destination_variable, None)

        if not dest:
            # Skip instances we cannot address
            return

        # Set the inventory name
        hostname = None
        if self.hostname_variable:
            if self.hostname_variable.startswith('tag_'):
                hostname = instance.tags.get(self.hostname_variable[4:], None)
            else:
                hostname = getattr(instance, self.hostname_variable)

        # If we can't get a nice hostname, use the destination address
        if not hostname:
            hostname = dest
        else:
            hostname = self.to_safe(hostname).lower()

        # if we only want to include hosts that match a pattern, skip those that don't
        if self.pattern_include and not self.pattern_include.match(hostname):
            return

        # if we need to exclude hosts that match a pattern, skip those
        if self.pattern_exclude and self.pattern_exclude.match(hostname):
            return

        # # Add to index
        self.index[hostname] = [region, instance.id, instance.name]

        # Inventory: Group by instance ID (always a group of 1)
        if self.group_by_instance_id:
            self.push(self.inventory, instance.id, hostname)
            if self.nested_groups:
                self.push_group(self.inventory, 'instances', instance.id)

        # Inventory: Group by region
        if self.group_by_region:
            self.push(self.inventory, region, hostname)
            if self.nested_groups:
                self.push_group(self.inventory, 'regions', region)

        # Inventory: Group by availability zone
        if self.group_by_availability_zone:
            self.push(self.inventory, instance.zone_id, hostname)
            if self.nested_groups:
                if self.group_by_region:
                    self.push_group(self.inventory, region, instance.zone_id)
                self.push_group(self.inventory, 'zones', instance.zone_id)

        # Inventory: Group by Alicloud Machine Image ID
        if self.group_by_image_id:
            self.push(self.inventory, instance.image_id, hostname)
            if self.nested_groups:
                self.push_group(self.inventory, 'images', instance.image_id)

        # Inventory: Group by instance type
        if self.group_by_instance_type:
            key = self.to_safe('type_' + instance.instance_type)
            self.push(self.inventory, key, hostname)
            if self.nested_groups:
                self.push_group(self.inventory, 'types', key)

        # Inventory: Group by VPC
        if self.group_by_vpc_id and instance.vpc_id:
            key = self.to_safe('vpc_id_' + instance.vpc_id)
            self.push(self.inventory, key, hostname)
            if self.nested_groups:
                self.push_group(self.inventory, 'vpcs', key)

        # Inventory: Group by vswitch
        if self.group_by_vswitch_id and instance.vswitch_id:
            key = self.to_safe('subnet_' + instance.vswitch_id)
            self.push(self.inventory, key, hostname)
            if self.nested_groups:
                self.push_group(self.inventory, 'subnets', key)

        # Inventory: Group by security group
        if self.group_by_security_group:
            for group in instance.security_group_ids['security_group_id']:
                key = self.to_safe("security_group_" + group)
                self.push(self.inventory, key, hostname)
                if self.nested_groups:
                    self.push_group(self.inventory, 'security_groups', key)

        # Inventory: Group by tag keys
        if self.group_by_tag_keys:
            for k, v in list(instance.tags.items()):
                if self.expand_csv_tags and v and ',' in v:
                    values = [x.strip() for x in v.split(',')]
                else:
                    values = [v]

                for v in values:
                    key = self.to_safe("tag_" + k)
                    if v:
                        key = self.to_safe("tag_" + k + "=" + v)

                    self.push(self.inventory, key, hostname)
                    if self.nested_groups:
                        self.push_group(self.inventory, 'tags', self.to_safe("tag_" + k))
                        if v:
                            self.push_group(self.inventory, self.to_safe("tag_" + k), key)

        # Global Tag: instances without tags
        if self.group_by_tag_none and len(instance.tags) == 0:
            self.push(self.inventory, 'tag_none', hostname)
            if self.nested_groups:
                self.push_group(self.inventory, 'tags', 'tag_none')

        self.push(self.inventory, 'alicloud', hostname)

        self.inventory["_meta"]["hostvars"][hostname] = instance.read()
        self.inventory["_meta"]["hostvars"][hostname]['ansible_ssh_host'] = dest