Puppet使用文件模板和concat重复声明错误

时间:2016-03-08 09:19:31

标签: templates puppet concat

在扩展应该管理/etc/network/interfaces的木偶模块时,我遇到了以下问题:

旧模块只是从hiera读取一些变量,并通过模板创建一个接口的文件。 为了消除这个限制,我在包含其他接口及其参数的hiera中添加了一个哈希。通过puppet-concat模块,我想将它们添加到interfaces文件中。

但是,如果第一个模板的文件和稍后的concat文件被声明,则抛出重复声明错误。

我如何首先使用该模板,然后再连接到该文件?或者这不可能吗?

  

错误:无法从远程服务器检索目录:错误400开启   服务器:评估错误:评估资源声明时出错,   评估错误:评估资源声明时出错,   重复声明:.... / modules / lip_network / manifests / debian.pp:16   无法重新声明   /etc/puppetlabs/code/modules/concat/manifests/init.pp:179 at   ... / init.pp:179:5 at ... / modules / lip_network / manifests / debian.pp:21

班级代码:

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 个答案:

答案 0 :(得分:2)

您应该将文件的部分从template("${module_name}/interfaces.erb")转换为模板片段本身。您可以使用低于您用于其他部分的10的订单来确保该部分位于文件的开头:

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

答案 1 :(得分:1)

最简单的解决方案是使用包含旧模板文件代码的inline_template,然后使用嵌入式Ruby代码添加所有接口和参数:

# 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'],
  }
}