Sha256: 637a00f528c4ab914cbfc53743de5c9b35fff074bd1145ba2a4f63e8b8f26a19

Contents?: true

Size: 1.08 KB

Versions: 56

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'resolv'
module ForemanAnsible
  # Ensures Ansible reports from hosts where the IP was used, are assigned
  # to the right hostname in Foreman
  module AnsibleReportImporter
    extend ActiveSupport::Concern
    included do
      def host
        hostname = name.downcase
        if AnsibleReportScanner.ansible_report?(raw) &&
           (Resolv::IPv4::Regex.match?(hostname) || Resolv::IPv6::Regex.match?(hostname)) &&
           Nic::Interface.find_by(:ip => hostname)
          @host = Nic::Interface.find_by(:ip => hostname).host
        end
        super
        partial_hostname_match(hostname)
      end

      def partial_hostname_match(hostname)
        return @host unless @host.new_record?
        hosts = Host.where(Host.arel_table[:name].matches("#{hostname}.%"))
        if hosts.count > 1
          msg = "More than 1 host found for name #{hostname}, "
          msg += 'please use host FQDN when uploading reports'
          Rails.logger.warn msg
          return @host
        end
        @host = hosts.first || @host
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
foreman_ansible-12.0.5 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-10.4.3 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-12.0.4 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-10.4.2 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-12.0.3 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-12.0.2 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-12.0.1 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-12.0.0 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-11.2.1 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-7.1.7 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-11.2.0 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-11.1.2 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-7.1.6 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-11.1.1 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-10.4.1 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-7.1.5 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-11.1.0 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-10.4.0 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-10.3.0 app/services/foreman_ansible/ansible_report_importer.rb
foreman_ansible-11.0.0 app/services/foreman_ansible/ansible_report_importer.rb