1
votes

While extending a puppet module which should manage /etc/network/interfaces I am facing the following problem:

The old module just reads some vars from hiera and creates the file with one interface through a template. To remove this limitation I added a hash to hiera containing other interfaces and their params. Through the puppet-concat module I want to add them to the interfaces file.

But if first the file for the template and later the file for the concat gets declared a Duplicate declaration error is thrown.

How can I first use the template and later concat to the file? Or is this not possible?

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: ..../modules/lip_network/manifests/debian.pp:16 cannot redeclare at /etc/puppetlabs/code/modules/concat/manifests/init.pp:179 at .../init.pp:179:5 at .../modules/lip_network/manifests/debian.pp:21

Class Code:

class lip_network::debian
{
  $ipaddress  = $::lip_network::ipaddress
  $netmask    = $::lip_network::netmask
  $gateway    = $::lip_network::gateway
  $dns1       = $::lip_network::dns1
  $domain     = $::lip_network::domain
  $iface      = $::lip_network::iface

  package { 'resolvconf':
    ensure  => latest,
  }

  file { '/etc/network/interfaces':
    mode    => '0644',
    owner   => 'root',
    content =>  template("${module_name}/interfaces.erb"),
  }
  concat { '/etc/network/interfaces':
    ensure => present,
  }
  $interface_configs = hiera_hash(lip_network_multi_interfaces::interfaces)
  $interface_list = keys($interface_configs)

  concat::fragment { "test_interfaces":
      target  => '/etc/network/interfaces',
      content => 'auto em0\niface em0 inet static',
      order   => "10"
  }


  # apparently /etc/init.d/networking does not regenerate
  exec { 'iface restart':
    command     => "ifdown ${iface} ; ifup ${iface}",
    refreshonly => true,
    subscribe   => File['/etc/network/interfaces'],
  }
}
2
Doesn't answer your question, but there are defined types and providers module that might make this easier? github.com/voxpupuli/puppet-network :) - Peter Souter

2 Answers

2
votes

You should turn the portion of the file coming from template("${module_name}/interfaces.erb") into a template fragment itself. You can make sure that portion is at the beginning of the file by using an order lower than the 10 you use for the other portions:

concat::fragment { "interfaces_main":
    target  => '/etc/network/interfaces',
    content => ("${module_name}/interfaces.erb"),
    order   => "5"
}
1
votes

Simplest solution turned out to be using an inline_template containing the code of the old template file and then add all interfaces and parameters with Embedded Ruby code:

# Debian old schoold network settings
class lip_network::debian
{
  $ipaddress  = $::lip_network::ipaddress
  $netmask    = $::lip_network::netmask
  $gateway    = $::lip_network::gateway
  $dns1       = $::lip_network::dns1
  $domain     = $::lip_network::domain
  $iface      = $::lip_network::iface
  $interfaceconfigs = hiera_hash(lip_network::interfaces)

  package { 'resolvconf':
    ensure  => latest,
  }
# creates inline_template; 
# the first interface is defined via static vars read from hiera
# further interfaces are added via ERB and an hash from hiera
$content = inline_template('
auto lo
 iface lo inet loopback

auto <%= @iface %> 
iface <%= @iface %> inet static
    address <%= @ipaddress %>
    netmask <%= @netmask %> 
    gateway <%= @gateway %>
    dns-nameservers <%= @dns1 %>
    dns-search <%= @domain %>

<% @interfaceconfigs.each do |interfacename, interfaceparams| -%>
auto <%= interfacename %>
iface <%= interfacename %> inet static
<% interfaceparams.each do |key, value| -%>
    <%= key %> <%= value%>
<% end %>
<% end %>'
)

file { '/etc/network/interfaces':
  ensure  => file,
  mode    => '0644',
  owner   => 'root',
  content => $content,
}


  # apparently /etc/init.d/networking does not regenerate
  # resolvconf settings O.o
  exec { 'iface restart':
    command     => "ifdown ${iface} ; ifup ${iface}",
    refreshonly => true,
    subscribe   => File['/etc/network/interfaces'],
  }
}