1 Vagrantfile pour une multitude de VM

vagrantfile vagrant pour multiple machine
vm

Vagrantfile pour multiple machine

Je te partage ci-dessous mon Vagrantfile qui contient une liste de vm, pour provisionner de multiple machine :

Vagrant.configure(2) do |config|

  etcHosts = ""
  ssh_config = "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
  debian = "debian/stretch64"
  centos = "centos/7"


	

	# la liste des VM
	NODES = [
    { :hostname => "serveur-prometheus", :ip => "172.16.16.31", :cpus => 1, :mem => 1024, :os => centos },
    { :hostname => "serveur-debian", :ip => "172.16.16.30", :cpus => 1, :mem => 1024, :os => centos },
    { :hostname => "serveur-test", :ip => "172.16.16.40", :cpus => 1, :mem => 1024, :os => debian },
    { :hostname => "main-bdd", :ip => "172.16.16.45", :cpus => 1, :mem => 1024, :os => debian },
    { :hostname => "gitlab-serveur", :ip => "172.16.16.50", :cpus => 1, :mem => 3072, :os => debian }


	]

	# define /etc/hosts for all servers
  NODES.each do |node|
    etcHosts += "echo '" + node[:ip] + "   " + node[:hostname] + "' >> /etc/hosts" + "\n"
	
  end #end NODES

	# run installation
  NODES.each do |node|
    config.vm.define node[:hostname] do |cfg|
			cfg.vm.box = node[:os]
      cfg.vm.hostname = node[:hostname]
      cfg.vm.network "private_network", ip: node[:ip]
      cfg.vbguest.auto_update = false
      cfg.vm.provider "virtualbox" do |v|
				v.customize [ "modifyvm", :id, "--cpus", node[:cpus] ]
        v.customize [ "modifyvm", :id, "--memory", node[:mem] ]
        v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
        v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
        v.customize ["modifyvm", :id, "--name", node[:hostname] ]
      end #end provider

			#for all
      cfg.vm.provision :shell, :inline => etcHosts
      cfg.vm.provision "shell", inline: <<-SHELL
        sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config    
        systemctl restart sshd.service || /etc/rc.d/rc.sshd restart
      SHELL
 
    end # end config
  end # end nodes
end

La partie suivante te permet de modifier la liste de VM :

# la liste des VM
	NODES = [
    { :hostname => "serveur-prometheus", :ip => "172.16.16.31", :cpus => 1, :mem => 1024, :os => centos },
    { :hostname => "serveur-debian", :ip => "172.16.16.30", :cpus => 1, :mem => 1024, :os => centos },
    { :hostname => "serveur-test", :ip => "172.16.16.40", :cpus => 1, :mem => 1024, :os => debian },
    { :hostname => "main-bdd", :ip => "172.16.16.45", :cpus => 1, :mem => 1024, :os => debian },
    { :hostname => "gitlab-serveur", :ip => "172.16.16.50", :cpus => 1, :mem => 3072, :os => debian }


	]

Cette liste contient les VM et leurs spécificités, chaque ligne correspond à une VM.

Tu peux en ajouter, en modifier, supprimer, etc… Ensuite la liste est utilisée par Vagrant pour créer les VM.

Ce Vagrantfile te permet de créer de multiple machine facilement et rapidement.

Si tu veux en savoir plus sur Vagrant n’hésite pas à aller sur la documentation officielle.

Laisser un commentaire