Want to build multiple VMs with KVM+Vagrant+ansible

Asked 2 years ago, Updated 2 years ago, 120 views

Vagrant creates multiple VMs on KVM and tries to manipulate them with an answer, but it doesn't work.
Specific symptoms include

  • The specified IP address is not assigned
  • SSH connection times out

We are confirming that
The environment is
Vagrant:2.0.0
available:2.3.2.0
openssh:1.0.2k-fips
libvirtd:3.2

The network configuration uses the KVM standard virbr0 and is 192.168.1.0/24<->192.168.122.0/24
I have a bridge connection.

The Vagrantfile is as follows.

#-*-mode:ruby-*-
# vi:set ft=ruby:   

  config.vm.define:node1do|t|
    t.vm.box="centos 7.3"
    t.vm.hostname="node1"
    t.vm.network "private_network", ip: "192.168.122.101"
  end
  config.vm.define:node2do|t|
    t.vm.box="centos 7.3"
    t.vm.hostname="node2"
    t.vm.network "private_network", ip: "192.168.122.102"
  end


  # conf for possible
  config.vm.provision "ansable" do |ans |
   an.playbook="ansable/vagrant.yml"
   an.inventory_path = "ansable/hosts"
   an.limit="all"
  end


  config.vm.provider:libvirt do|lv|
    lv.management_network_name = "default"
    lv.management_network_address = "192.168.122.30/24"
    lv.storage_pool_name = "kvm_storage"
  end

end

The playbook and hosts files for the answer are as follows.

available/vagrant.yml
- hosts:vagrant
remote_user:vagrant
sudo:yes
tasks:
- name —yum install nmap
yum:name=nmap

available/hosts
[vagrant]
192.168.122.101
192.168.122.102

vagrant ansible

2022-09-30 20:24

1 Answers

Self-resolved.

The reason was that the host was manually specified without knowing the Vagrant's standard feature, the automatic generation of inventory.

I have attached the final Vagrantfile and playbook.

Vagrant.configure("2") do | config |
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requirements a box.You can search for
  # boxes at https://vagrantcloud.com/search.

  config.vm.box="centos7.3"
  N = 2
  (1..N).each do|id|
    config.vm.define "host#{id}" do | node |
      node.vm.hostname="host#{id}"
#      node.vm.network "private_network", ip: "192.168.122.#{100+id}"

      if id == N
        node.vm.provision —ansable do |ans |
          an.limit="all"
          an.playbook="vagrant.yml"
        end
      end
    end
  end
   config.vm.provider:libvirt do|lv|
    lv.management_network_name = "default"
    lv.management_network_address = "192.168.122.30/24"
    lv.storage_pool_name = "kvm_storage"
  end
end




- hosts —all
   remote_user —vagrant
   sudo:yes
   tasks:
     - name —yum install nmap
       yum —name=nmap


2022-09-30 20:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.