从人偶清单中的Yaml获取变量 [英] Fetch variable from yaml in puppet manifest

查看:118
本文介绍了从人偶清单中的Yaml获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个人偶做一个项目,但是目前陷入一种逻辑.

I'm doing one project for puppet, however currently stuck in one logic.

因此,想知道我们能否从人偶清单文件中的.yaml,.json或纯文本文件中获取变量.

Thus, want to know can we fetch variable from .yaml, .json or plain text file in puppet manifest file.

例如, 我的木偶清单希望创建用户,但该变量存在于.yaml或任何配置文件中,因此需要从外部文件中获取变量.如果文件清单在.yaml文件中存在多个用户,它也可以循环播放.

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. The puppet manifest also can do looping if it exist multiple users in .yaml file.

我读到了有关hiera的文章,但可以说,我们没有使用hiera的任何可能方式.

I read about hiera but let say we are not using hiera is there any possible way.

推荐答案

使用内置和

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.

  • Using the built-in file function and the parseyaml or parsejson stdlib functions:

在mymodule/files/myfile.yaml中创建文件:

Create a file at 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)

这将输出:

Notice: Scope(Class[mymodule]): {foo => bar}

  • 或者,使用loadyamlloadjson stdlib函数:
    • Or, using the loadyaml or loadjson stdlib functions:
    • $myhash = loadyaml('/etc/puppet/data/myfile.yaml')
      notice($myhash)
      

      该方法的问题在于,您需要知道Puppet主文件上的文件路径.或者,您可以使用Puppet 6 推迟功能并从代理节点上的文件中读取数据.

      The problem with that approach is that you need to know the path to file on the Puppet master. Or, you could use a Puppet 6 deferred function and read the data from a file on the agent node.

      (您是否应该这样做完全是另一回事-提示:答案是您几乎肯定应该使用Hiera,但这不是您要问的问题.)

      (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.)

      这篇关于从人偶清单中的Yaml获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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