Ask Your Question
0

How to use role custom fact in Puppet

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

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 :

:backends:
  - yaml
:yaml:
  :datadir: "/etc/puppetlabs/code/environments/%{::environment}/hieradata"
:hierarchy:
  - "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
1

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

lupin gravatar image

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

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/
             'loadbalancer'
         when /^foo/
             'balancer'
         when /^db/
             'database'
    end
  end
end

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

edit flag offensive delete link more

Comments

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 -0500 )edit
1

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 -0500 )edit

Couldn't specify a rule in a Puppet Group using Puppet Console on your puppet master? https://docs.puppet.com/pe/latest/console_classes_groups.html#adding-nodes-dynamically

Red Cricket gravatar imageRed Cricket ( 2016-09-18 15:51:59 -0500 )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 -0500 )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

Stats

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

Seen: 530 times

Last updated: Sep 18 '16