如何在 Puppet 中迭代数组 [英] How to iterate over an array in Puppet

查看:59
本文介绍了如何在 Puppet 中迭代数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个存储为 Facter 事实的数组,并为数组的每个元素创建一个新的系统用户和一个目录,最后对 AWS 进行 API 调用.

I would like to iterate over an array that is stored as a Facter fact, and for each element of the array create a new system user and a directory, and finally make API calls to AWS.

事实示例:my_env =>[shared1,shared2,shared3]

如何在 Puppet 中迭代数组?

How can I iterate over an array in Puppet?

推荐答案

这可能会奏效,这取决于你在做什么

This might work, depending on what you are doing

# Assuming fact my_env => [ shared1, shared2, shared3 ]

define my_resource {
  file { "/var/tmp/$name":
    ensure => directory,
    mode   => '0600',
  }
  user { $name:
    ensure => present,
  }
}
my_resource { $my_env: }

如果您的要求很简单,它会起作用,否则,Puppet 会使这很难做到.Puppet 开发人员基于对声明式语言如何工作的误解而对迭代抱有非理性的偏见.

It will work if your requirements are simple, if not, Puppet makes this very hard to do. The Puppet developers have irrational prejudices against iteration based on a misunderstanding about how declarative languages work.

如果这种资源不适合您,也许您可​​以更好地了解要从数组中设置哪些资源属性?

If this kind of resource doesn't work for you, perhaps you could give a better idea of which resource properties you are trying to set from your array?

Puppet 4 终于修复了这个可悲的缺陷.此处记录了当前的事态.正如文档所说,您会在许多旧代码中找到上述解决方案的示例.

With Puppet 4, this lamentable flaw was finally fixed. Current state of affairs documented here. As the documentation says, you'll find examples of the above solution in a lot of old code.

这篇关于如何在 Puppet 中迭代数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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