木偶& Hiera层级&班级名称

时间:2014-10-21 19:57:15

标签: inheritance puppet hiera

' calling_class' Hiera中的查找变量给我带来了麻烦。

给出如此的hiera配置:

---
:backends: yaml
:yaml:
  :datadir: 
:hierarchy:
  - "node/%{::clientcert}"
  - "profile/%{calling_class}"
  - "roles/%{calling_class}"
  - common
:logger: console

Puppet中的role.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

似乎当一个类被继承(或包含,以相同的方式发生)时,“call_class&#39;在Hiera中设置为继承类,而不是继承的类。我错过了什么,或者这是Hiera的工作方式?我认为继承一个类会将scope.source.name设置为子类,而不是父类。

1 个答案:

答案 0 :(得分:0)

您也可以使用此解决方案。唯一的缺点是角色的最大数量是硬编码的。这将是更好的hiera 3,然后尝试这个:

/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