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

Mat*_*her 4 puppet vagrant puphpet vagrantfile

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

Jua*_*nio 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
Run Code Online (Sandbox Code Playgroud)

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根目录中,存在where Vagrantfilepuphpetdirectory,创建以服务器类型命名的新目录.

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

$ tree -L 2
.
??? db
?   ??? Vagrantfile
??? puphpet
    ??? config-custom.yaml
    ??? config-custom.yaml.dist
    ??? config-db.yaml
    ??? config.yaml
    ??? files
    ??? puppet
    ??? ruby
    ??? shell
    ??? vagrant
Run Code Online (Sandbox Code Playgroud)

您的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
Run Code Online (Sandbox Code Playgroud)

我们现在告诉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',
}
Run Code Online (Sandbox Code Playgroud)

它将在你的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: {  }
Run Code Online (Sandbox Code Playgroud)

在你config-db.yaml,你愿意,你可以设置特定的DB-值:

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'
Run Code Online (Sandbox Code Playgroud)

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

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

vagrant up db1,vagrant destroy -f db1等等.

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