Exemple #1
0
    def _get_hostname(self, instance, hostnames):
        '''
            :param instance: an instance dict returned by boto3 ec2 describe_instances()
            :param hostnames: a list of hostname destination variables in order of preference
            :return the preferred identifer for the host
        '''
        if not hostnames:
            hostnames = ['dns-name', 'private-dns-name']

        hostname = None
        for preference in hostnames:
            if 'tag' in preference:
                if not preference.startswith('tag:'):
                    raise AnsibleError(
                        "To name a host by tags name_value, use 'tag:name=value'."
                    )
                hostname = self._get_tag_hostname(preference, instance)
            else:
                hostname = self._get_boto_attr_chain(preference, instance)
            if hostname:
                break
        if hostname:
            if ':' in to_text(hostname):
                return to_safe_group_name(to_text(hostname))
            else:
                return to_text(hostname)
Exemple #2
0
    def fill_auto_groups(self, table_client, table, host_source, name_source,
                         columns, group_by):
        records = table_client.list_records(table,
                                            query=self.query(
                                                group_by, host_source,
                                                name_source, columns))

        for record in records:
            host = self.add_host(record, table, host_source, name_source)
            if host:
                for category in group_by.keys():
                    group_name = to_safe_group_name(record[category])
                    # Only truthy names are allowed
                    # Column values can be set to "None", and we don't want those
                    if group_name:
                        self.inventory.add_group(group_name)
                        self.inventory.add_host(host, group=group_name)
                    else:
                        msg = (
                            "Encountered invalid group name '{1}' for host {0}. "
                            "Group will not be created.").format(
                                group_name, host)
                        self.display.warning(msg)

                self.set_hostvars(host, record, columns)
Exemple #3
0
 def _add_safe_group_name(self, group, child=None):
     group_name = self.inventory.add_group(
         to_safe_group_name('%s%s' % (self.get_option('group_prefix'),
                                      group.lower().replace(" ", ""))))
     if child is not None:
         self.inventory.add_child(group_name, child)
     return group_name
Exemple #4
0
    def parse(self, inventory, loader, path, cache=False):
        super(InventoryModule, self).parse(inventory, loader, path)

        self._read_config_data(path)

        strict = self.get_option('strict')

        try:
            objects = []

            json_response = self.get_d42_inventory()

            if 'Devices' in json_response:
                objects = json_response['Devices']

            for object_ in objects:
                host_name = self.inventory.add_host(to_safe_group_name(object_['name']))
                for k in object_.keys():
                    self.inventory.set_variable(host_name, 'd42_' + k, object_[k])

                if object_['ip_addresses']:
                    self.inventory.set_variable(host_name, 'ansible_host', object_['ip_addresses'][0]['ip'])

                self._set_composite_vars(
                    self.get_option('compose'),
                    self.inventory.get_host(host_name).get_vars(), host_name,
                    strict)

                self._add_host_to_composed_groups(self.get_option('groups'), dict(), host_name, strict)
                self._add_host_to_keyed_groups(self.get_option('keyed_groups'), dict(), host_name, strict)
        except Exception as e:
            print(e)
Exemple #5
0
    def _get_host_info_dict_from_device(self, device):
        device_vars = {}
        device.ip_addresses
        for key in vars(device):
            value = getattr(device, key)
            key = to_safe_group_name(key)

            # Handle complex types
            if key == 'state':
                device_vars[key] = device.state or ''
            elif key == 'hostname':
                device_vars[key] = value
            elif isinstance(value, (int, bool)):
                device_vars[key] = value
            elif isinstance(value, six.string_types):
                device_vars[key] = value.strip()
            elif value is None:
                device_vars[key] = ''
            elif key == 'facility':
                device_vars[key] = value['code']
            elif key == 'operating_system':
                device_vars[key] = value.slug
            elif key == 'plan':
                device_vars[key] = value['slug']
            elif key == 'project':
                device_vars[key] = value['href'].strip('/projects/')
            elif key == 'ip_addresses':
                device_vars[key] = []

                for addr in value:
                    device_vars[key].append(
                        {
                            'address': addr['address'],
                            'address_family': addr['address_family'],
                            'public': addr['public'],
                            'cidr': addr['cidr'],
                            'enabled': addr['enabled'],
                            'gateway': addr['gateway'],
                            'global_ip': addr['global_ip'],
                            'manageable': addr['manageable'],
                            'management': addr['management'],
                            'netmask': addr['netmask'],
                            'network': addr['network'],
                            'tags': addr['tags'],
                        }
                    )
            elif key == 'tags':
                device_vars[key] = value
            else:
                pass

        return device_vars
