简体   繁体   中英

iterating over yaml hash in puppet

I am creating a small freeradius module in puppet. I have some problems creating the client.conf file, which should look something like this:

client switch01 {
    ipaddr = 10.10.10.50
    secret = secret
    shortname = switch01
}
client switch02 {
    ipaddr = 10.10.10.51
    secret = secret
    shortname = switch02
}

So I am trying to create this with a template file clients.erb . These are the files:

yaml file:

test_freeradius::clients:
    'switch01':
        ip: '10.10.10.50'
        secret: 'secret'
        shortname: 'switch01' 
    'switch02::
        ip: '10.10.10.51'
        secret: 'secret'
        shortname: 'switch02'

define:

define test_freeradius::clients (

  $ip,
  $secret,
  $shortname,

) {

  include test_freeradius::service

  if ! defined(File['/etc/freeradius/clients.conf']){
    file { '/etc/freeradius/clients.conf' :
      ensure  => 'file',
      owner   => 'root',
      group   => 'freerad',
      mode    => '0640',
      content => template('test_freeradius/clients.erb'),
      require => Class['test_freeradius::install'],
      notify  => Service['freeradius'],
    }
  }
  $data = hiera_hash('test_freeradius::clients')
}

init.pp:

class test_freeradius {

  create_resources(test_freeradius::clients, $data)
}

I could create one client like this:

client <%= @shortname %> {
    ipaddr = <%= @ip %>
    secret = <%= @secret %>
    shortname = <%= @shortname %>
}

but I couldn't achieve creating multiple clients! at the end I need to create 10 clients.

This didn't work:

<% test_freeradius::clients.each do |key,value| -%>
  client <%= key %> {
      ipadd = <%= value['ip'] %>
      asecret = <%= value['secret'] %>
      shortname = <%= value['shortname'] %>
  }
  <% end -%> 

My question is how could I iterate over the hash to create the client.conf file?

Thank you very much!

A somewhat related problem is that your $data is undefined in your init.pp , which causes problems when used in conjunction with create_resources . Also, you should consider just creating a parameter for that hash in your defined resource type and then passing that as an attribute parameter. Then you can use the hash in your defined resource type. At the moment, you are attempting to pass an undefined hash and then look up the same hash in the defined resource type, which is also redundant. I recommend either passing it or looking it up and not doing both.

For example:

# clients.pp
define test_freeradius::clients(
  $client_settings = {}
)
...
}

# init.pp
...
test_freeradius::clients { 'create client confs':
  $client_settings => hiera_hash('test_freeradius::clients')
}

would optimize and clean things up a bit. Also note your hiera_hash would do a hash merge if that key is found in multiple levels, so be aware of that.

As for your use of the test_freeradius inside the ERB, you have a couple problems there. The first is that the variable is data and not test_freeradius::clients . The next is that ERB is used by Puppet with bindings to Puppet variables in the same scope that you can use as instance variables with @ , so in this case @data . Combining the two fixes, we arrive at:

<% @data.each do |key, value| -%>
client <%= key %> {
    ipadd = <%= value['ip'] %>
    asecret = <%= value['secret'] %>
    shortname = <%= value['shortname'] %>
}
<% end -%>

Note a very relevant example in the documentation: https://docs.puppet.com/puppet/4.10/lang_template_erb.html#iteration

and further recommend checking out other sections in that document for using Puppet variables in ERB within Puppet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM