Ask Your Question

how to create resources from hash?

asked 2014-01-11 13:00:49 -0500

deric gravatar image

I'd like to create files from a Hash structure like this:

  isolation: 'cgroups'
  cgroups_hierarchy: '/sys/fs/cgroup'
  cgroups_root: '/cgroups'

For each of the key there should be a file and the key should be its content. Is it possible with basic puppet syntax or do I need custom function?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-01-11 19:55:27 -0500

brenix gravatar image

You could use hiera_hash() and create_resources to generate the files/content

The hiera data could look like this:

    content: 'Test1'
    content: 'Test2'

Then in a manifest you could do the following:

$files = hiera_hash('files')
create_resources ( file, $files )

Example output:

sudo puppet apply test.pp
Notice: Compiled catalog for ******  in environment production in 0.14 seconds
Notice: /Stage[main]/Main/File[/tmp/test1]/ensure: defined content as '{md5}e1b849f9631ffc1829b2e31402373e3c'
Notice: /Stage[main]/Main/File[/tmp/test2]/ensure: defined content as '{md5}c454552d52d55d3ef56408742887362b'
Notice: Finished catalog run in 0.40 seconds
edit flag offensive delete link more


Thanks, it ain't the shortest syntax but it works. Moreover a directory could be specified with default values `create_resources ( file, $files, { dir => '/etc/mydir })`

deric gravatar imagederic ( 2014-01-12 17:46:08 -0500 )edit

answered 2016-07-28 13:49:46 -0500

With puppet 4 you can use iteration to make this much nicer:

Consider your hiera data like this:

  isolation: 'cgroups'
  cgroups_hierarchy: '/sys/fs/cgroup'
  cgroups_root: '/cgroups'

Then you can write

hiera_hash('params').each |$key, $value| {
  file {
      content => "${value}\n"
      # other defaults

and gain much more control and a readable data structure in hiera.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-01-11 13:00:49 -0500

Seen: 8,028 times

Last updated: Jan 11 '14