Vagrant.configure("2") do |config| # Define VMs with static private IP addresses, vcpu, memory and vagrant-box. boxes = [ { :name => "web1.demo.com", ⇒ Host1 this is one of the target nodes :box => "centos/8", ⇒ OS version :ram => 1024, ⇒ Allocated memory :vcpu => 1, ⇒ AllocatedCPU :ip => "192.168.29.2" ⇒ AllocatedIP address of the node }, { :name => "web2.demo.com", ⇒ Host2 this is one of the target nodes :box => "centos/8", :ram => 1024, :vcpu => 1, :ip => "192.168.29.3" }, { :name => "ansible-host", ⇒ AnsibleHost with AnsibleEngine :box => "centos/8", :ram => 8048, :vcpu => 1, :ip => "192.168.29.4" } ]
# Provision each of the VMs. boxes.each do |opts| config.vm.define opts[:name] do |config| # Only Enable this if you are connecting to Proxy server # config.proxy.http = "http://usernam:password@x.y:80"⇒ Needed if you have a proxy # config.proxy.https = "http://usernam:password@x.y:80" # config.proxy.no_proxy = "localhost,127.0.0.1" config.vm.synced_folder ".", "/vagrant", id:"vagrant-root", disabled:true config.ssh.insert_key = false config.vm.box = opts[:box] config.vm.hostname = opts[:name] config.vm.provider :virtualboxdo |v| ⇒ Defines the vagrant provider v.memory = opts[:ram] v.cpus = opts[:vcpu] end config.vm.network :private_network, ip: opts[:ip] config.vm.provision :filedo |file| file.source = './keys/vagrant' ⇒ vagrant keys to allow access to the nodes file.destination = '/tmp/vagrant' ⇒ the location to copy the vagrant key end config.vm.provision :shell, path:"bootstrap-node.sh" ⇒ script that copy hosts entry config.vm.provision :ansibledo |ansible| ⇒ declaration to run ansible playbook ansible.verbose = "v" ansible.playbook = "playbook.yml" ⇒ the playbook used to configure the hosts end end end end
$ vagrant up Bringing machine 'web1.demo.com' up with 'virtualbox' provider... Bringing machine 'web2.demo.com' up with 'virtualbox' provider... Bringing machine 'ansible-host' up with 'virtualbox' provider... ==> web1.demo.com: Importing base box 'centos/8'... ==> web1.demo.com: Matching MAC addressfor NAT networking... ==> web1.demo.com: Checking if box 'centos/8' version '1905.1' is up to date... ==> web1.demo.com: Setting the name of the VM: ansible-labs_web1democom_1606434176593_70913 ==> web1.demo.com: Clearing any previously set network interfaces... ==> web1.demo.com: Preparing network interfaces based on configuration... web1.demo.com: Adapter 1: nat web1.demo.com: Adapter 2: hostonly ==> web1.demo.com: Forwarding ports... web1.demo.com: 22 (guest) => 2222 (host) (adapter 1) ==> web1.demo.com: Running 'pre-boot' VM customizations... ==> web1.demo.com: Booting VM... ==> web1.demo.com: Waiting for machine to boot. This may take a few minutes... web1.demo.com: SSH address: 127.0.0.1:2222 web1.demo.com: SSH username: vagrant web1.demo.com: SSH auth method: private key [...]