I''m trying to manipulate the following facter variable: ipaddress => 10.85.207.2 What I need is the second group of digits. If it''s 84 then location is A, but if it''s 85 then location is B. In puppet I couldn''t grab this second set of digits, but maybe there is a way? I''ve already tried to make a custom fact, but my ruby knowledge is *ahem* pretty basic. So far I''ve only managed to manipulate a string with chop!, but that really isn''t the way. Regular expressions in Ruby are a bit of a mystery to me.... Ideas anyone? -- 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.
Split & Join inside an inline template should work well. For eg., $nsip=inline_template(''<%= ipaddress.split(".")[0..1].collect{|x| x}.join(".") %>'') This will split an ip address, say 192.168.1.3, split it at each occurrence of a "." and will join the first two numbers ( referenced as [0..1] ), to give 192.168. So, in your case, I guess something like <%= ipaddress.split(".")[1] %> should work. On Thu, Oct 14, 2010 at 7:27 PM, Dennis vdM <cybernijntje@gmail.com> wrote:> I''m trying to manipulate the following facter variable: > > ipaddress => 10.85.207.2 > > What I need is the second group of digits. If it''s 84 then location is > A, but if it''s 85 then location is B. > In puppet I couldn''t grab this second set of digits, but maybe there > is a way? > > I''ve already tried to make a custom fact, but my ruby knowledge is > *ahem* pretty basic. > So far I''ve only managed to manipulate a string with chop!, but that > really isn''t the way. > Regular expressions in Ruby are a bit of a mystery to me.... > > Ideas anyone? > > -- > 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<puppet-users%2Bunsubscribe@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.
THNX! Just what I needed ;-) On 14 okt, 16:18, Mohit Chawla <mohit.chawla.bin...@gmail.com> wrote:> Split & Join inside an inline template should work well. > > For eg., > $nsip=inline_template(''<%= ipaddress.split(".")[0..1].collect{|x| > x}.join(".") %>'') > > This will split an ip address, say 192.168.1.3, split it at each occurrence > of a "." and will join the first two numbers ( referenced as [0..1] ), to > give 192.168. > > So, in your case, I guess something like <%= ipaddress.split(".")[1] %> > should work. > > > > On Thu, Oct 14, 2010 at 7:27 PM, Dennis vdM <cybernijn...@gmail.com> wrote: > > I''m trying to manipulate the following facter variable: > > > ipaddress => 10.85.207.2 > > > What I need is the second group of digits. If it''s 84 then location is > > A, but if it''s 85 then location is B. > > In puppet I couldn''t grab this second set of digits, but maybe there > > is a way? > > > I''ve already tried to make a custom fact, but my ruby knowledge is > > *ahem* pretty basic. > > So far I''ve only managed to manipulate a string with chop!, but that > > really isn''t the way. > > Regular expressions in Ruby are a bit of a mystery to me.... > > > Ideas anyone? > > > -- > > 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<puppet-users%2Bunsubscribe@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/puppet-users?hl=en.- Tekst uit oorspronkelijk bericht niet weergeven - > > - Tekst uit oorspronkelijk bericht weergeven --- 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.
I think the bigger question is, what problem are you trying solve with this? On Oct 14, 2010 10:26 AM, "Dennis vdM" <cybernijntje@gmail.com> wrote:> THNX! Just what I needed ;-) > > On 14 okt, 16:18, Mohit Chawla <mohit.chawla.bin...@gmail.com> wrote: >> Split & Join inside an inline template should work well. >> >> For eg., >> $nsip=inline_template(''<%= ipaddress.split(".")[0..1].collect{|x| >> x}.join(".") %>'') >> >> This will split an ip address, say 192.168.1.3, split it at eachoccurrence>> of a "." and will join the first two numbers ( referenced as [0..1] ), to >> give 192.168. >> >> So, in your case, I guess something like <%= ipaddress.split(".")[1] %> >> should work. >> >> >> >> On Thu, Oct 14, 2010 at 7:27 PM, Dennis vdM <cybernijn...@gmail.com>wrote:>> > I''m trying to manipulate the following facter variable: >> >> > ipaddress => 10.85.207.2 >> >> > What I need is the second group of digits. If it''s 84 then location is >> > A, but if it''s 85 then location is B. >> > In puppet I couldn''t grab this second set of digits, but maybe there >> > is a way? >> >> > I''ve already tried to make a custom fact, but my ruby knowledge is >> > *ahem* pretty basic. >> > So far I''ve only managed to manipulate a string with chop!, but that >> > really isn''t the way. >> > Regular expressions in Ruby are a bit of a mystery to me.... >> >> > Ideas anyone? >> >> > -- >> > You received this message because you are subscribed to the GoogleGroups>> > "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<puppet-users%2Bunsubscribe@googlegroups.com><puppet-users%2Bunsubscribe@googlegroups.com>>> > . >> > For more options, visit this group at >> >http://groups.google.com/group/puppet-users?hl=en.- Tekst uitoorspronkelijk bericht niet weergeven ->> >> - Tekst uit oorspronkelijk bericht weergeven - > > -- > 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 topuppet-users+unsubscribe@googlegroups.com<puppet-users%2Bunsubscribe@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.
There''s a split function in Puppet''s DSL as well. If you don''t want to do the inline_template() route. http://docs.puppetlabs.com/references/stable/function.html#split On Thu, Oct 14, 2010 at 10:26 AM, Dennis vdM <cybernijntje@gmail.com> wrote:> THNX! Just what I needed ;-) > > On 14 okt, 16:18, Mohit Chawla <mohit.chawla.bin...@gmail.com> wrote: >> Split & Join inside an inline template should work well. >> >> For eg., >> $nsip=inline_template(''<%= ipaddress.split(".")[0..1].collect{|x| >> x}.join(".") %>'') >> >> This will split an ip address, say 192.168.1.3, split it at each occurrence >> of a "." and will join the first two numbers ( referenced as [0..1] ), to >> give 192.168. >> >> So, in your case, I guess something like <%= ipaddress.split(".")[1] %> >> should work. >> >> >> >> On Thu, Oct 14, 2010 at 7:27 PM, Dennis vdM <cybernijn...@gmail.com> wrote: >> > I''m trying to manipulate the following facter variable: >> >> > ipaddress => 10.85.207.2 >> >> > What I need is the second group of digits. If it''s 84 then location is >> > A, but if it''s 85 then location is B. >> > In puppet I couldn''t grab this second set of digits, but maybe there >> > is a way? >> >> > I''ve already tried to make a custom fact, but my ruby knowledge is >> > *ahem* pretty basic. >> > So far I''ve only managed to manipulate a string with chop!, but that >> > really isn''t the way. >> > Regular expressions in Ruby are a bit of a mystery to me.... >> >> > Ideas anyone? >> >> > -- >> > 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<puppet-users%2Bunsubscribe@googlegroups.com> >> > . >> > For more options, visit this group at >> >http://groups.google.com/group/puppet-users?hl=en.- Tekst uit oorspronkelijk bericht niet weergeven - >> >> - Tekst uit oorspronkelijk bericht weergeven - > > -- > 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.
On Thursday 14 Oct 2010 14:57:08 Dennis vdM wrote:> I''m trying to manipulate the following facter variable: > > ipaddress => 10.85.207.2 > > What I need is the second group of digits. If it''s 84 then location is > A, but if it''s 85 then location is B. > In puppet I couldn''t grab this second set of digits, but maybe there > is a way? > > I''ve already tried to make a custom fact, but my ruby knowledge is > *ahem* pretty basic. > So far I''ve only managed to manipulate a string with chop!, but that > really isn''t the way. > Regular expressions in Ruby are a bit of a mystery to me.... > > Ideas anyone?We use the KSMETA section of Cobbler and a custom fact script (which I''ve uploaded at http://tinyurl.com/368hccf ) to pull the data from this field. This approach means that we can create arbitrary key->value pairs that we can use as custom facts. As an example, we currently have the following custom facts available to us through this method: datacentre dc_rack dc_suite dc_room mysql_master and a few others including which databases are replicated by a given host etc. It''s a lot easier than trying to split out IP Addresses etc and because KSMETA isn''t hard-coded on build, it means that we can update these values when a server is moved etc. Hope this is of some help, I know it''s not exactly what you''re trying to do but it may be a worthwhile alternative. Kind regards, Matt -- 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 Thu, Oct 14, 2010 at 7:18 AM, Mohit Chawla <mohit.chawla.binary@gmail.com> wrote:> Split & Join inside an inline template should work well.Wouldn''t it be a bit cleaner to use the builtin regsubst function? http://docs.puppetlabs.com/references/latest/function.html#regsubst "Example: Get the third octet from the node’s IP address: $i3 = regsubst($ipaddress,''^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$'',''\3'')" I really try to avoid inline_template unless I absolutely have to, even though the actual Ruby code there is cleaner than the long regex. If you really only want the third octet, you could use a much simpler regex than the one above. Obligatory plug for the site I always use for regex references: http://www.regular-expressions.info as it''s an easy domain name to remember.> For eg., > $nsip=inline_template(''<%= ipaddress.split(".")[0..1].collect{|x| > x}.join(".") %>'') > This will split an ip address, say 192.168.1.3, split it at each occurrence > of a "." and will join the first two numbers ( referenced as [0..1] ), to > give 192.168. > So, in your case, I guess something like <%= ipaddress.split(".")[1] %> > should work. > > > On Thu, Oct 14, 2010 at 7:27 PM, Dennis vdM <cybernijntje@gmail.com> wrote: >> >> I''m trying to manipulate the following facter variable: >> >> ipaddress => 10.85.207.2 >> >> What I need is the second group of digits. If it''s 84 then location is >> A, but if it''s 85 then location is B. >> In puppet I couldn''t grab this second set of digits, but maybe there >> is a way? >> >> I''ve already tried to make a custom fact, but my ruby knowledge is >> *ahem* pretty basic. >> So far I''ve only managed to manipulate a string with chop!, but that >> really isn''t the way. >> Regular expressions in Ruby are a bit of a mystery to me.... >> >> Ideas anyone? >> >> -- >> 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. >-- 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 Thu, Oct 14, 2010 at 9:55 PM, Nigel Kersten <nigel@explanatorygap.net>wrote:> On Thu, Oct 14, 2010 at 7:18 AM, Mohit Chawla > <mohit.chawla.binary@gmail.com> wrote: > > Split & Join inside an inline template should work well. > > Wouldn''t it be a bit cleaner to use the builtin regsubst function? > > http://docs.puppetlabs.com/references/latest/function.html#regsubst > > "Example: > > Get the third octet from the node’s IP address: > > $i3 > regsubst($ipaddress,''^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$'',''\3'')" > > > I really try to avoid inline_template unless I absolutely have to, > even though the actual Ruby code there is cleaner than the long regex. > If you really only want the third octet, you could use a much simpler > regex than the one above. > > Obligatory plug for the site I always use for regex references: > http://www.regular-expressions.info as it''s an easy domain name to > remember. > > > > > > For eg., > > $nsip=inline_template(''<%= ipaddress.split(".")[0..1].collect{|x| > > x}.join(".") %>'') > > This will split an ip address, say 192.168.1.3, split it at each > occurrence > > of a "." and will join the first two numbers ( referenced as [0..1] ), to > > give 192.168. > > So, in your case, I guess something like <%= ipaddress.split(".")[1] %> > > should work. > > > > > > On Thu, Oct 14, 2010 at 7:27 PM, Dennis vdM <cybernijntje@gmail.com> > wrote: > >> > >> I''m trying to manipulate the following facter variable: > >> > >> ipaddress => 10.85.207.2 > >> > >> What I need is the second group of digits. If it''s 84 then location is > >> A, but if it''s 85 then location is B. > >> In puppet I couldn''t grab this second set of digits, but maybe there > >> is a way? > >> > >> I''ve already tried to make a custom fact, but my ruby knowledge is > >> *ahem* pretty basic. > >> So far I''ve only managed to manipulate a string with chop!, but that > >> really isn''t the way. > >> Regular expressions in Ruby are a bit of a mystery to me.... > >> > >> Ideas anyone? > >> > >> -- > >> 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<puppet-users%2Bunsubscribe@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<puppet-users%2Bunsubscribe@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<puppet-users%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >Hi Nigel, Thanks for pointing that out, I wasn''t aware of regsubst. But, just for my curiosity, what are the disadvantages of using an inline template, in such ( trivial ? ) cases ? Would be nice to know. 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.
On Thu, Oct 14, 2010 at 9:48 AM, Mohit Chawla <mohit.chawla.binary@gmail.com> wrote:> > > On Thu, Oct 14, 2010 at 9:55 PM, Nigel Kersten <nigel@explanatorygap.net> > wrote: >> >> On Thu, Oct 14, 2010 at 7:18 AM, Mohit Chawla >> <mohit.chawla.binary@gmail.com> wrote: >> > Split & Join inside an inline template should work well. >> >> Wouldn''t it be a bit cleaner to use the builtin regsubst function? >> >> http://docs.puppetlabs.com/references/latest/function.html#regsubst >> >> "Example: >> >> Get the third octet from the node’s IP address: >> >> $i3 >> regsubst($ipaddress,''^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$'',''\3'')" >> >> >> I really try to avoid inline_template unless I absolutely have to, >> even though the actual Ruby code there is cleaner than the long regex. >> If you really only want the third octet, you could use a much simpler >> regex than the one above. >> >> Obligatory plug for the site I always use for regex references: >> http://www.regular-expressions.info as it''s an easy domain name to >> remember. >> >> >> >> >> > For eg., >> > $nsip=inline_template(''<%= ipaddress.split(".")[0..1].collect{|x| >> > x}.join(".") %>'') >> > This will split an ip address, say 192.168.1.3, split it at each >> > occurrence >> > of a "." and will join the first two numbers ( referenced as [0..1] ), >> > to >> > give 192.168. >> > So, in your case, I guess something like <%= ipaddress.split(".")[1] %> >> > should work. >> > >> > >> > On Thu, Oct 14, 2010 at 7:27 PM, Dennis vdM <cybernijntje@gmail.com> >> > wrote: >> >> >> >> I''m trying to manipulate the following facter variable: >> >> >> >> ipaddress => 10.85.207.2 >> >> >> >> What I need is the second group of digits. If it''s 84 then location is >> >> A, but if it''s 85 then location is B. >> >> In puppet I couldn''t grab this second set of digits, but maybe there >> >> is a way? >> >> >> >> I''ve already tried to make a custom fact, but my ruby knowledge is >> >> *ahem* pretty basic. >> >> So far I''ve only managed to manipulate a string with chop!, but that >> >> really isn''t the way. >> >> Regular expressions in Ruby are a bit of a mystery to me.... >> >> >> >> Ideas anyone? >> >> >> >> -- >> >> 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. >> > >> >> -- >> 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 Nigel, > > Thanks for pointing that out, I wasn''t aware of regsubst. But, just for my > curiosity, what are the disadvantages of using an inline template, in such ( > trivial ? ) cases ? Would be nice to know.I just find them less readable. those nasty angle brackets have sharp edges :) It''s probably a personal preference, but I try to only resort to inline_template if I can''t achieve what I need in a reasonable manner without it, and will often write a function instead of using inline_template if that''s an option. -- 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.