2016-03-08 64 views
1

木偶重複申報錯誤而延伸的應該管理/etc/network/interfaces我面臨着以下問題傀儡模塊:使用文件模板和CONCAT

舊模塊剛剛從hiera讀一些增值經銷商,並通過創建一個接口文件模板。 要刪除此限制,我添加了一個散列到包含其他接口及其參數的hiera。通過puppet-concat模塊,我想將它們添加到interfaces文件中。

但是,如果首先爲模板文件和後來的concat文件獲取聲明瞭重複聲明錯誤被拋出。

我該如何首先使用該模板,然後再使用該文件進行concat?或者這是不可能的?

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

不回答你的問題,但有定義的類型和提供程序模塊,可能會使這更容易? https://github.com/voxpupuli/puppet-network :) –

回答

2

你應該把文件從template("${module_name}/interfaces.erb")進入模板片段本身的部分。您可以確保部分是在文件的開頭使用比你使用的其他部分10低的順序爲:

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

偉大的有相同的想法:)。但之後我必須刪除舊的interfaces文件,然後再用碎片重新構建它。我怎麼能沒有另一個重複的聲明錯誤? – prototyp

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