简体   繁体   English

在puppet清单中从yaml获取变量

[英]Fetch variable from yaml in puppet manifest

I'm doing one project for puppet, however currently stuck in one logic. 我正在为木偶做一个项目,但目前只停留在一个逻辑中。

Thus, want to know can we fetch variable from .yaml, .json or plain text file in puppet manifest file. 因此,想知道我们可以从puppet清单文件中的.yaml,.json或纯文本文件中获取变量。

For example, My puppet manifest want to create user but the variable exist in the .yaml or any configuration file, hence need to fetch the varibale from the outside file. 例如,我的puppet清单想要创建用户,但变量存在于.yaml或任何配置文件中,因此需要从外部文件中获取varibale。 The puppet manifest also can do looping if it exist multiple users in .yaml file. 如果.yaml文件中存在多个用户,则puppet清单也可以进行循环。

I read about hiera but let say we are not using hiera is there any possible way. 我读了关于hiera,但是让我们说我们没有使用hiera是否有任何可行的方法。

There are a number of ways you can do this using a combination of built-in and stdlib functions, at least for YAML and JSON. 使用内置函数和stdlib函数的组合可以通过多种方式实现此目的,至少对于YAML和JSON。

  • Using the built-in file function and the parseyaml or parsejson stdlib functions: 使用内置文件函数和parseyamlparsejson stdlib函数:

Create a file at mymodule/files/myfile.yaml: 在mymodule / files / myfile.yaml创建一个文件:

▶ cat files/myfile.yaml 
---
foo: bar

Then in your manifests read it into a string and parse it: 然后在您的清单中将其读入字符串并解析它:

$myhash = parseyaml(file('mymodule/myfile.yaml'))
notice($myhash)

That will output: 这将输出:

Notice: Scope(Class[mymodule]): {foo => bar}
  • Or, using the loadyaml or loadjson stdlib functions: 或者,使用loadyamlloadjson stdlib函数:
$myhash = loadyaml('/etc/puppet/data/myfile.yaml')
notice($myhash)

The problem with that approach is that you need to know the path to file on the Puppet master. 这种方法的问题是你需要知道Puppet master上的文件路径。 Or, you could use a Puppet 6 deferred function and read the data from a file on the agent node. 或者,您可以使用Puppet 6 延迟函数并从代理节点上的文件中读取数据。

(Whether or not you should do this is another matter entirely - hint: answer is you almost certainly should be using Hiera - but that isn't the question you asked.) (你是否应该这样做完全是另一回事 - 提示:答案是你几乎肯定应该使用Hiera - 但这不是你问的问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM