Templates 使用文件模板和concat时出现Puppet重复声明错误
在扩展应管理Templates 使用文件模板和concat时出现Puppet重复声明错误,templates,puppet,concat,Templates,Puppet,Concat,在扩展应管理/etc/network/interfaces的puppet模块时,我面临以下问题: 旧模块只是从hiera中读取一些变量,并通过一个模板用一个接口创建文件。 为了消除这个限制,我在hiera中添加了一个包含其他接口及其参数的哈希。通过puppet concat模块,我想将它们添加到接口文件中 但是,如果先声明模板的文件,然后声明concat的文件,则会抛出重复声明错误 如何先使用模板,然后再使用concat来创建文件?或者这是不可能的 错误:无法从远程服务器检索目录:上的错误400
/etc/network/interfaces
的puppet模块时,我面临以下问题:
旧模块只是从hiera中读取一些变量,并通过一个模板用一个接口创建文件。
为了消除这个限制,我在hiera中添加了一个包含其他接口及其参数的哈希。通过puppet concat模块,我想将它们添加到接口文件中
但是,如果先声明模板的文件,然后声明concat的文件,则会抛出重复声明错误
如何先使用模板,然后再使用concat来创建文件?或者这是不可能的
错误:无法从远程服务器检索目录:上的错误400
服务器:求值错误:求值资源语句时出错,
求值错误:求值资源语句时出错,
重复声明:..../modules/lip_network/manifests/debian.pp:16
不能重新申报
/etc/puppetlabs/code/modules/concat/manifests/init.pp:179 at
…/init.pp:179:5在…/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'],
}
}
您应该将来自
模板(${module\u name}/interfaces.erb”)
的文件部分转换为模板片段本身。您可以使用低于用于其他部分的10
的顺序来确保该部分位于文件的开头:
concat::fragment { "interfaces_main":
target => '/etc/network/interfaces',
content => ("${module_name}/interfaces.erb"),
order => "5"
}
最简单的解决方案是使用包含旧模板文件代码的内联_模板,然后使用嵌入的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'],
}
}
#Debian老派网络设置
类lip_网络::debian
{
$ipaddress=$::lip\U网络::ipaddress
$netmask=$::lip\u网络::netmask
$gateway=$::lip_网络::网关
$dns1=$::lip_网络::dns1
$domain=$::lip\u网络::域
$iface=$::lip\U网络::iface
$interfaceconfigs=hiera\u散列(lip\u网络::接口)
包{'resolvconf':
确保=>最新,
}
#创建内联模板;
#第一个接口是通过从hiera读取的静态变量定义的
#通过ERB和hiera的哈希添加更多接口
$content=inline_模板('
自动lo
iface lo inet环回
自动的
iface inet静态
地址
网络掩码
网关
dns名称服务器
dns搜索
自动的
iface inet静态
'
)
文件{'/etc/network/interfaces':
确保=>文件,
模式=>'0644',
所有者=>'root',
content=>$content,
}
#显然/etc/init.d/networking不会重新生成
#resolvconf设置O.O
exec{'iface restart':
command=>“ifdown${iface};ifup${iface}”,
refreshonly=>true,
订阅=>File['/etc/network/interfaces'],
}
}
great也有同样的想法:)。但是我必须先删除旧的接口文件,然后再用片段重新构建它。我如何才能在没有另一个重复声明错误的情况下做到这一点?这并不能回答您的问题,但有一些已定义的类型和提供程序模块可以使这一点变得更容易:)