Access variables defined in ENC

puppet

#1

Hi,

i have a small Problem i wrote some code that works fine now i want to add the erb for it and reference variable defined in the yaml. But im unable to access the variables referenced in that yaml.

Dou you have any tips ?

Some examples
in my init.pp i call this resource

if $rrp_config {
   create_resources(rrp_config, $rrp_config)
}

This Resource then expect a few Variables:

define pcs::rrp_config (
$rrp_name      = $title,
$ip            = undef,
$rrp_host      = undef,
$rrp_interface = undef,
) {
host { "${rrp_name}_name":
 ensure => present,
 name   => $rrp_name,
 ip     => $ip
}
if $rrp_host == $::hostname {
 file { "/etc/sysconfig/network-scripts/ifcfg-${rrp_interface}-${rrp_host}":
   ensure  => 'file',
   path    => "/etc/sysconfig/network-scripts/ifcfg-${rrp_interface}",
   owner   => 'root',
   group   => 'root',
   mode    => '0644',
   content => template('pcs/ifcfg.erb'),
  }
} else {
 notify {"rrp_name = ${rrp_name} and hostname = ${::hostname} and inteface = ${rrp_interface}":}
 }
}

In the Output of the Foreman yaml it looks like

classes:
  pcs:
    pcssudoer: false
    pcssudoextrarules: false
    requiretty: true
    rrp_config:
      foo-ring1:
        ip: 1.2.3.4
        rrp_interface: eth1
        rrp_host: foo

im my erb i would look to access the variable ip now how can i get there ?
And as plus how can i ensure that the ip only gets set on the host that matches its hostname with the variable rrp_host ?

Any Help appreciated :slight_smile:


#2

Can you paste the full init.pp? Also note that create_resources is deprecated in favor of using loops. I’d expect it to be something like this:

class pcs(
  Hash[String, Hash[String, Any]] $rrp_config = {},
) {
  $rrp_config.each |$rrp, $parameters| {
    pcs::rrp_config { $rrp:
      * => $parameters,
    }
  }
}

I’d also add types to the resource to ensure they’re correct. For example, String can be used to enforced some value. The catalog will fail to compile giving you more correct output where it went wrong.

Instead of $::hostname the more modern way is $facts['hostname'].


#3

Hi,

i should add that we are still (shame on us) on puppet 3.8 due to the satellite 6 and are now migration to puppet 5(but we still need this running on puppet 3.8) . The “full” init looks like

init.pp

class pcs (
 $pcssudoer         = '',
 $pcssudoextrarules = '',
 $requiretty        = true,
 $rrp_config    = undef,
 ) {
 tag 'pcs'
 ...
if $rrp_config {
create_resources(rrp_config, $rrp_config)
}

and the full content of the rrp_config.pp is

# This a Resource over which init.pp loops 
define pcs::rrp_config (
$rrp_name      = $title,
$ip            = undef,
$rrp_host      = undef,
$rrp_interface = undef,
) {

host { "${rrp_name}_name":
 ensure => present,
 name   => $rrp_name,
 ip     => $ip
}

if $rrp_host == $::hostname {
file { "/etc/sysconfig/network-scripts/ifcfg-${rrp_interface}-${rrp_host}":
  ensure  => 'file',
  path    => "/etc/sysconfig/network-scripts/ifcfg-${rrp_interface}",
  owner   => 'root',
  group   => 'root',
  mode    => '0644',
  content => template('pcs/ifcfg.erb'),
}
} else {
notify {"rrp_name = ${rrp_name} and hostname = ${::hostname} and inteface = ${rrp_interface}":}
}

}

#4

Satellite 6.3 does support both Puppet 3 and Puppet 5 but that’s the last one. 6.4 and newer have dropped Puppet 3 and you need to migrate on 6.3.

I suspect this is the issue. It should be create_resources('pcs::rrp_config', $rrp_config).

I would also advise on removing the = undef parts in the define’s parameters since they’re not really optional, but that shouldn’t affect your code.


#5

But how can i access the hash inside of rrp config

See:

BOOTPROTO="none"
IPADDR="<%- @rrp_config['foo-ring1']['ip'] %>"
NETMASK="255.255.255.0"
DEVICE=<% @rrp_interface %>
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
DEFROUTE=no


<% @rrp_config.each_pair do |k,v| %>
key: <%= k %> Value <%= v %>
<% end %>

I want to put the value of ip in the field if possible i also want to check if $rrp_config::host matches the hostname.

So far i got the following output:

BOOTPROTO="none"
IPADDR=""
NETMASK="255.255.255.0"
DEVICE=
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
DEFROUTE=no

key: foo-ring1 Value {"ip"=>"1.2.3.4", "rrp_interface"=>"eth1", "rrp_host"=>"foo"}