通过PuPHPet使用不同配置的多个Vagrant VM

时间:2016-06-09 11:26:31

标签: vagrant puppet vagrantfile puphpet

我觉得我错过了一些基本的东西。如何使用PuPHPet在一个vagrantfile中定义两台机器,包括Ubuntu 14.04,但是一台安装了mysql,一台使用elasticsearch?我看到如何定义多台机器,但每台机器的配置似乎相同???

1 个答案:

答案 0 :(得分:6)

这不是通过PuPHPet GUI本机支持的,但是......您可以轻松实现它。

注意:我将通过PuPHPet的github跟踪器为以下内容提供支持。请不要为此发布门票。但是,我使用类似于一些自由职业客户的东西,它运作良好。

PuPHPet支持多个配置文件,每个配置文件都扩展并覆盖前一个。

看看puphpet/puppet/hiera.yaml

---
:backends: yaml
:yaml:
    :datadir: '/'
:hierarchy:
    - vagrant/puphpet/config-custom
    - vagrant/puphpet/config-%{::provisioner_type}
    - vagrant/puphpet/config
:merge_behavior: deeper
:logger: console

Hiera向后加载配置文件,因此它config.yaml - > config-%{::provisioner_type}.yaml - > config-custom.yaml

您可以在config-custom下面添加一个新行:- vagrant/puphpet/config-%{::server_type}

这会将此文件中的任何自定义设置注入Puppet环境。但是,您仍然需要让V​​agrant知道它,因为Vagrant从Vagrantfile读取执行命令。您可能还需要为每种不同的服务器类型执行一些自定义工作。请注意,此上下文中的server_type可能意味着代理,app,db,jenkins等。

在PuPHPet根目录中,存在Vagrantfilepuphpet目录的位置,创建以服务器类型命名的新目录。

例如,创建一个新的db目录,删除Vagrantfile并在此新目录中创建一个新目录,并在config-db.yaml目录中创建一个puphpet

$ tree -L 2
.
├── db
│   └── Vagrantfile
└── puphpet
    ├── config-custom.yaml
    ├── config-custom.yaml.dist
    ├── config-db.yaml
    ├── config.yaml
    ├── files
    ├── puppet
    ├── ruby
    ├── shell
    └── vagrant

您的db/Vagrantfile内容可能如下所示:

# -*- mode: ruby -*-

dir = File.dirname(File.expand_path(__FILE__))
dir = "#{dir}/.."

server_type = 'proxy'

VAGRANT_DOTFILE_PATH = "#{dir}/.vagrant";
currpath = ENV['VAGRANT_DOTFILE_PATH'];
if(currpath.nil?)
    currpath = '.vagrant';
end
if(currpath != VAGRANT_DOTFILE_PATH)
    ENV['VAGRANT_DOTFILE_PATH'] = VAGRANT_DOTFILE_PATH
    args = ARGV.join(' ');
    system "vagrant #{args}"

    if Dir.exists?('Directory Name')
        FileUtils.rm_r(currpath)
    end

    abort "Finished"
end

require 'yaml'
require "#{dir}/puphpet/ruby/deep_merge.rb"
require "#{dir}/puphpet/ruby/to_bool.rb"
require "#{dir}/puphpet/ruby/puppet.rb"

configValues = YAML.load_file("#{dir}/puphpet/config.yaml")

provider = ENV['VAGRANT_DEFAULT_PROVIDER'] ? ENV['VAGRANT_DEFAULT_PROVIDER'] : 'local'
if File.file?("#{dir}/puphpet/config-#{provider}.yaml")
  custom = YAML.load_file("#{dir}/puphpet/config-#{provider}.yaml")
  configValues.deep_merge!(custom)
end

if File.file?("#{dir}/puphpet/config-#{server_type}.yaml")
  custom = YAML.load_file("#{dir}/puphpet/config-#{server_type}.yaml")
  configValues.deep_merge!(custom)
end

if File.file?("#{dir}/puphpet/config-custom.yaml")
  custom = YAML.load_file("#{dir}/puphpet/config-custom.yaml")
  configValues.deep_merge!(custom)
end

data = configValues['vagrantfile']

Vagrant.require_version '>= 1.8.1'

Vagrant.configure('2') do |config|
  eval File.read("#{dir}/puphpet/vagrant/Vagrantfile-#{data['target']}")
end

我们现在告诉Vagrant有关#{dir}/puphpet/config-#{server_type}.yaml的新配置文件 - > #{dir}/puphpet/config-db.yaml - > ..

根据需要为尽可能多的服务器类型创建尽可能多的服务器类型。

如果您希望Puppet知道server_type值,您可以将其作为一个参与者传递。使用以下命令更新vagrant/Vagrantfile-*文件:

puppet.facter = {
    'server_name'      => "#{machine['id']}",
    'server_type'      => "#{server_type}",
    'fqdn'             => "#{machine_id.vm.hostname}",
    'ssh_username'     => "#{ssh_username}",
    'provisioner_type' => 'local',
}

它将在您的Puppet清单中显示为$::server_type

那么实际上发生了什么?

好吧,在您的config.yaml(即主配置文件)中,您可以设置服务器场的基础知识。每个服务器可能需要git等软件包,或者具有防火墙打开的特定端口。您可以在那里添加这些值。

将服务器特定的应用程序设置为不安装在config.yaml

mongodb:
    install: '0'
    settings:
        bind_ip: 127.0.0.1
        port: '27017'
    globals:
        version: 2.6.0
    databases: {  }

config-db.yaml中,您可以根据需要设置特定于数据库的值:

mongodb:
    install: '1'
    settings:
        ensure: true
        package_name: mongodb-org-server
        bind_ip: "%{::ipaddress_tun0}"
        port: '27017'
        config: /etc/mongod.conf
        replset: rsmain
    globals:
        bind_ip: "%{::ipaddress_tun0}"
        version: 3.2.6
        service_name: mongodb
    databases:
         your_db:
             name: db_name
             user: db_user
             password: 'awesome_password'

现在,您的数据库服务器将获取这些更改并根据需要应用。您的应用服务器(或jenkins / proxy / etc)仍会看到mongodb不需要从config.yaml安装。你可以用这种方式创建相当复杂的农场,通过一些简单的YAML文件。

最后要注意的是,由于这是一个多服务器环境,因此当您运行大多数命令时,Vagrant需要知道要处理的服务器。您可以使用要在每个config-#{server_type}.yaml文件中设置的计算机ID来执行此操作。每个文件可以为其类型定义多台计算机。然后只需将机器ID附加到vagrant命令:

vagrant up db1vagrant destroy -f db1

我希望这可以回答你所有的问题,或者至少可以帮助你完成你想要完成的任务!

相关问题