Ask Your Question

How to use role custom fact in Puppet

asked 2016-09-17 04:07:24 -0600

stranger90 gravatar image

I'm using a control-repo for my puppet master profiles/roles.

I'm using Hiera and I would like to add role as hierarchy

my hiera.yaml looks like :

  - yaml
  :datadir: "/etc/puppetlabs/code/environments/%{::environment}/hieradata"
  - "nodes/%{::trusted.certname}"
  - "roles/%{::role}"
  - "common"

the puppet agent doesn't seem to have the role fact. so I added a role fact in /etc/puppetlabs/facter/facts.d

But I don't want to do this for every node. I want this to be automatic.

do you have any ideas guys ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-09-18 00:58:27 -0600

lupin gravatar image

updated 2016-09-18 15:24:02 -0600

You can create a module that contain a custom facts or add a custom facts to an existing module ( it will automatically sync/distributed to all Puppet nodes). i.e

cat role.rb 
Facter.add('role') do
  setcode do
  # Retrieve hostname and assign role base on it        
  hname = Facter.value(:hostname)
    case hname
        when /^ha/
         when /^foo/
         when /^db/

An untested example. You then put something similar in your module_name/lib/facter/role.rb.

edit flag offensive delete link more


Is there any way to use this ruby code in the control-repo. and not in a new module. I think it's useless to add a new module. if the hostname reflects the role. so I could use the hierarchy with a hostname and not with the node role.

stranger90 gravatar imagestranger90 ( 2016-09-18 07:15:56 -0600 )edit

You can add it inside any of your existing module be it role or profile. The above is just a trivial example basing on host name but it's not limited to that - you can can create facts base on ip, presence of file. process ..etc.

lupin gravatar imagelupin ( 2016-09-18 15:25:18 -0600 )edit

Couldn't specify a rule in a Puppet Group using Puppet Console on your puppet master?

Red Cricket gravatar imageRed Cricket ( 2016-09-18 15:51:59 -0600 )edit

The 'role' is really just a construct ontop of an underlying puppet module, you can just put the facter fact in the 'role' module as long as you have pluginsync enabled

DarylW gravatar imageDarylW ( 2016-09-18 21:41:19 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2016-09-17 04:07:24 -0600

Seen: 1,277 times

Last updated: Sep 18 '16