Exemple #6
0
    def _populate(self):

        for host in self._get_hosts():

            if host.get('name'):
                self.inventory.add_host(host['name'])

                # create directly mapped groups
                group_name = host.get('hostgroup_title',
                                      host.get('hostgroup_name'))
                if group_name:
                    group_name = to_safe_group_name(
                        '%s%s' % (self.get_option('group_prefix'),
                                  group_name.lower().replace(" ", "")))
                    group_name = self.inventory.add_group(group_name)
                    self.inventory.add_child(group_name, host['name'])

                # set host vars from host info
                try:
                    for k, v in host.items():
                        if k not in ('name', 'hostgroup_title',
                                     'hostgroup_name'):
                            try:
                                self.inventory.set_variable(
                                    host['name'],
                                    self.get_option('vars_prefix') + k, v)
                            except ValueError as e:
                                self.display.warning(
                                    "Could not set host info hostvar for %s, skipping %s: %s"
                                    % (host, k, to_native(e)))
                except ValueError as e:
                    self.display.warning(
                        "Could not get host info for %s, skipping: %s" %
                        (host['name'], to_native(e)))

                # set host vars from params
                if self.get_option('want_params'):
                    for p in self._get_all_params_by_id(host['id']):
                        try:
                            self.inventory.set_variable(
                                host['name'], p['name'], p['value'])
                        except ValueError as e:
                            self.display.warning(
                                "Could not set hostvar %s to '%s' for the '%s' host, skipping:  %s"
                                % (p['name'], to_native(
                                    p['value']), host, to_native(e)))

                # set host vars from facts
                if self.get_option('want_facts'):
                    self.inventory.set_variable(host['name'], 'ansible_facts',
                                                self._get_facts(host))
Exemple #7
0
    def parse(self,
              inventory,
              loader,
              path,
              cache=True):  # Plugin interface (2)
        super(InventoryModule, self).parse(inventory, loader, path)

        self._read_config_data(path)
        self.cache_key = self.get_cache_key(path)

        self.use_cache = self.get_option('cache') and cache
        self.update_cache = self.get_option('cache') and not cache

        self.login_zabbix()
        zapi_query = self.get_option('host_zapi_query')
        content = self._zapi.host.get(zapi_query)

        strict = self.get_option('strict')

        for record in content:
            # add host to inventory
            host_name = self.inventory.add_host(record['host'])
            # set variables for host
            for k in record.keys():
                self.inventory.set_variable(host_name, 'zbx_%s' % k, record[k])

            # added for compose vars and keyed groups
            self._set_composite_vars(
                self.get_option('compose'),
                self.inventory.get_host(host_name).get_vars(), host_name,
                strict)

            self._add_host_to_composed_groups(self.get_option('groups'),
                                              dict(), host_name, strict)
            self._add_host_to_keyed_groups(self.get_option('keyed_groups'),
                                           dict(), host_name, strict)

        # organize inventory by zabbix groups
        if self.get_option('add_zabbix_groups'):
            content = self._zapi.host.get({'selectGroups': ['name']})
            for record in content:
                host_name = record['host']
                if len(record['groups']) >= 1:
                    for group in record['groups']:
                        group_name = to_safe_group_name(group['name'])
                        self.inventory.add_group(group_name)
                        self.inventory.add_child(group_name, host_name)
