我试图从一个单一的Vagrantfile
提供两个虚拟机。我的目标是能够启动Web服务器和数据库服务器,应用一些常用配置,然后根据系统是否托管数据或网站来应用更具体的配置。很明显,如果我在做两个静态虚拟机,这不是问题,但最终系统数量可能是动态的,所以我想首先解决这个问题。下面是Vagrantfile
的要点:提供多个虚拟机与他们自己的木偶环境
Vagrant.config(2) do |config|
config.vm.provision :shell, inline: 'apt-get install -y -q puppet'
config.vm.box = 'ubuntu/xenial64'
config.vm.provision :puppet do |default_puppet|
puppet.manifests_path = 'puppet/manifests'
puppet.module_path = 'puppet/modules'
end
config.vm.define 'web' do |web|
config.vm.provision :puppet do |web_puppet|
web_puppet.environment_path = 'puppet/environments'
web_puppet.environment = 'web'
end
end
config.vm.define 'db' do |db|
config.vm.provision :puppet do |db_puppet|
db_puppet.environment_path = 'puppet/environments'
db_puppet.environment = 'db'
end
end
end
我有以下目录结构:
.
├── Vagrantfile
└── puppet
├── environments
│ ├── db
│ │ └── manifests
│ │ └── default.pp
│ └── web
│ └── manifests
│ └── default.pp
├── hiera.yaml
├── manifests
│ └── default.pp
└── modules
├── ntp
└── stdlib
我manifests/default.pp
很简单:
# Set up some default stuff that both servers should have
class { '::ntp':
servers => [
'0.ubuntu.pool.ntp.org',
'1.ubuntu.pool.ntp.org',
'2.ubuntu.pool.ntp.org',
'3.ubuntu.pool.ntp.org',
]
}
file { '/home/ubuntu/file0.txt':
ensure => file,
content => 'should be available on all systems'
}
而且/environments/{db,web}/manifests/default.pp
文件也相当简单,基本上就是这样:
# set up something specifically for the database server
file { '/home/ubuntu/file1.txt':
ensure => file,
content => 'this file should only be on the db server.'
}
文件名和内容已更改,因此在查看一切正常工作的目录时,这一点显而易见。
这几乎可以工作 - 当我更新manifests/default.pp
时,更改将应用于两者;当我更新environments/web/manifests/default.pp
web
服务器获取这些更改。但是,当我准备了db
环境,它得到两种环境:
==> web: Running provisioner: puppet...
==> web: Running Puppet with default.pp...
==> web: Info: Loading facts
==> web: Notice: Compiled catalog for web.vm in environment production in 0.64 seconds
==> web: Info: Applying configuration version '1481059856'
==> web: Notice: /Stage[main]/Main/File[/home/ubuntu/file0.txt]/ensure: defined content as '{md5}ba94d90e4b2e228b54c75d9110e3179c'
==> web: Notice: Finished catalog run in 0.07 seconds
==> web: Running provisioner: puppet...
==> web: Running Puppet with environment web...
==> web: Notice: Compiled catalog for web.vm in environment web in 0.12 seconds
==> web: Notice: Finished catalog run in 0.01 seconds
==> db: Running provisioner: puppet...
==> db: Running Puppet with default.pp...
==> db: Info: Loading facts
==> db: Notice: Compiled catalog for db.vm in environment production in 0.64 seconds
==> db: Info: Applying configuration version '1481059866'
==> db: Notice: /Stage[main]/Main/File[/home/ubuntu/file0.txt]/ensure: defined content as '{md5}ba94d90e4b2e228b54c75d9110e3179c'
==> db: Notice: Finished catalog run in 0.07 seconds
** shouldn't be doing this **
==> db: Running provisioner: puppet...
==> db: Running Puppet with environment web...
==> db: Notice: Compiled catalog for db.vm in environment web in 0.11 seconds
==> db: Notice: Finished catalog run in 0.01 seconds
** this is fine though **
==> db: Running provisioner: puppet...
==> db: Running Puppet with environment db...
==> db: Notice: Compiled catalog for db.vm in environment db in 0.12 seconds
==> db: Notice: Finished catalog run in 0.01 seconds
这发生在我运行vagrant provision
。现在,我还发现,如果我运行vagrant provision db
,则只应用db
环境......但我不应该能够在多台机器上设置每台计算机的环境吗?有没有另一种方法来做我想做的事情?