Fetch variable from yaml in puppet manifest

拈花ヽ惹草 提交于 2019-12-11 05:13:40

问题


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.

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.

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


回答1:


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:

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)

That will output:

Notice: Scope(Class[mymodule]): {foo => bar}
  • Or, using the loadyaml or loadjson stdlib functions:
$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. Or, you could use a Puppet 6 deferred function and read the data from a file on the agent node.

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



来源:https://stackoverflow.com/questions/55113213/fetch-variable-from-yaml-in-puppet-manifest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!