Hi guys I have a custom fact envt, it was working fine but now I have to make some changes in it; puppet client''s hostname is of the type 1234qa-abc-de.local where 1234 can be any thing of any length.My custom fact should contain qa, ie I want to check in the first part(before the first -) of the hostname if there exists a pattern/word qa and if it exists then my custom fact for envt should be qa. My old envt.rb code is: require ''facter'' Facter.add("envt") do setcode do %x{hostname -s|cut -c 1-2}.chomp end end I am not good with regex/ patterns please help me out guys. Thanks -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Hi, I use something like this. ... hostnamevar.scan(/^\d+-qa-/) ... Here is the best tool on the planet for writing and testing regexes: http://www.rubular.com Den On 25/08/2011, at 11:43, newguy <aimanparvaiz@gmail.com> wrote:> Hi guys > I have a custom fact envt, it was working fine but now I have to make > some changes in it; > puppet client''s hostname is of the type 1234qa-abc-de.local where 1234 > can be any thing of any length.My custom fact should contain qa, ie I > want to check in the first part(before the first -) of the hostname > if there exists a pattern/word qa and if it exists then my custom > fact > for envt should be qa. > > My old envt.rb code is: > > require ''facter'' > Facter.add("envt") do > setcode do > %x{hostname -s|cut -c 1-2}.chomp > end > end > > I am not good with regex/ patterns please help me out guys. > Thanks > > -- > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en. >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Hey thanks for this but where should I include this in envt.rb code. The following doesnt work, I know that this might be a silly question but am stuck plz help me require ''facter'' Facter.add("envt") do setcode do {%hostnamevar.scan(/^\d+-qa-/)}.chomp end end On Aug 24, 8:13 pm, Denmat <tu2bg...@gmail.com> wrote:> Hi, > > I use something like this. > > ... > hostnamevar.scan(/^\d+-qa-/) > ... > > Here is the best tool on the planet for writing and testing regexes: > > http://www.rubular.com > > Den > On 25/08/2011, at 11:43, newguy <aimanparv...@gmail.com> wrote: > > > > > > > > > Hi guys > > I have a custom fact envt, it was working fine but now I have to make > > some changes in it; > > puppet client''s hostname is of the type 1234qa-abc-de.local where 1234 > > can be any thing of any length.My custom fact should contain qa, ie I > > want to check in the first part(before the first -) of the hostname > > if there exists a pattern/word qa and if it exists then my custom > > fact > > for envt should be qa. > > > My old envt.rb code is: > > > require ''facter'' > > Facter.add("envt") do > > setcode do > > %x{hostname -s|cut -c 1-2}.chomp > > end > > end > > > I am not good with regex/ patterns please help me out guys. > > Thanks > > > -- > > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > > To post to this group, send email to puppet-users@googlegroups.com. > > To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. > > For more options, visit this group athttp://groups.google.com/group/puppet-users?hl=en.-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Hi That was kind of pseudo code. In your example something like this: setcode do %x{hostname}.scan(regex goes here) end Den On 25/08/2011, at 15:52, newguy <aimanparvaiz@gmail.com> wrote:>>> setcode do >>> %x{hostname -s|cut -c 1-2}.chomp-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Hey Denmat Thanks for all your help but it seems that my little/no knowledge of ruby is creating a problem, I did what you suggested to get the following: sh: Syntax error: word unexpected (expecting ")") Here is the code: require ''facter'' Facter.add("envt") do setcode do %x{hostname.scan(/^\d+qa-/)}.chomp end end I tried to play around, changing sysntax, looked up google for syntax of ruby''s scan function but no success and hence am bothering you again. Please help On Aug 25, 12:30 am, Denmat <tu2bg...@gmail.com> wrote:> Hi > > That was kind of pseudo code. In your example something like this: > > setcode do > %x{hostname}.scan(regex goes here) > end > > Den > > On 25/08/2011, at 15:52, newguy <aimanparv...@gmail.com> wrote: > > > > > > > > >>> setcode do > >>> %x{hostname -s|cut -c 1-2}.chomp-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Aug 24, 8:43 pm, newguy <aimanparv...@gmail.com> wrote:> Hi guys > I have a custom fact envt, it was working fine but now I have to make > some changes in it; > puppet client''s hostname is of the type 1234qa-abc-de.local where 1234 > can be any thing of any length.My custom fact should contain qa, ie I > want to check in the first part(before the first -) of the hostname > if there exists a pattern/word qa and if it exists then my custom > fact > for envt should be qa. > > My old envt.rb code is: > > require ''facter'' > Facter.add("envt") do > setcode do > %x{hostname -s|cut -c 1-2}.chomp > end > end > > I am not good with regex/ patterns please help me out guys.Tangential issue: why are you doing this with a custom fact? The fact value depends entirely the node''s hostname, which is already exposed to Puppet as a the standard fact $hostname. You can and should set envt on the master based on the $hostname fact. I offer examples below. Before the examples, however, I observe that your description of the needed fact value does not match the old fact code. The old fact returns the first two characters of the hostname regardless of what they are, so are you sure that what you now want isn''t the last two preceding the first hyphen? The old fact could have been implemented via this one-liner at the top level of your site.pp: $envt = regsubst($hostname, ''^(.{2}).*'', ''\1'') It would no longer be a fact per se if you did it that way, but the manifests that use it would be unable to distinguish. I''m going to suggest that you use that approach to implement the revised version of your fact: $envt = regsubst($hostname, ''^[^-]*([^-]{2})-.*'', ''\1'') That''s more general than what you actually asked for, in that it will give you the two characters preceding the first dash in the hostname, whatever they may be (that is, even if they aren''t "qa"). In English, it reads something like () At the beginning of the string (^), () zero or more characters that are not hyphens ([^-]*), followed by () exactly two characters that are not hyphens, as a captured group (([^-]{2})), followed by () a hyphen (-), followed by () anything (.*) The function uses as its result the first (and only) captured group (\1). If the hostname does not contain at least one hyphen or if there are fewer than two characters in it before the first hyphen then that regex won''t match it. In that case the value assigned to $envt should be an empty string. Note that two switch from a custom fact to an ordinary global variable as I suggest, you probably will need to clear the custom fact code from your nodes. You can do that by removing its .rb file, but if you are using pluginsync then you might find it easier to simply synchronize a version of the .rb that is empty. If you must do it as a custom fact, then this ought to work: require ''facter'' Facter.add("envt") do setcode do %x{hostname}.sub(/^[^-]*([^-]{2})-.*/m, ''\1'') end end But I really don''t see the point of making things so complicated. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.