deet
2010-Mar-25 18:27 UTC
[Puppet Users] facter or ifconfig to find hosts with ip on certain subnet
Hello. I''m running facter 1.5.7 and puppet 0.25.4. I''m trying to create a manifest that would identify if a host has an IP address in a certain subnet. If the host does have an IP in the specified subnet then some static route mangling would happen. The desired subnets don''t align with a an interface name so I can''t just check for the existence of say network_eth2. Facter already contains all the info I would like to use to make my decision but I don''t know how to ask it. For example facter shows this output network_e1000g0 => 14.9.32.0 network_e1000g1 => 192.168.70.0 network_igb0 => 10.55.12.0 And I want to say something like (pseudo code) if network_* = 10.55.12.0 then static route magic here However I can''t quite grasp how I would even go about doing this? My fallback solution is just to do an exec ifconfig, grep, awk etc but if facter already has what I want to know I figured I would see if someone could help me understand how to ask it for the info. Any tips are appreciated. TIA. Derek. -- 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.
Ashley Penney
2010-Mar-25 19:05 UTC
Re: [Puppet Users] facter or ifconfig to find hosts with ip on certain subnet
I made a custom fact for something kind of similar to this: require ''facter'' Facter.add("networklocation") do setcode do result = case Facter["ipaddress"].value when /10.241.209/: "209" else "" end result end end It''s kind of horrible and I''m kind of terrible at this stuff, but it did let me make a custom fact that I could then process further. Maybe you can just do something similar until someone tells you a smarter way! On Thu, Mar 25, 2010 at 2:27 PM, deet <someword@gmail.com> wrote:> Hello. > I''m running facter 1.5.7 and puppet 0.25.4. I''m trying to create a > manifest that would identify if a host has an IP address in a certain > subnet. If the host does have an IP in the specified subnet then > some static route mangling would happen. The desired subnets don''t > align with a an interface name so I can''t just check for the > existence of say network_eth2. > Facter already contains all the info I would like to use to make my > decision but I don''t know how to ask it. For example facter shows > this output > > network_e1000g0 => 14.9.32.0 > network_e1000g1 => 192.168.70.0 > network_igb0 => 10.55.12.0 > > And I want to say something like (pseudo code) > if network_* = 10.55.12.0 > then > static route magic here > > However I can''t quite grasp how I would even go about doing this? > > My fallback solution is just to do an exec ifconfig, grep, awk etc > but if facter already has what I want to know I figured I would see if > someone could help me understand how to ask it for the info. > Any tips are appreciated. > TIA. Derek. > > -- > 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.
donavan
2010-Mar-26 08:39 UTC
[Puppet Users] Re: facter or ifconfig to find hosts with ip on certain subnet
> And I want to say something like (pseudo code) > if network_* = 10.55.12.0 > then > static route magic hereFrom this example you''re trying to detect it any interface is on the 10.55.12.0 network, correct? Are you also trying to match a set of networks (10.55.12.0, 10.55.13.0, etc) with different behaviour for each network? Are you looking for a custom fact that will report a magic keyword? What Ashley posted is, conceptually, the same as a ''location'' fact I made for work. It maps the nodes primary ipaddress to a string physical location. Extending that to check if any interface matches is pretty trivial. You could also assign the output of a template to a variable, then evaluate that. There are examples in Puppet_Templating on the wiki that seem pretty similar. -- 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.
deet
2010-Mar-26 16:19 UTC
[Puppet Users] Re: facter or ifconfig to find hosts with ip on certain subnet
> From this example you''re trying to detect it any interface is on the > 10.55.12.0 network,That''s correct. I have 4 specific subnet''s I''m looking for in this example. A host may have zero interfaces on one of the specific subnets or just 1. I was wanting to return the ipaddress that the host has on one of the specific subnets.> > Are you looking for a custom fact that will report a magic keyword?I was hoping I would be able to ask facter for the information as it already contains it. I just couldn''t even figure out how to begin formulating such a question (like trying to decide where to bite into an oversized submarine sandwich!). After Ashley''s response and lack of other ideas I put together the following custom fact which will return the ipaddress the host has on the specific subnet''s (if the host has one). I''ve not ventured down the custom fact road before so this should be an little adventure. Facter.add("nsd_gateway") do setcode do # Get the array of ip''s on the machine output = %x{/sbin/ifconfig -a} ip = [] # Put the ip addresses into the ip array output.each_line do |s| ip.push($1) if s =~ /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ end ip.grep(/(14.1.22.|19.7.28.|15.0.8.)/) end end> > You could also assign the output of a template to a variable, then > evaluate that.Thanks for the alternate idea on using templating to solve the problem. I had not thought of that approach! Thanks! Derek -- 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.
donavan
2010-Mar-27 03:48 UTC
[Puppet Users] Re: facter or ifconfig to find hosts with ip on certain subnet
> host has one). I''ve not ventured down the custom fact road before so > this should be an little adventure.Your version certainly will work. I think this is how I might take a stab at it though: require ''facter'' require ''ipaddr'' require ''yaml'' Facter.add("nsd_gateway") do setcode do begin interfaces = Facter.value(:interfaces).split('','') nsd_gateway = nil interfaces.each do |iface| next unless (address Facter.value("ipaddress_#{iface}")) YAML::load(File.open(''/usr/local/etc/config.yml'')) [''nsd_networks''].each do |net| net = IPAddr.new(net) nsd_gateway = address if net.include?(address) end end nsd_gateway end end end Where your ''/usr/local/etc/config.yml'' file contains a definition of your nsd_networks: --- nsd_networks: - 14.1.22.0/24 - 19.7.28.0/24 - 15.0.8.0/24> Thanks for the alternate idea on using templating to solve the > problem. I had not thought of that approach!No problem. I can''t envision it offhand, but you *might* also be able to do it in a define. But youve already solved it for now. -- 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.
deet
2010-Mar-29 03:47 UTC
[Puppet Users] Re: facter or ifconfig to find hosts with ip on certain subnet
> interfaces = Facter.value(:interfaces).split('','') > nsd_gateway = nilI like the idea of accessing this information through facter instead of needlessly running ifconfig again.> interfaces.each do |iface| > next unless (address > Facter.value("ipaddress_#{iface}")) > YAML::load(File.open(''/usr/local/etc/config.yml''))Is their any "standard" location to put a config.yml file like in your above example? Or just where ever I happen to store config files at my location? I think i''ll extend your example for default gateway lookups. Thanks again for the additional ideas on how to approach this problem! Derek -- 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.
donavan
2010-Mar-30 01:32 UTC
[Puppet Users] Re: facter or ifconfig to find hosts with ip on certain subnet
On Mar 28, 8:47 pm, deet <somew...@gmail.com> wrote:> I like the idea of accessing this information through facter instead > of needlessly running ifconfig again.Indeed. Take a look at the facter libraries, they''re quite readable overall. Plenty of good bits in ''facter/util'' that you can use also.> Is their any "standard" location to put a config.yml file like in > your above example? Or just where ever I happen to store config files > at my location? I think i''ll extend your example for default gateway > lookups.I think /usr/local/etc/ is pretty standard for configs that aren''t supplied by the base os. If they''re specific to a single program you could also see "/usr/local/$program/". Check ''man hier'' of your platform of choice for more info. In my case we''re using config.yml to store all those site common settings that usually get embedded into 10 different scripts and then forgotten. Ive done similar facts for providing gateway & broadcast addresses based on existing facts. Those were used for dhcp servers to automagically build dhcp.conf settings for attached networks. Let me know if you have any questions. -- 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.