Иерархия и имена классов Puppet и Hiera

Переменная поиска calling_class в Hiera вызывает у меня головную боль.

Учитывая такую ​​​​конфигурацию hiera:

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

И role.pp в Puppet со следующим:

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

Кажется, что когда класс наследуется (или включается, происходит то же самое в любом случае), этот 'calling_class' в Hiera устанавливается на унаследованный класс, а не на класс, выполняющий наследование. Я что-то упустил, или Hiera так и должна работать? Я думал, что наследование класса установит scope.source.name в дочерний класс, а не в родительский.


person Sean Hagen    schedule 21.10.2014    source источник
comment
FWIW, я бы не стал полагаться на $calling_class для чего-либо, что не является просто забавой для глаз.   -  person Felix Frank    schedule 22.10.2014
comment
@FelixFrank Я надеялся использовать его, чтобы упростить разделение данных Hiera на иерархию на основе класса, из которого я их вызываю.   -  person Sean Hagen    schedule 22.10.2014


Ответы (1)


Вы также можете использовать это решение. Единственным недостатком является то, что максимальное количество ролей жестко запрограммировано. Это будет лучше с 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/кукольный/манифесты/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/имя_вашего_узла.yaml

---
roles:
 - webserver

classes:
# put node specific stuff here
person Thomas Lohner    schedule 12.02.2016