0

I'm running puppet 4 and I would like to generate several config files from the same template with different configurations for each one.

for example :

# cat /tmp/a.conf 
test1

# cat /tmp/b.conf 
test2

And I need to put all those informations in hiera, so something like that I think :

test::clusters:
  - 'a.conf'
    text: 'test1'
  - 'b.conf'
    text: 'test2'

Thx

2

You need a defined type

define test::clusters (
  $text = undef
) {

  file { "/tmp/${title}":
    ensure  => $ensure,
    owner   => 'root',
    group   => 'root',
    content => template('my_file/example.erb'),
  }

}

in templates/test/clusters

<%= @text %>

Then you can define a test::clisters in a manifest like so:

::test::clusters { 'a.conf':
  text => 'test1'
}

Or if you still wish to use hiera, you can use create_resources

| improve this answer | |
  • ok thanks I have this running now : class test { test::conf {'a': text=> "test", } } define test::conf ( String[1] $text, String[1] $text2 = $title, ) { file { "/tmp/${title}.conf": ensure => file, owner => 'root', group => 'root', mode => '0644', content => template('test/test2.erb'), } } now I need to make it works with hiera – Skullone Nov 24 '16 at 20:00
  • If you could accept the answer by selecting the tick, it'd be much appreciated! – jaxxstorm Nov 24 '16 at 21:09
0

Ok I find how to make it works :

Here is my hiera data/common.yaml :

test::paramconf:
  'a':
    text: '1'
  'b':
    text: '2'

Here is my module configuration manifests/init.pp :

class test ($paramconf){
    create_resources(test::conf, $paramconf)
}

define test::conf (
  String[1] $text,
  String[1] $text2 = $title,
) {
  file { "/tmp/${title}.conf":
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    content => template('test/test2.erb'),
  }
}

The only thing I don't understand is why this is working :

test::paramconf:
  'a':
    text: '1'
  'b':
    text: '2'

And that doesn't work :

test::paramconf:
  - 'a':
    text: '1'
  - 'b':
    text: '2'
| improve this answer | |
  • 1
    The first one creates a hash, the second one is an array of hashes. You see the differences in how puppet's ruby interpreter reads these like so: first file: ruby -e "require 'yaml'; require 'pp'; require 'json'; hash = YAML.load_file('file.yml'); pp hash" {"test::paramconf"=>{"a"=>{"text"=>"1"}, "b"=>{"text"=>"2"}}} second file: ruby -e "require 'yaml'; require 'pp'; require 'json'; hash = YAML.load_file('file1.yml'); pp hash" {"test::paramconf"=>[{"a"=>nil, "text"=>"1"}, {"b"=>nil, "text"=>"2"}]} – jaxxstorm Nov 24 '16 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.