Two Facter files seem to conflict

43 views
Skip to first unread message

aar...@gmail.com

unread,
Oct 21, 2016, 4:05:06 PM10/21/16
to Puppet Users
I have two custom facts and I have put each file in the directory for a single module.  Both facts get loaded onto my clients.

The problem I am having is that each fact runs successfully when it is by itself, but when both run at the same time, the results aren't as expected.

To test, I rename one fact as *.rb.bak, and then I get proper results.  Switch to the other fact, again, proper results.  Remove .bak so they both run, results are wrong.

Here are my .rb files.

All of my servers start with the letter a and have either an aX or aXXX which describes their physical location.  All of the servers end with 5 characters that describe the network they are on.  The remaining characters describe what the server does.

All of my workstations start with a #.

What seems to happen, is that when both facts are running, none of my servers start with "a" so all of my servers show up as workstations.

It appears that 'location_code.slice! seems to run, so the prefix of my hostname is gone and the first letter appears to be the part of the hostname that describes the servers function.

To test, I changed "a" in my code to "f" and my file server had the proper server_role of file.

I don't understand why this happens as my variables are different between the two script

I appreciate any assistance.

require 'facter'

Facter.add(:location_code) do
  setcode
do
    address
= Facter.value(:ipaddress).tr('.', '')
    address
= address.to_i
    location_code
= Facter.value(:hostname)
     
if address.between?(104127100, 104127255)
        location_code
.slice!(0,2)
     
else
        location_code
.slice!(0,4)
     
end
 
end
end




require 'facter'

Facter.add(:server_role) do
  setcode
do
    ip_address
= Facter.value(:ipaddress).tr('.', '')
    ip_address
= ip_address.to_i
    server_role
= Facter.value(:hostname)
   
if server_role[0,1] != "a"
      server_role
= 'workstation'
   
else
     
if ip_address.between?(104127100, 104127255)
        server_role
.slice!(0,2)
   
else
        server_role
.slice!(0,4)
   
end
    server_role
= server_role[0..-6]
   
end
 
end
end


warron.french

unread,
Oct 21, 2016, 4:40:48 PM10/21/16
to puppet...@googlegroups.com
Hello, sorry that I cannot contribute directly to your solution, but in the body of your email messsage you said all workstations start with a #, did you mean w?  The two characters on the keyboard are near each other.

--------------------------
Warron French


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/f0e17cd7-6179-4607-ab87-870bed5c4040%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

aar...@gmail.com

unread,
Oct 21, 2016, 4:44:51 PM10/21/16
to Puppet Users
Actually I meant that all of my workstations begin with a number. 

Thank you.

--------------------------
Warron French


To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.

warron.french

unread,
Oct 21, 2016, 4:47:31 PM10/21/16
to puppet...@googlegroups.com
OK, sorry aarb.

--------------------------
Warron French


To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/c2b30a49-5c0c-4281-82db-d9e58c15f650%40googlegroups.com.

aar...@gmail.com

unread,
Oct 22, 2016, 12:22:38 AM10/22/16
to Puppet Users
Some additional information.

I can confirm that the reason that 
server_role = Facter.value(:hostname)
if server_role[0,1] != "a"

isn't true for servers who's names do start with a is directly related to settings in the location_code.rb file
if I rem out everything in server_role except 
server_role = Facter.value(:hostname)
the hostname is printed with the first 2 or 4 characters removed as shown in location_code.erb
if ip_address.between?(104127100, 104127255)
        location_code
.slice!(0,2)
   
else
        location_code
.slice!(0,4)
   
end
If I change the numbers from 2 to 3, or 4 to 5 for example, the hostname is different on my remarked server_role.erb file

I would assume that the line in server_role.erb

server_role = Facter.value(:hostname)

would get the proper hostname from Facter, but it doesn't appear to.

Any ideas on why?

Rob Nelson

unread,
Oct 22, 2016, 1:42:56 AM10/22/16
to puppet...@googlegroups.com
I assume slice! modifies the string in place rather than returning the resulting string without modifying the original, as most ! funcs do. Is there a reason you are doing that rather than saving the result in another variable and returning that value at the end?

I ask this because string assignment under the covers doesn't copy the value, but creates a reference, so that modifying your new variable also changes the original variable's contents. I am boarding a plane so cannot confirm this, just a hunch. Regardless, I think you could get away with using slice instead of slice! by assigning to a temp variable along the way. It seems cleaner in general, and may as a bonus fix this, as well :)
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--

aar...@gmail.com

unread,
Oct 22, 2016, 10:18:18 AM10/22/16
to Puppet Users
Thank you Rob.  I have it working using slice and some new variables.

Here are my final scripts.  

Facter.add(:server_role) do
  setcode
do
    ip_address
= Facter.value(:ipaddress).tr('.', '')
    ip_address
= ip_address.
to_i
    host
= Facter.value(:hostname)
   
if host[0,1] != "a"
      server_role
= 'workstation'
   
else
     
if ip_address.between?(1921680160, 1921680170)
        server_role
= host.slice(0,2)
     
else
        server_role
= host.slice(0.4)
     
end
   
end
 
end
end

Facter.add(:location_code) do
  setcode
do
    address
= Facter.value(:ipaddress).tr('.', '')
    address
= address.
to_i
    host
= Facter.value(:hostname)
   
if host[0,1] != "a"
      net
= Facter.value(:network)
      net
= net[0..-3] += '.30'
      net
= Facter::Core::Execution.exec("nslookup #{net} | grep name | awk '{print $4}'")
      net
= net.partition('.').first
      net
= net[0..3]
   
else
     
if address.between?(1921680160, 1921680170)
        location_code
= host.slice(0,2)
     
else
        location_code
= host.slice(0,4)
     
end
   
end
 
end
end





Rob Nelson

unread,
Oct 22, 2016, 11:03:15 AM10/22/16
to puppet...@googlegroups.com
Awesome, glad to hear that helped!
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/e0a376db-83c9-4366-9ddd-318e6e298b36%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--

Reply all
Reply to author
Forward
0 new messages