我有几个主机上有Docker容器,所以我定义了一个名为apps::docker
的类,它在主机上安装了Docker:
class apps::docker {
include apps::docker::repository, apps::docker::install,
apps::docker::service
Class["Apps::Docker::Repository"] ->
Class["Apps::Docker::Install"] ->
Class["Apps::Docker::Service"]
Package["Docker"] ~> Service["Docker"]
}
class apps::docker::repository {
apt::source { 'docker':
location => "http://get.docker.io/ubuntu",
key => "A88D21E9",
release => "docker",
repos => "main",
include_src => false
}
}
class apps::docker::install {
package { 'docker':
name => "lxc-docker",
ensure => present
}
class apps::docker::service {
service { 'docker':
provider => 'upstart',
enable => true,
ensure => running,
hasrestart => true,
hasstatus => true
}
}
实际上很简单。
问题在于,当我尝试定义依赖于此类的类时,执行不按顺序发生,命令失败。例如,我的班级profiles::shiningstar::containers
取决于apps::docker
中定义的profiles::shiningstar
:
class profiles::shiningstar {
include apps::docker
include profiles::shiningstar::containers
Class["Apps::Docker"] -> Class["Profiles::Shiningstar::Containers"]
}
不幸的是,这不起作用如下所示:
Error: /Stage[main]/Profiles::Shiningstar::Containers::Puppetmaster::Pull/Docker::Image[rfkrocktk/puppetmaster:1.0.5]/Exec[docker pull rfkrocktk/puppetmaster:1.0.5]: Could not evaluate: Could not find command 'docker'
... (similar errors)
Notice: /Stage[main]/Apps::Docker::Repository/Apt::Source[docker]/Apt::Key[Add key: A88D21E9 from Apt::Source docker]/Apt_key[Add key: A88D21E9 from Apt::Source docker]/ensure: created
它完全不按顺序执行。我的配置有什么问题,如何指定在apps::docker
之前必须满足profiles::shiningstar::containers
的所有依赖关系?
答案 0 :(得分:3)
您可能希望contain内部类而不是仅仅包含它们。
class apps::docker {
contain apps::docker::repository
contain apps::docker::install
contain apps::docker::service
Class['apps::docker::repository']
->
Class['apps::docker::install']
~>
Class['apps::docker::service']
}
请注意,将::install
类作为一个整体notify
整个::service
类,这是有意义的(至少在您的情况下)。这使您可以更灵活地重构这些类的相应实现。
在第一次评论后编辑 - 请勿尝试在contain
语句之间添加链接箭头。
答案 1 :(得分:1)
您应该使用锚点,这将确保构建所有依赖项
class apps::docker {
include apps::docker::repository, apps::docker::install,
apps::docker::service
Class["Apps::Docker::Repository"] ->
Class["Apps::Docker::Install"] ->
Class["Apps::Docker::Service"] ->
anchor{"apps::docker":}
Package["Docker"] ~> Service["Docker"]
}