含糊-如何制定特定于主机平台的资源调配步骤



我们有一个多样化的开发团队,一个在Windows上,另一个在Ubuntu上,还有一个在OSX上。作为一个窗口男孩,我设置了流浪汉设置脚本的第一个版本,效果非常好;)

然而,当它在Ubuntu主机上运行时,第一次进入调用bash脚本的提供步骤时,由于权限的原因,它会失败。

在windows上,这并不重要,因为samba共享自动拥有足够的权限来运行bash脚本(它位于项目层次结构中,因此存在于虚拟机上的/farrant共享中),但对于ubuntu,我需要在调用它之前在提供脚本中设置该文件的权限。

这不是问题所在,老实说,我怀疑即使有了额外的"chmod"步骤,它在windows下仍然可以正常工作,但是,在流浪文件中有没有办法将某些设置步骤标记为"仅windows"、"仅Linux"或"仅Mac"?

即在pseduo代码中,类似于。

.
.
if (host == windows) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_windows.sh"
else if (host == linux) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_linux.sh"
else if (host == osx) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_osx.sh"
end if
.
.

提前谢谢。

请注意,Vagrant::Util::Platform类中的Vagrant本身已经在BernardoSilva的答案中实现了平台检查逻辑的更高级版本。

因此,在Vagrantfile中,您可以简单地使用以下内容:

if Vagrant::Util::Platform.windows? then
    myHomeDir = ENV["USERPROFILE"]
else
    myHomeDir = "~"
end

在Vagrantfile中查找当前操作系统

将此添加到您的流浪者文件:

module OS
    def OS.windows?
        (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
    end
    def OS.mac?
        (/darwin/ =~ RUBY_PLATFORM) != nil
    end
    def OS.unix?
        !OS.windows?
    end
    def OS.linux?
        OS.unix? and not OS.mac?
    end
end

然后你可以随心所欲地使用它。

if OS.windows? [then]
    code...
end

编辑:是否缺少?在if条件下。

用于测试的示例:

is_windows_host = "#{OS.windows?}"
puts "is_windows_host: #{OS.windows?}"
if OS.windows?
    puts "Vagrant launched from windows."
elsif OS.mac?
    puts "Vagrant launched from mac."
elsif OS.unix?
    puts "Vagrant launched from unix."
elsif OS.linux?
    puts "Vagrant launched from linux."
else
    puts "Vagrant launched from unknown platform."
end

执行:

# Ran provision to call Vagrantfile.
$ vagrant provision
is_windows_host: false
Vagrant launched from mac.

下面是一个使用Vagrant utils检查mac和windows的版本:

    if Vagrant::Util::Platform.windows?
        # is windows
    elsif Vagrant::Util::Platform.darwin?
        # is mac
    else
        # is linux or some other OS
    end

根据我的说法,当我读到最初的问题时,不是如何找出它在哪个操作系统上自行运行,而是要配置的虚拟机有哪个操作系统。这就是为什么您希望根据新虚拟机的不同操作系统运行不同的配置脚本,例如:"/warrant/provisioning/only_run_this_on_${OS_of_new_VM}.sh"。

不幸的是,Vagrant还没有这个功能,所以这是我的解决方案:我在我的流浪文件上定义我的VM:

   cluster = {
  "control.ansible.RHEL76" => { :ip => "192.168.1.31", :type => 0, :cpus => 1, :mem => 1024, :box_image => "centos/7" },
  "app01.ansible.RHEL76"   => { :ip => "192.168.1.32", :type => 1, :cpus => 1, :mem => 1024, :box_image => "centos/7" },
  "app02.ansible.RHEL76"   => { :ip => "192.168.1.33", :type => 1, :cpus => 1, :mem => 1024, :box_image => "centos/7" },
  "winserver"          => { :ip => "192.168.1.34", :type => 2, :cpus => 1, :mem => 1024, :box_image => "mwrock/Windows2016" },
}

然后,我的代码中的这些条件可以根据VM的操作系统提供不同的方式:

if "#{info[:box_image]}" == "mwrock/Windows2016" then
  puts "is_windows_host: #{info[:box_image]}"
  config.vm.provision : shell, inline => "/vagrant/provisioning/only_run_this_on_windows.psl"
end
if "#{info[:box_image]}" == "centos/7" then
  puts "is_linux_host: #{info[:box_image]}"
  config.vm.provision : shell, inline => "/vagrant/provisioning/only_run_this_on_linux.sh"
end

顺便说一句,只有ansible控制器应该安装ansible,因为在现实生活中(是的,在办公室),我不使用流浪者,而是一个ansible的控制器,我也想在实验室里使用它(在我的Windows 10桌面和Ubuntu笔记本电脑上都有OK Virtual Box)。我使用一个条件来测试"type=0"(我将其用于可解析的"控制器")。只有ansible控制器启动ansible_local,为VM集群提供ansible。

   if info[:type]  == 0 then
     cfg.vm.provision "shell", inline: "if [ `which ansible` ] ; then echo "ansible available"; else sudo  yum -y update; sudo yum -y install epel-release; sudo yum -y install ansible; fi"
     cfg.vm.provision "ansible_local" do |ansible|
        ansible.extra_vars = { ansible_ssh_user: 'vagrant' }
        ansible.inventory_path = "./production"
        ansible.playbook = "rhelhosts.yml"
        ansible.limit = "local"
     end # ansible_local

这在流浪者提供期间显示:

PS D:\Documents\流浪者\top>流浪者提供控制.top.RHEL76is_linux_host:centros/7is_linux_host:centros/7is_linux_host:centros/7is_windows_host:mwrock/Windows2016

这在流浪者提供期间显示:

PS D:Documentsvagrantansible> vagrant provision control.ansible.RHEL76

is_linux_host: centos/7

is_linux_host: centos/7

is_linux_host: centos/7

is_windows_host: mwrock/Windows2016

--- many more lines, not relevant ---

体验和部署多机/多操作系统实验室的乐趣!

最新更新