Hiera中的"calling_class"查找变量让我头疼。
给定这样的层次结构配置:
---
:backends: yaml
:yaml:
:datadir:
:hierarchy:
- "node/%{::clientcert}"
- "profile/%{calling_class}"
- "roles/%{calling_class}"
- common
:logger: console
和一个角色.pp在木偶与以下:
class role::base {
notify { "output scope 1":
message => inline_template("scope='<%= scope.source.name %>'"),
}
$profiles = hiera_array('role::profiles', [])
notify { "Including profiles: ${profiles}": }
# include $profiles
}
class role::app inherits role::base{
notify { "output scope 2":
message => inline_template("scope='<%= scope.source.name %>'"),
}
$profiles = hiera_array('role::profiles', [])
notify { "Including profiles: ${profiles}": }
}
和具有以下内容的roles/role::app.yaml
:
---
role::profiles:
- webserver
- application
我希望看到这样的东西:
Notice: Including profiles: webapp
Notice: scope='role::app'
Notice: Including profiles: webapp
Notice: scope='role::app'
Notice: Finished catalog run in 0.11 seconds
但这就是我得到的:
Notice: Including profiles:
Notice: scope='role::base'
Notice: Including profiles: webapp
Notice: scope='role::app'
Notice: Finished catalog run in 0.11 seconds
似乎当一个类被继承(或被包含,无论哪种方式都是一样的)时,Hiera中的"calling_class"被设置为继承的类,而不是执行继承的类。我是错过了什么,还是希拉应该这样工作?我认为继承一个类会将scope.source.name
设置为子类,而不是父类。
您也可以使用此解决方案。唯一的缺点是角色的最大数量是硬编码的。在那之前,用hiera 3会更好。试试这个:
/etc/木偶/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/ppuppet/manifest/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/ppuppet/hieradata/roles/webserver.yaml
---
classes:
- nginx
# put nginx config here
/etc/popup/hieradata/nodes/your_node_name.yaml
---
roles:
- webserver
classes:
# put node specific stuff here