Exemple #8
0
    def _populate(self):

        for host in self._get_hosts():

            if host.get('name'):
                host_name = self.inventory.add_host(host['name'])

                # create directly mapped groups
                group_name = host.get('hostgroup_title',
                                      host.get('hostgroup_name'))
                if group_name:
                    group_name = to_safe_group_name(
                        '%s%s' % (self.get_option('group_prefix'),
                                  group_name.lower().replace(" ", "")))
                    group_name = self.inventory.add_group(group_name)
                    self.inventory.add_child(group_name, host_name)

                # set host vars from host info
                try:
                    for k, v in host.items():
                        if k not in ('name', 'hostgroup_title',
                                     'hostgroup_name'):
                            try:
                                self.inventory.set_variable(
                                    host_name,
                                    self.get_option('vars_prefix') + k, v)
                            except ValueError as e:
                                self.display.warning(
                                    "Could not set host info hostvar for %s, skipping %s: %s"
                                    % (host, k, to_text(e)))
                except ValueError as e:
                    self.display.warning(
                        "Could not get host info for %s, skipping: %s" %
                        (host_name, to_text(e)))

                # set host vars from params
                if self.get_option('want_params'):
                    for p in self._get_all_params_by_id(host['id']):
                        try:
                            self.inventory.set_variable(
                                host_name, p['name'], p['value'])
                        except ValueError as e:
                            self.display.warning(
                                "Could not set hostvar %s to '%s' for the '%s' host, skipping:  %s"
                                % (p['name'], to_native(
                                    p['value']), host, to_native(e)))

                # set host vars from facts
                if self.get_option('want_facts'):
                    self.inventory.set_variable(host_name, 'foreman_facts',
                                                self._get_facts(host))

                # create group for host collections
                if self.get_option('want_hostcollections'):
                    host_data = self._get_host_data_by_id(host['id'])
                    hostcollections = host_data.get('host_collections')
                    if hostcollections:
                        # Create Ansible groups for host collections
                        for hostcollection in hostcollections:
                            try:
                                hostcollection_group = to_safe_group_name(
                                    '%shostcollection_%s' %
                                    (self.get_option('group_prefix'),
                                     hostcollection['name'].lower().replace(
                                         " ", "")))
                                hostcollection_group = self.inventory.add_group(
                                    hostcollection_group)
                                self.inventory.add_child(
                                    hostcollection_group, host_name)
                            except ValueError as e:
                                self.display.warning(
                                    "Could not create groups for host collections for %s, skipping: %s"
                                    % (host_name, to_text(e)))

                # put ansible_ssh_host as hostvar
                if self.get_option('want_ansible_ssh_host'):
                    if host.get('ip'):
                        try:
                            self.inventory.set_variable(
                                host_name, 'ansible_ssh_host', host['ip'])
                        except ValueError as e:
                            self.display.warning(
                                "Could not set hostvar ansible_ssh_host to '%s' for the '%s' host, skipping: %s"
                                % (host['ip'], host_name, to_text(e)))

                strict = self.get_option('strict')

                hostvars = self.inventory.get_host(host_name).get_vars()
                self._set_composite_vars(self.get_option('compose'), hostvars,
                                         host_name, strict)
                self._add_host_to_composed_groups(self.get_option('groups'),
                                                  hostvars, host_name, strict)
                self._add_host_to_keyed_groups(self.get_option('keyed_groups'),
                                               hostvars, host_name, strict)
