Template variables from bash output


#1

I need to add “interface to Internet” to template.
I tried this approach but there is empty place in my file after I run chef-client…

bash "Get interface" do
    Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
    command = "route -n | awk '$1 ~ /0.0.0.0/ {print $NF}'"
    so = shell_out(command)
    output = so.stdout   
  end

  template '/etc/telegraf/telegraf.conf' do
    source 'config_telegraf.erb'
    owner 'root'
    group 'root'
    mode '0644'
    notifies :restart, "service[telegraf]"
    variables(
     :interface => @output
    )
  end

And my template sample
interfaces = ["<%= @interface %>"]


#2

Using ohai is much better approach.
interfaces = ["<%= node['network']['default_interface'] %>"]


#3

coul, may I urge you not to insist that such settings be derived directly from deduced local network configurations, or from the node derived settings published by Chef? Not that those are not great places to get the base value, especially the chef node settings. Rather, I’d suggest that you set that as a default attribute in your cookbook from that value, to allow you to reset it from a role or an alternative node attribute in your cookbook.

I suggest this because it’s easy to hardcode such settings in your recipes, then find later that your recipe has to deal with an unexpected setup such as an internal VLAN where you really do prefer the traffic, or activating a VPN and setting the external routes to go through the VPN leading to network confusion, etc.