resources - 迭代 puppet 资源收集器
问题描述
我正在尝试开发一个具有已定义资源的木偶类,该资源为网站创建配置。
定义的资源必须做的一件事是将网站的 IP 地址分配给一个虚拟接口。由于项目的限制,这是通过 NetworkManager 完成的。
所以我必须生成一个像
[connection]
id=dummydsr
uuid=50819d31-8967-4321-aa34-383f4a658789
type=dummy
interface-name=dummydsr
permissions=
[ipv4]
method=manual
#IP Addresses come here
ipaddress1=1.2.3.4/32
ipaddress2=5.6.7.8/32
ipaddress3=8.7.6.5/32
[ipv6]
method=ignore
对于已定义资源的每个实例,都有一行 ipaddressX=... 。
我的问题是如何跟踪定义的资源被实例化的次数,以便我可以以某种方式增加一个计数器并生成 ipaddress 行。
或者对于每个实例化的已定义资源,将 IP 地址附加到一个数组中,稍后我可以使用该数组来构建文件
解决方案
这不太理想,因为我更愿意将它放在定义的资源中,但是因为我使用来自哈希的数据实例化定义的资源,所以我使用所述哈希来迭代该部分。
class xxx_corp_webserver (
Hash $websites ={}
){
create_resources('xxx_corp_webserver::website', $websites)
# This would be nicer inside the defined class, but I did not find any other way
# Build and array with the IP addresses which are for DSR
$ipaddresses = $websites.map | $r | {
if $r[1]['enabledsr'] {
$r[1]['ipaddress']
}
}
# For each DSR address add the line
$ipaddresses.each | Integer $index , String $ipaddress | {
$num = $index+1
file_line{"dummydsr-ipaddress${num}":
ensure => present,
path => '/etc/NetworkManager/system-connections/dummydsr',
line => "address${num} = ${ipaddress}/32",
match => "^address.* = ${ipaddress}/32",
after => '# IP Addresses come here',
notify => Service['NetworkManager'],
require => File['/etc/NetworkManager/system-connections/dummydsr'],
}
}
}
推荐阅读
- azure-ad-b2c - 如何删除 Azure DevOps 企业应用程序?
- java - 为什么会发生这种情况,我该如何纠正这个“错误”?
- r - 如何在漏斗图中固定大小但动态悬停文本?
- java - 从碎片按钮单击时如何获取下一个活动
- javascript - 有没有办法根据 LightningChart JS 中的点数据命名系列?
- sql-server - 使用 Entity Framework Fluent API 的带有 where 子句的唯一索引
- python - 从具有多个日期/价格列的数据框在 pandas 中创建一个面板
- python-3.x - 如何循环csv并写入新的xlsx
- r - 通过组变量有效地扩展 data.table 中的网格
- javascript - 从反应组件的 HTML 中删除“role=banner”?