在属性和附加数据上使用keys.join()

发布于 2025-01-18 21:48:52 字数 498 浏览 3 评论 0原文

我有两组属性需要在 Chef 中组合,

Set 1
cert1.sec1.offdtr.dev
cert2.sec1.offdtr.dev

Set 2
cert3.sec2.ondrt.dev,
cert4.sec2.ondrt.dev

我可以在每个数据集上使用keys.join(',') 将它们连接在一起。

<% node['code']['block'][sec1].keys.join(','),node['code']['block'][sec2].keys.join(',') %>

但我需要第二次行动;将 */ 附加到每个值。输出应该类似于:

*/cert1.sec1.offdtr.dev,*/cert2.sec1.offdtr.dev,*/cert3.sec2.ondrt.dev,*/cert4.sec1.ondrt.dev

我如何在厨师模板中执行此操作?

I have two set's of attributes i need to combine in Chef

Set 1
cert1.sec1.offdtr.dev
cert2.sec1.offdtr.dev

Set 2
cert3.sec2.ondrt.dev,
cert4.sec2.ondrt.dev

I can use keys.join(',') on each data set to concat them together.

<% node['code']['block'][sec1].keys.join(','),node['code']['block'][sec2].keys.join(',') %>

But i need a second action ; append */ to each value. Output should be something like :

*/cert1.sec1.offdtr.dev,*/cert2.sec1.offdtr.dev,*/cert3.sec2.ondrt.dev,*/cert4.sec1.ondrt.dev

How can i do this in a chef template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2025-01-25 21:48:52

首先,在厨师模板中,我们需要使用&lt;%=(请注意=标志)以获取呈现属性/变量的值。与此同时,我们可以使用,*/加入密钥。

因此,如果您在模板(.erb)文件中放置下面的内容:

*/<%= node['code']['block']['sec1'].keys.join(',*/') %>,*/<%= node['code']['block']['sec2'].keys.join(',*/') %>

渲染时它将产生预期结果:

*/cert1.sec1.offdtr.dev,*/cert2.sec1.offdtr.dev,*/cert3.sec2.ondrt.dev,*/cert4.sec2.ondrt.dev

在食谱中,我们使用#{..} for变量的插值。例如,当在其他地方使用mycerts变量时,它将产生完全相同的结果。

mycerts = "*/#{node['cookbook1']['sec1'].keys.join(',*/')},*/#{node['cookbook1']['sec2'].keys.join(',*/')}

Firstly, in Chef template, we need to use <%= (note the = sign) to get the value of attributes/variables rendered. Along with this, we can use ,*/ to join the keys.

So if you put something like below in a template (.erb) file:

*/<%= node['code']['block']['sec1'].keys.join(',*/') %>,*/<%= node['code']['block']['sec2'].keys.join(',*/') %>

It will produce the expected result when rendered:

*/cert1.sec1.offdtr.dev,*/cert2.sec1.offdtr.dev,*/cert3.sec2.ondrt.dev,*/cert4.sec2.ondrt.dev

In a recipe we use #{..} for interpolation of variables. For e.g. when the mycerts variable defined below is used elsewhere, it will result in exactly the same result.

mycerts = "*/#{node['cookbook1']['sec1'].keys.join(',*/')},*/#{node['cookbook1']['sec2'].keys.join(',*/')}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文