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

Skullone
  • 195
  • 1
  • 1
  • 11

2 Answers2

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

jaxxstorm
  • 606
  • 6
  • 10
  • 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'
Skullone
  • 195
  • 1
  • 1
  • 11
  • 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