傀儡迭代字符串/数组 [英] Puppet iteration string/array

查看:31
本文介绍了傀儡迭代字符串/数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能在 Puppet 中想出解决这个问题的方法吗?

我有一个自定义事实,根据运行它的域生成一串 IP 地址,它可以解析为具有 1 到 n 个地址.

10.1.29.1"10.1.29.1,10.1.29.5"10.1.29.1,10.1.29.5,10.1.29.7"等等

我想将这些添加到主机文件中,例如生成的服务器名称为 servernameX;

10.1.29.1 myservername110.1.29.5 我的服务器名称 210.1.29.7 我的服务器名称 3

那么你怎么能做到这一点,因为 puppet 没有像for each"这样的数组迭代器?

解决方案

遗憾的是,即使您在将自定义事实拆分为逗号时使用自定义定义"来迭代数组,结果也将是不是你所期望的,甚至没有接近for each"循环——除了让你头疼之外,可能.

说了,我不确定这是否是你想要实现的,但看看这个方法:

$fact = '1.1.1.1,2.2.2.2,3.3.3.3'$servers = split($::fact, ',')$count = 大小($servers)$names = rack_expansion("host[01-${count}].address")文件{'/tmp/test.txt':内容 =>inline_template('<%= @servers.each_with_index.map {|v,i| "#{v}\t\t#{@names[i]}\n" } %>'),确保 =>展示}

我们有两个自定义函数:size()和bracket_expansion();然后我们使用他们在一个 hack 中提供的值,利用 inline_template() 函数利用对两个数组的并行访问来呈现文件的内容 - 一个具有来自您的事实的 IP 地址,另一个具有应遵循的主机名.

结果如下:

matti@acrux ~ $ cat |傀儡申请$fact = '1.1.1.1,2.2.2.2,3.3.3.3'$servers = split($::fact, ',')$count = 大小($servers)$names = rack_expansion("host[01-${count}].address")文件{'/tmp/test.txt':内容 =>inline_template('<%= @servers.each_with_index.map {|v,i| "#{v}\t\t#{@names[i]}\n" } %>'),确保 =>展示}注意:/Stage[main]//File[/tmp/test.txt]/ensure: created注意:0.07秒完成目录运行matti@acrux ~ $ cat/tmp/test.txt1.1.1.1 host01.address2.2.2.2 host02.address3.3.3.3 host03.address马蒂@acrux ~ $

size() 和bracket_expansion() 函数都可以在这里找到:

https://github.com/kwilczynski/puppet-functions/树/master/lib/puppet/parser/functions/

我希望这会有所帮助:-)

Can you think of a way to solve this problem in Puppet?

I have a custom fact with generates a string of IP addresses depending on the domain it is run on, it can resolve to have 1 to n addresses.

"10.1.29.1"
"10.1.29.1,10.1.29.5"
"10.1.29.1,10.1.29.5,10.1.29.7"
etc

I want to add these to the host file with a generated server names of servernameX for example;

10.1.29.1 myservername1
10.1.29.5 myservername2
10.1.29.7 myservername3

So how can you do this as puppet doesn't have an array iterator like "for each"?

解决方案

Sadly, even if you go about and use a custom "define" to iterate over an array upon splitting your custom fact on a comma, the result will be rather not what you expect and not even close to a "for each" loop -- aside of causing you a headache, probably.

Said that, I am not sure if this is what you want to achieve, but have a look at this approach:

$fact = '1.1.1.1,2.2.2.2,3.3.3.3'

$servers = split($::fact, ',')
$count   = size($servers)

$names = bracket_expansion("host[01-${count}].address")

file { '/tmp/test.txt':
  content => inline_template('<%= @servers.each_with_index.map {|v,i| "#{v}\t\t#{@names[i]}\n" } %>'),
  ensure  => present
}

What we have there are two custom functions: size() and bracket_expansion(); which we then use values that they provide inside a hack that leverages the inline_template() function to render content of the file utilising parallel access to two arrays -- one with IP addresses from your fact and one with host names that should follow.

The result is a follows:

matti@acrux ~ $ cat | puppet apply
$fact = '1.1.1.1,2.2.2.2,3.3.3.3'

$servers = split($::fact, ',')
$count   = size($servers)

$names = bracket_expansion("host[01-${count}].address")

file { '/tmp/test.txt':
  content => inline_template('<%= @servers.each_with_index.map {|v,i| "#{v}\t\t#{@names[i]}\n" } %>'),
  ensure  => present
}
notice: /Stage[main]//File[/tmp/test.txt]/ensure: created
notice: Finished catalog run in 0.07 seconds
matti@acrux ~ $ cat /tmp/test.txt 
1.1.1.1         host01.address
2.2.2.2         host02.address
3.3.3.3         host03.address
matti@acrux ~ $

Both size() and bracket_expansion() functions can be found here:

https://github.com/kwilczynski/puppet-functions/tree/master/lib/puppet/parser/functions/

I hope this helps a little :-)

这篇关于傀儡迭代字符串/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