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 :)

On Friday, October 21, 2016, <[email protected]> wrote:

> 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?
>
>
>
> On Friday, October 21, 2016 at 3:05:06 PM UTC-5, [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');> wrote:
>>
>> 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
>>
>>
>> --
> 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 [email protected]
> <javascript:_e(%7B%7D,'cvml','puppet-users%[email protected]');>
> .
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/puppet-users/3caf42f8-bd4a-4c38-aebc-d88ba0a6360b%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/3caf42f8-bd4a-4c38-aebc-d88ba0a6360b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>


-- 

Rob Nelson
[email protected]

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CAC76iT_wUBMhFuOLSQt118SL7jKrqRZB0Ge-f6D-J%2BOgo80sYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to