Assigning a dynamic role to be used by hiera

534 views
Skip to first unread message

JeremyCampbell

unread,
Aug 21, 2013, 1:20:56 PM8/21/13
to puppet...@googlegroups.com

I need to define data for roles so I add the 'role' dynamic data source.

- "%{::environment}/%{::clientcert}"
- "%{::clientcert}"
- "%{::environment}
- "%{::role}"
- common


Our site.pp uses a hostname regex to classify nodes into roles e.g.

node /^ns\d+$/ {
  include role::nameserver
}


Can we add the $role variable to the role class and hiera will use it? e.g.

class role::nameserver {
  $role = 'nameserver'
  include base
  include bind
  include shorewall
}


Would this work? Is there a better pattern that doesn't involve having to define custom facts on the servers?

Chad Huneycutt

unread,
Aug 21, 2013, 3:58:21 PM8/21/13
to puppet...@googlegroups.com
Yes, that will work. We actually do something slightly ickier, so the
data all stays in hiera:

hiera hierarchy:
- nodes/%{hostname}
- ...
- roles/%{role}

nodes/foo.yaml:
...
role: desktop
classes:
- "roles::%{role}"

node default {
$role = hiera('role')
include hiera('classes')
}


- Chad
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To post to this group, send email to puppet...@googlegroups.com.
> Visit this group at http://groups.google.com/group/puppet-users.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Chad M. Huneycutt

Ellison Marks

unread,
Aug 21, 2013, 6:29:56 PM8/21/13
to puppet...@googlegroups.com
you might want to explicitly top-scope role, ie.

$::role = 'nameserver'

Not sure if that will matter in this case, but I don't think it can hurt.

As an aside, we set our role variable with facter.d on the nodes themselves, as we don't have hostnames that easily work out to roles.

JuanBrein

unread,
Mar 24, 2014, 5:54:54 PM3/24/14
to puppet...@googlegroups.com
Today looking for a solution to this I came up with a nice solution. I know this is old but I'm posting it just in case is useful for somebody:

on hiera.yaml:

:hierarchy:
  - 'host/%{::domain}/%{::hostname}'
  - 'domain/%{::domain}'
  - 'role/%{system_role}'
  - 'common'

on site.pp

node default {

  $system_role = get_system_role()

}

and get_system_role is a custom function:

require 'yaml'

module Puppet::Parser::Functions

  newfunction(:get_system_role, :type => :rvalue) do |args|

    hostname = lookupvar('fqdn')

    enc = %x[/etc/puppet/node.rb #{hostname}]

    yaml = YAML::load enc

    system_role = yaml['classes'].keys.first.scan(/::([a-z]*)/).flatten[0]


  end

end

This is asuming you use an ENC to assign classes like foreman / cobbler or pe

Cheers

Juan
Reply all
Reply to author
Forward
0 new messages