Romain Pelisse
2013-Sep-27 09:47 UTC
[Puppet Users] Generating a variable based on the hostname
Hi all, I''m wondering what would be the best approach to extract a value from the hostname. In my case, the hostname contains the type of environment (dev, int, qa, prod) and I would like to have a variable called $env which contains will be extracted from such FQDNs: - vm-role-app-prod-1 - vm-role-app-qa-1 ... I''ve tried using split, but it was not really practical. Any better idea ? Short to deploy a custom fact to add this value to the facts.. PS: I''m using Puppet 2.7.21 -- Romain PELISSE, *"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it" -- Terry Pratchett* Belaran ins Prussia (blog) <http://blog.wordpress.belaran.eu/> (... finally up and running !) -- 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 post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Peter Bukowinski
2013-Sep-27 10:58 UTC
Re: [Puppet Users] Generating a variable based on the hostname
A custom fact that uses regex to grab the fourth, dash-separated value from the hostname seems like a good way to go as it would make the $env value accessible globally. Alternatively, you can use a selector within your manifests: $env = $hostname ? { /-dev-/ => ''dev'', /-int-/ => ''int'', /-qa-/ => ''qa'', /-prod-/ => ''prod'', } -- Peter> On Sep 27, 2013, at 5:47 AM, Romain Pelisse <belaran@gmail.com> wrote: > > Hi all, > > I''m wondering what would be the best approach to extract a value from the hostname. In my case, the hostname contains the type of environment (dev, int, qa, prod) and I would like to have a variable called $env which contains will be extracted from such FQDNs: > > - vm-role-app-prod-1 > - vm-role-app-qa-1 > ... > > I''ve tried using split, but it was not really practical. Any better idea ? Short to deploy a custom fact to add this value to the facts.. > > PS: I''m using Puppet 2.7.21-- 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 post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Sneha More
2013-Sep-27 11:18 UTC
[Puppet Users] Re: Generating a variable based on the hostname
Hi, you can use fact like, env.rb Facter.add("env") do setcode do Facter::Util::Resolution.exec(''hostname | cut -d"-" -f4'') end end I hope this will work fine. Thanks & Regards, Sneha More, NTT DATA GTS, OSS Center, India (Pune) On Friday, September 27, 2013 3:17:49 PM UTC+5:30, Romain PELISSE wrote:> > Hi all, > > I''m wondering what would be the best approach to extract a value from the > hostname. In my case, the hostname contains the type of environment (dev, > int, qa, prod) and I would like to have a variable called $env which > contains will be extracted from such FQDNs: > > - vm-role-app-prod-1 > - vm-role-app-qa-1 > ... > > I''ve tried using split, but it was not really practical. Any better idea ? > Short to deploy a custom fact to add this value to the facts.. > > PS: I''m using Puppet 2.7.21 > > -- > Romain PELISSE, > *"The trouble with having an open mind, of course, is that people will > insist on coming along and trying to put things in it" -- Terry Pratchett* > Belaran ins Prussia (blog) <http://blog.wordpress.belaran.eu/> (... > finally up and running !) >-- 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 post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Sep-27 14:17 UTC
[Puppet Users] Re: Generating a variable based on the hostname
On Friday, September 27, 2013 4:47:49 AM UTC-5, Romain PELISSE wrote:> > Hi all, > > I''m wondering what would be the best approach to extract a value from the > hostname. In my case, the hostname contains the type of environment (dev, > int, qa, prod) and I would like to have a variable called $env which > contains will be extracted from such FQDNs: > > - vm-role-app-prod-1 > - vm-role-app-qa-1 > ... > > I''ve tried using split, but it was not really practical. Any better idea ? > Short to deploy a custom fact to add this value to the facts.. > >I don''t see why a custom fact would be a good implementation of this. One of the regex-based facilities supported by Puppet would be better. Or split() -- what was wrong with that? Anyway, here''s a way to do exactly what you asked via Puppet''s built-in regsubst() function: $env = regsubst($hostname, ''^vm-role-app-([^-]+)-.*'', ''\1'') or here''s an alternative that makes different assumptions about the form of incoming hostnames: $env = regsubst($hostname, ''.*-(dev|int|ga|prod)-.*'', ''\1'') John -- 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 post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Erik Dalén
2013-Sep-27 14:35 UTC
Re: [Puppet Users] Re: Generating a variable based on the hostname
On 27 September 2013 16:17, jcbollinger <John.Bollinger@stjude.org> wrote:> > > On Friday, September 27, 2013 4:47:49 AM UTC-5, Romain PELISSE wrote: >> >> Hi all, >> >> I''m wondering what would be the best approach to extract a value from the >> hostname. In my case, the hostname contains the type of environment (dev, >> int, qa, prod) and I would like to have a variable called $env which >> contains will be extracted from such FQDNs: >> >> - vm-role-app-prod-1 >> - vm-role-app-qa-1 >> ... >> >> I''ve tried using split, but it was not really practical. Any better idea >> ? Short to deploy a custom fact to add this value to the facts.. >> >> > I don''t see why a custom fact would be a good implementation of this. One > of the regex-based facilities supported by Puppet would be better. Or > split() -- what was wrong with that? > > Anyway, here''s a way to do exactly what you asked via Puppet''s built-in > regsubst() function: > > $env = regsubst($hostname, ''^vm-role-app-([^-]+)-.*'', ''\1'') > > or here''s an alternative that makes different assumptions about the form > of incoming hostnames: > > $env = regsubst($hostname, ''.*-(dev|int|ga|prod)-.*'', ''\1'') > > >I think a custom fact for it is nice because it makes it accessible outside of puppet if you want to. And it would also be stored in puppetdb so you can query for hosts based on that value. -- Erik Dalén -- 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 post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.