Exemple #9
0
    def _populate_host_api(self):
        hostnames = self.get_option('hostnames')
        strict = self.get_option('strict')
        for host in self._get_hosts():
            if not (host):
                continue

            composed_host_name = self._get_hostname(host,
                                                    hostnames,
                                                    strict=strict)

            if (composed_host_name in self._cache.keys()):
                continue

            host_name = self.inventory.add_host(composed_host_name)

            # create directly mapped groups
            group_name = host.get('hostgroup_title',
                                  host.get('hostgroup_name'))
            if group_name:
                parent_name = None
                group_label_parts = []
                for part in group_name.split('/'):
                    group_label_parts.append(part.lower().replace(" ", ""))
                    gname = to_safe_group_name(
                        '%s%s' % (self.get_option('group_prefix'),
                                  '/'.join(group_label_parts)))
                    result_gname = self.inventory.add_group(gname)
                    if parent_name:
                        self.inventory.add_child(parent_name, result_gname)
                    parent_name = result_gname
                self.inventory.add_child(result_gname, host_name)

            if self.get_option('legacy_hostvars'):
                hostvars = self._get_hostvars(host)
                self.inventory.set_variable(host_name, 'foreman', hostvars)
            else:
                omitted_vars = ('name', 'hostgroup_title', 'hostgroup_name')
                hostvars = self._get_hostvars(host,
                                              self.get_option('vars_prefix'),
                                              omitted_vars)

                for k, v in hostvars.items():
                    try:
                        self.inventory.set_variable(host_name, k, v)
                    except ValueError as e:
                        self.display.warning(
                            "Could not set host info hostvar for %s, skipping %s: %s"
                            % (host, k, to_text(e)))

            # set host vars from params
            if self.get_option('want_params'):
                params = self._get_all_params_by_id(host['id'])
                filtered_params = {}
                for p in params:
                    if 'name' in p and 'value' in p:
                        filtered_params[p['name']] = p['value']

                if self.get_option('legacy_hostvars'):
                    self.inventory.set_variable(host_name, 'foreman_params',
                                                filtered_params)
                else:
                    for k, v in filtered_params.items():
                        try:
                            self.inventory.set_variable(host_name, k, v)
                        except ValueError as e:
                            self.display.warning(
                                "Could not set hostvar %s to '%s' for the '%s' host, skipping:  %s"
                                % (k, to_native(v), host, to_native(e)))

            # set host vars from facts
            if self.get_option('want_facts'):
                self.inventory.set_variable(host_name, 'foreman_facts',
                                            self._get_facts(host))

            # create group for host collections
            if self.get_option('want_hostcollections'):
                host_data = self._get_host_data_by_id(host['id'])
                hostcollections = host_data.get('host_collections')
                if hostcollections:
                    # Create Ansible groups for host collections
                    for hostcollection in hostcollections:
                        try:
                            hostcollection_group = to_safe_group_name(
                                '%shostcollection_%s' %
                                (self.get_option('group_prefix'),
                                 hostcollection['name'].lower().replace(
                                     " ", "")))
                            hostcollection_group = self.inventory.add_group(
                                hostcollection_group)
                            self.inventory.add_child(hostcollection_group,
                                                     host_name)
                        except ValueError as e:
                            self.display.warning(
                                "Could not create groups for host collections for %s, skipping: %s"
                                % (host_name, to_text(e)))

            hostvars = self.inventory.get_host(host_name).get_vars()
            self._set_composite_vars(self.get_option('compose'), hostvars,
                                     host_name, strict)
            self._add_host_to_composed_groups(self.get_option('groups'),
                                              hostvars, host_name, strict)
            self._add_host_to_keyed_groups(self.get_option('keyed_groups'),
                                           hostvars, host_name, strict)
