Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve doc/logs/examples to use objects based facts (#44) #57

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions default_hostgroup.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# List the regexes in the order of priority, you can have as many as you like. The
# first one to match will be used. Debug statements are written to the log if you
# are getting the wrong group applied.
# Look for DefaultHostgroupMatch in production.log to find logs related to this
# plugin
---
:default_hostgroup:
:facts_map:
Expand All @@ -25,3 +27,7 @@
"osfamily": "Darwin"
"Base":
"hostname": ".*"
"debian/11":
"os::distro::codename": "bullseye"
"default/windows/server2019":
"os::windows::product_name": '^Windows Server 2019 .*'
12 changes: 6 additions & 6 deletions lib/default_hostgroup_base_host_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def import_facts(facts, source_proxy = nil, without_alias = false)
# Check settings are created
return result unless settings_exist?

Rails.logger.debug "DefaultHostgroupMatch: performing Hostgroup match"
Rails.logger.debug "DefaultHostgroupMatch: performing Hostgroup match for #{self.host}"

return result unless host_new_or_forced?
return result unless host_has_no_hostgroup_or_forced?
Expand Down Expand Up @@ -60,9 +60,9 @@ def group_matches?(facts)
facts.each do |fact_name, fact_regex|
fact_regex.gsub!(%r{(\A/|/\z)}, "")
host_fact_value = self.host.facts[fact_name]
Rails.logger.info "Fact = #{fact_name}"
Rails.logger.info "Regex = #{fact_regex}"
return true if Regexp.new(fact_regex).match?(host_fact_value)
match_fact_value = Regexp.new(fact_regex).match?(host_fact_value)
Rails.logger.info "DefaultHostgroupMatch: Host=#{self.host} Fact=#{fact_name} Value=#{host_fact_value} Regex=#{fact_regex} Match?=#{match_fact_value}"
return true if match_fact_value
end
false
end
Expand All @@ -80,7 +80,7 @@ def host_new_or_forced?
# hosts have already been saved during import_host, so test the creation age instead
new_host = ((Time.current - self.host.created_at) < 300)
unless new_host && self.host.hostgroup.nil? && self.host.reports.empty?
Rails.logger.debug "DefaultHostgroupMatch: skipping, host exists"
Rails.logger.debug "DefaultHostgroupMatch: skipping #{self.host}, host exists"
return false
end
end
Expand All @@ -90,7 +90,7 @@ def host_new_or_forced?
def host_has_no_hostgroup_or_forced?
unless Setting[:force_hostgroup_match]
if self.host.hostgroup.present?
Rails.logger.debug "DefaultHostgroupMatch: skipping, host has hostgroup"
Rails.logger.debug "DefaultHostgroupMatch: skipping, host #{self.host} has hostgroup"
return false
end
end
Expand Down