How can I split my hiera config by role?

▼魔方 西西 提交于 2019-12-03 14:26:56

To be able to use $Role in your hiera config, it needs to be supplied as a fact/variable, however there is a way to do this on the master instead of on the node. This is one of the things that External Node Classifiers can be used for.

Basically, you need to write a script that takes the node name and prints out yaml that includes the Role parameter's value. For example, you could have one yaml file that is just a map of node names to roles, and then the script does a lookup and prints the result (as a parameter in the linked schema). Here is an example.

There are also more robust ENC's out there, if you are interested in new tooling. For example, Foreman gives you a web interface for grouping hosts together into similar roles, setting parameters to inject into puppet runs, etc.

I've come up with a solution for this. Only disadvantage is that the max number of roles is hardcoded. This will be better with hiera 3 until then try this:

/etc/puppet/hiera.yaml

 ---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/hieradata
:hierarchy:
  - 'nodes/%{::clientcert}'
  - 'roles/%{::role_4}'
  - 'roles/%{::role_3}'
  - 'roles/%{::role_2}'
  - 'roles/%{::role_1}'
  - common

/etc/puppet/manifests/site.pp

# Get roles
$roles = hiera_array('roles', [])

# Declare Roles in vars (not needed in puppet 4)
$role_1 = $roles[0]
$role_2 = $roles[1]
$role_3 = $roles[2]
$role_4 = $roles[3]

# Include Classes
hiera_include('classes')

/etc/puppet/hieradata/roles/webserver.yaml

---
classes:
  - nginx

# put nginx config here

/etc/puppet/hieradata/nodes/your_node_name.yaml

---
roles:
 - webserver

classes:
# put node specific stuff here
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!