Exemple #10
0
    def _populate_report_api(self):
        self.groups = dict()
        self.hosts = dict()
        try:
            inventory_report_response = self._post_request()
        except Exception as exc:
            self.display.warning(
                "Failed to use Reports API, falling back to Hosts API: {0}".
                format(exc))
            self._populate_host_api()
            return
        self.group_prefix = self.get_option('group_prefix')

        hostnames = self.get_option('hostnames')
        strict = self.get_option('strict')

        host_data = json.loads(inventory_report_response)
        for host in host_data:
            if not (host):
                continue

            composed_host_name = self._get_hostname(host,
                                                    hostnames,
                                                    strict=strict)

            if (composed_host_name in self._cache.keys()):
                continue

            host_name = self.inventory.add_host(composed_host_name)

            group_name = host.get('hostgroup_title',
                                  host.get('hostgroup_name'))
            if group_name:
                group_name = to_safe_group_name(
                    '%s%s' % (self.get_option('group_prefix'),
                              group_name.lower().replace(" ", "")))
                group_name = self.inventory.add_group(group_name)
                self.inventory.add_child(group_name, host_name)

            host_params = host.pop('host_parameters', {})
            fact_list = host.pop('facts', {})

            if self.get_option('legacy_hostvars'):
                hostvars = self._get_hostvars(host)
                self.inventory.set_variable(host_name, 'foreman', hostvars)
            else:
                omitted_vars = ('name', 'hostgroup_title', 'hostgroup_name')
                hostvars = self._get_hostvars(host,
                                              self.get_option('vars_prefix'),
                                              omitted_vars)

                for k, v in hostvars.items():
                    try:
                        self.inventory.set_variable(host_name, k, v)
                    except ValueError as e:
                        self.display.warning(
                            "Could not set host info hostvar for %s, skipping %s: %s"
                            % (host, k, to_text(e)))

            content_facet_attributes = host.get('content_attributes', {}) or {}
            if self.get_option('want_facts'):
                self.inventory.set_variable(host_name, 'foreman_facts',
                                            fact_list)

            # Create ansible groups for hostgroup
            group = 'host_group'
            group_name = host.get(group)
            if group_name:
                parent_name = None
                group_label_parts = []
                for part in group_name.split('/'):
                    group_label_parts.append(part.lower().replace(" ", ""))
                    gname = to_safe_group_name(
                        '%s%s' % (self.get_option('group_prefix'),
                                  '/'.join(group_label_parts)))
                    result_gname = self.inventory.add_group(gname)
                    if parent_name:
                        self.inventory.add_child(parent_name, result_gname)
                    parent_name = result_gname
                self.inventory.add_child(result_gname, host_name)

            # Create ansible groups for environment, location and organization
            for group in ['environment', 'location', 'organization']:
                val = host.get('%s' % group)
                if val:
                    safe_key = to_safe_group_name('%s%s_%s' % (to_text(
                        self.group_prefix), group, to_text(val).lower()))
                    env_lo_org = self.inventory.add_group(safe_key)
                    self.inventory.add_child(env_lo_org, host_name)

            for group in ['lifecycle_environment', 'content_view']:
                val = content_facet_attributes.get('%s_name' % group)
                if val:
                    safe_key = to_safe_group_name('%s%s_%s' % (to_text(
                        self.group_prefix), group, to_text(val).lower()))
                    le_cv_group = self.inventory.add_group(safe_key)
                    self.inventory.add_child(le_cv_group, host_name)
            params = host_params

            if self.want_hostcollections:
                hostcollections = host.get('host_collections')

                if hostcollections:
                    # Create Ansible groups for host collections
                    for hostcollection in hostcollections:
                        try:
                            host_collection_group_name = to_safe_group_name(
                                '%shostcollection_%s' %
                                (to_text(self.group_prefix),
                                 to_text(hostcollection).lower()))
                            hostcollection_group = self.inventory.add_group(
                                host_collection_group_name)
                            self.inventory.add_child(hostcollection_group,
                                                     host_name)
                        except ValueError as e:
                            self.display.warning(
                                "Could not create groups for host collections for %s, skipping: %s"
                                % (host_name, to_text(e)))

            # set host vars from params
            if self.get_option('want_params'):
                if self.get_option('legacy_hostvars'):
                    self.inventory.set_variable(host_name, 'foreman_params',
                                                params)
                else:
                    for k, v in params.items():
                        try:
                            self.inventory.set_variable(host_name, k, v)
                        except ValueError as e:
                            self.display.warning(
                                "Could not set hostvar %s to '%s' for the '%s' host, skipping:  %s"
                                % (k, to_native(v), host, to_native(e)))
            hostvars = self.inventory.get_host(host_name).get_vars()
            self._set_composite_vars(self.get_option('compose'), hostvars,
                                     host_name, strict)
            self._add_host_to_composed_groups(self.get_option('groups'),
                                              hostvars, host_name, strict)
            self._add_host_to_keyed_groups(self.get_option('keyed_groups'),
                                           hostvars, host_name, strict)
