简体   繁体   中英

How to add a custom or external fact for role in Puppet?

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

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

My hiera.yaml looks like :

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

site.pp

node xx01 {
include role::cassandra
}

node xx02 {
include role::mysql
}

node xx03 {
include role::cassandra
}

For example I should add role fact for node xx01 and xx03. So it would useless to add fact for every new node in the future. so I want the fact to be added for every new node.

So the best way is to add a code to add a the fact for role in the control repo. not in the modules.

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

I think it's useless to compare with hostname to add the fact for roles if the hostname reflects the role. so I could use the hierarchy with a hostname rather than role.

You can either do this with an external fact or a custom fact. I should also note that your hiera file is completely fine and will automatically pick up your role fact for data resolution once that fact is populated. I am also going to assume from that hiera file that you are using Puppet 4, Facter 3, and Hiera 3 (not the Puppet Data Provider with module data lookups etc.), since you are using syntax and conventions consistent with those.

Let us say you have roles app, db, and report.

For custom facts, you would want to write some code like the following in the lib/facter/role.rb directory of a compiled module:

Facter.add('role') do
  setcode do
    case Facter.value(:hostname)
    when /db/ then role = 'db'
    when /app/ then role = 'app'
    when /report/ then role = 'report'
    else role = default
    role
  end
end

This would be a simple example of how to do this.

You can also do this with an external fact placed in the lib/facts.d directory of a compiled module, like a role.yaml or role.sh file. The yaml would be good for static data, and the shell script would be an example of how to dynamically ascertain the role of the server without using ruby.

You can check additional documentation here: https://docs.puppet.com/facter/3.4/custom_facts.html

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