Exemple #11
0
    def parse(self,
              inventory,
              loader,
              path,
              cache=True):  # Plugin interface (2)
        super(InventoryModule, self).parse(inventory, loader, path)

        if not HAS_REQUESTS:
            raise AnsibleParserError(
                'Please install "requests" Python module as this is required'
                ' for ServiceNow dynamic inventory plugin.')

        self._read_config_data(path)
        self.cache_key = self.get_cache_key(path)

        self.use_cache = self.get_option('cache') and cache
        self.update_cache = self.get_option('cache') and not cache

        selection = self.get_option('selection_order')
        fields = self.get_option('fields')
        table = self.get_option('table')
        filter_results = self.get_option('filter_results')

        options = "?sysparm_exclude_reference_link=true&sysparm_display_value=true"

        enhanced = self.get_option('enhanced')
        enhanced_groups = False

        if enhanced:
            path = '/api/snc/ansible_inventory' + options + \
                "&sysparm_fields=" + ','.join(fields) + \
                "&sysparm_query=" + filter_results + \
                "&table=" + table
            enhanced_groups = self.get_option('enhanced_groups')
        else:
            path = '/api/now/table/' + table + options + \
                "&sysparm_fields=" + ','.join(fields) + \
                "&sysparm_query=" + filter_results

        content = self.invoke('GET', path, None)
        strict = self.get_option('strict')

        for record in content['result']:

            target = None

            # select name for host
            for k in selection:
                if k in record:
                    if record[k] != '':
                        target = record[k]
                if target is not None:
                    break

            if target is None:
                continue

            # add host to inventory
            host_name = self.inventory.add_host(target)

            # set variables for host
            for k in record.keys():
                k2 = k.replace('.', '_')
                self.inventory.set_variable(host_name, 'sn_%s' % k2, record[k])

            # add relationship based groups
            if enhanced and enhanced_groups:
                for item in record['child_relationships']:
                    ci = to_safe_group_name(item['ci'])
                    ci_rel_type = to_safe_group_name(
                        item['ci_rel_type'].split('__')[0])
                    ci_type = to_safe_group_name(item['ci_type'])
                    if ci != '' and ci_rel_type != '' and ci_type != '':
                        child_group = "%s_%s" % (ci, ci_rel_type)
                        self.inventory.add_group(child_group)
                        self.inventory.add_child(child_group, host_name)

                for item in record['parent_relationships']:
                    ci = to_safe_group_name(item['ci'])
                    ci_rel_type = to_safe_group_name(
                        item['ci_rel_type'].split('__')[-1])
                    ci_type = to_safe_group_name(item['ci_type'])

                    if ci != '' and ci_rel_type != '' and ci_type != '':
                        child_group = "%s_%s" % (ci, ci_rel_type)
                        self.inventory.add_group(child_group)
                        self.inventory.add_child(child_group, host_name)

            self._set_composite_vars(
                self.get_option('compose'),
                self.inventory.get_host(host_name).get_vars(), host_name,
                strict)

            self._add_host_to_composed_groups(self.get_option('groups'),
                                              dict(), host_name, strict)
            self._add_host_to_keyed_groups(self.get_option('keyed_groups'),
                                           dict(), host_name, strict)
Exemple #12
0
    def parse(self, inventory, loader, path, cache=True):

        super(InventoryModule, self).parse(inventory, loader, path)

        # read config from file, this sets 'options'
        self._read_config_data(path)

        # get connection host
        self.cobbler_url = self.get_option('url')
        self.cache_key = self.get_cache_key(path)
        self.use_cache = cache and self.get_option('cache')

        self.exclude_profiles = self.get_option('exclude_profiles')
        self.group_by = self.get_option('group_by')

        for profile in self._get_profiles():
            if profile['parent']:
                self.display.vvvv('Processing profile %s with parent %s\n' % (profile['name'], profile['parent']))
                if profile['parent'] not in self.exclude_profiles:
                    parent_group_name = self._add_safe_group_name(profile['parent'])
                    self.display.vvvv('Added profile parent group %s\n' % parent_group_name)
                    if profile['name'] not in self.exclude_profiles:
                        group_name = self._add_safe_group_name(profile['name'])
                        self.display.vvvv('Added profile group %s\n' % group_name)
                        self.inventory.add_child(parent_group_name, group_name)
            else:
                self.display.vvvv('Processing profile %s without parent\n' % profile['name'])
                # Create a heirarchy of profile names
                profile_elements = profile['name'].split('-')
                i = 0
                while i < len(profile_elements) - 1:
                    profile_group = '-'.join(profile_elements[0:i + 1])
                    profile_group_child = '-'.join(profile_elements[0:i + 2])
                    if profile_group in self.exclude_profiles:
                        self.display.vvvv('Excluding profile %s\n' % profile_group)
                        break
                    group_name = self._add_safe_group_name(profile_group)
                    self.display.vvvv('Added profile group %s\n' % group_name)
                    child_group_name = self._add_safe_group_name(profile_group_child)
                    self.display.vvvv('Added profile child group %s to %s\n' % (child_group_name, group_name))
                    self.inventory.add_child(group_name, child_group_name)
                    i = i + 1

        # Add default group for this inventory if specified
        self.group = to_safe_group_name(self.get_option('group'))
        if self.group is not None and self.group != '':
            self.inventory.add_group(self.group)
            self.display.vvvv('Added site group %s\n' % self.group)

        for host in self._get_systems():
            # Get the FQDN for the host and add it to the right groups
            hostname = host['hostname']  # None
            interfaces = host['interfaces']

            if host['profile'] in self.exclude_profiles:
                self.display.vvvv('Excluding host %s in profile %s\n' % (host['name'], host['profile']))
                continue

            # hostname is often empty for non-static IP hosts
            if hostname == '':
                for (iname, ivalue) in iteritems(interfaces):
                    if ivalue['management'] or not ivalue['static']:
                        this_dns_name = ivalue.get('dns_name', None)
                        if this_dns_name is not None and this_dns_name != "":
                            hostname = this_dns_name
                            self.display.vvvv('Set hostname to %s from %s\n' % (hostname, iname))

            if hostname == '':
                self.display.vvvv('Cannot determine hostname for host %s, skipping\n' % host['name'])
                continue

            self.inventory.add_host(hostname)
            self.display.vvvv('Added host %s hostname %s\n' % (host['name'], hostname))

            # Add host to profile group
            group_name = self._add_safe_group_name(host['profile'], child=hostname)
            self.display.vvvv('Added host %s to profile group %s\n' % (hostname, group_name))

            # Add host to groups specified by group_by fields
            for group_by in self.group_by:
                if host[group_by] == '<<inherit>>':
                    groups = []
                else:
                    groups = [host[group_by]] if isinstance(host[group_by], str) else host[group_by]
                for group in groups:
                    group_name = self._add_safe_group_name(group, child=hostname)
                    self.display.vvvv('Added host %s to group_by %s group %s\n' % (hostname, group_by, group_name))

            # Add to group for this inventory
            if self.group is not None:
                self.inventory.add_child(self.group, hostname)

            # Add host variables
            if self.get_option('want_facts'):
                try:
                    self.inventory.set_variable(hostname, 'cobbler', host)
                except ValueError as e:
                    self.display.warning("Could not set host info for %s: %s" % (hostname, to_text(e)))
 def fill_enhanced_auto_groups(self, record, host):
     for rel_group in record["relationship_groups"]:
         rel_group = to_safe_group_name(rel_group)
         self.inventory.add_group(rel_group)
         self.inventory.add_child(rel_group, host)