虚拟机未从 Vagrantfile 获取静态 IP

VMs are not getting static IP from Vagrantfile

我是网络方面的初学者,而且我刚刚开始使用 VM。 我正在做 "Ansible for Devops" 中的示例,在第 3 章中,我应该创建三个虚拟机并设置一个具有静态 ip 的专用网络。

我的 Vagrant 文件如下所示:

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

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "geerlingguy/centos7"
  config.ssh.insert_key = false
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.vm.provider :virtualbox do |v|
    v.memory = 256
    v.linked_clone = true
  end

  config.vm.define "app1" do |app|
    app.vm.hostname = "orc-app1.dev"
    app.vm.network :private_network, ip: "192.168.60.4"
  end

  config.vm.define "app2" do |app|
    app.vm.hostname = "orc-app2.dev"
    app.vm.network :private_network, ip: "192.168.60.5"
  end

  config.vm.define "db" do |db|
    db.vm.hostname = "orc-db.dev"
    db.vm.network :private_network, ip: "192.168.60.6"
  end
end

流浪小屋:

❯ vagrant up
Bringing machine 'app1' up with 'virtualbox' provider...
Bringing machine 'app2' up with 'virtualbox' provider...
Bringing machine 'db' up with 'virtualbox' provider...
==> app1: Cloning VM...
==> app1: Matching MAC address for NAT networking...
==> app1: Checking if box 'geerlingguy/centos7' is up to date...
==> app1: Setting the name of the VM: 3_app1_1485309004899_30536
==> app1: Fixed port collision for 22 => 2222. Now on port 2202.
==> app1: Clearing any previously set network interfaces...
==> app1: Preparing network interfaces based on configuration...
    app1: Adapter 1: nat
    app1: Adapter 2: hostonly
==> app1: Forwarding ports...
    app1: 22 (guest) => 2202 (host) (adapter 1)
==> app1: Running 'pre-boot' VM customizations...
==> app1: Booting VM...
==> app1: Waiting for machine to boot. This may take a few minutes...
    app1: SSH address: 127.0.0.1:2202
    app1: SSH username: vagrant
    app1: SSH auth method: private key
    app1: Warning: Remote connection disconnect. Retrying...
==> app1: Machine booted and ready!
==> app1: Checking for guest additions in VM...
==> app1: Setting hostname...
==> app1: Configuring and enabling network interfaces...
==> app2: Cloning VM...
==> app2: Matching MAC address for NAT networking...
==> app2: Checking if box 'geerlingguy/centos7' is up to date...
==> app2: Setting the name of the VM: 3_app2_1485309032690_32260
==> app2: Fixed port collision for 22 => 2222. Now on port 2203.
==> app2: Clearing any previously set network interfaces...
==> app2: Preparing network interfaces based on configuration...
    app2: Adapter 1: nat
    app2: Adapter 2: hostonly
==> app2: Forwarding ports...
    app2: 22 (guest) => 2203 (host) (adapter 1)
==> app2: Running 'pre-boot' VM customizations...
==> app2: Booting VM...
==> app2: Waiting for machine to boot. This may take a few minutes...
    app2: SSH address: 127.0.0.1:2203
    app2: SSH username: vagrant
    app2: SSH auth method: private key
    app2: Warning: Remote connection disconnect. Retrying...
==> app2: Machine booted and ready!
==> app2: Checking for guest additions in VM...
==> app2: Setting hostname...
==> app2: Configuring and enabling network interfaces...
==> db: Cloning VM...
==> db: Matching MAC address for NAT networking...
==> db: Checking if box 'geerlingguy/centos7' is up to date...
==> db: Setting the name of the VM: 3_db_1485309060266_65663
==> db: Fixed port collision for 22 => 2222. Now on port 2204.
==> db: Clearing any previously set network interfaces...
==> db: Preparing network interfaces based on configuration...
    db: Adapter 1: nat
    db: Adapter 2: hostonly
==> db: Forwarding ports...
    db: 22 (guest) => 2204 (host) (adapter 1)
==> db: Running 'pre-boot' VM customizations...
==> db: Booting VM...
==> db: Waiting for machine to boot. This may take a few minutes...
    db: SSH address: 127.0.0.1:2204
    db: SSH username: vagrant
    db: SSH auth method: private key
    db: Warning: Remote connection disconnect. Retrying...
==> db: Machine booted and ready!
==> db: Checking for guest additions in VM...
==> db: Setting hostname...
==> db: Configuring and enabling network interfaces...

和 Vagrant SSH 配置:

Host app1
  HostName 127.0.0.1
  User vagrant
  Port 2202
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/mst/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host app2
  HostName 127.0.0.1
  User vagrant
  Port 2203
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/mst/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host db
  HostName 127.0.0.1
  User vagrant
  Port 2204
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/mst/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

如您所见,这些机器没有获得我为它们设置的那些静态 ip,我无法使用它连接到它们。他们刚刚获得了本地主机 IP 和一些高级端口。在那个例子中,我应该在使用 ansible 的机器上工作,并在清单文件中使用静态 ips,所以他们应该正确设置它。

有什么想法吗?

macOS 塞拉利昂 流浪者 1.9.1 VirtualBox 5.1.14

谢谢

编辑:机器使用 CentOS,ip addr 输出为:

[root@orc-app1 vagrant]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:dd:23:fa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 86067sec preferred_lft 86067sec
    inet6 fe80::a00:27ff:fedd:23fa/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 08:00:27:4d:38:fc brd ff:ff:ff:ff:ff:ff

我根据示例进行了检查 - 网络接口文件已由 vagrant 正确创建

[vagrant@orc-app2 ~]$ cd /etc/sysconfig/network-scripts
[vagrant@orc-app2 network-scripts]$ ll
total 236
-rw-r--r--. 1 root    root      353 25 janv. 16:06 ifcfg-enp0s3
-rw-------. 1 vagrant vagrant   214 25 janv. 16:06 ifcfg-enp0s8

并且这个新网络接口的内容是正确的

[vagrant@orc-app2 network-scripts]$ more ifcfg-enp0s8
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.60.5
NETMASK=255.255.255.0
DEVICE=enp0s8
PEERDNS=no
#VAGRANT-END

所以我刚重启网络服务试试

[vagrant@orc-app2 network-scripts]$ sudo systemctl restart network

没关系

[vagrant@orc-app2 network-scripts]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:dd:23:fa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 86391sec preferred_lft 86391sec
    inet6 fe80::a00:27ff:fedd:23fa/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:42:83:e9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.60.5/24 brd 192.168.60.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe42:83e9/64 scope link
       valid_lft forever preferred_lft forever

我没有另一个 centos7 盒子可以测试(仍然可以很好地与 6 一起使用)来确认它与这个盒子或新的 centos 有问题

尝试使用 vagrant 1.9.0。我的同事有一个问题,如果 1.9.1,nfs 共享将无法正确挂载,这与盒子没有自动添加一个必要的接口这一事实有关。

降级到 1.9.0 解决了这个问题。

关于 vagrants github 有几个未解决的问题,它们具体与 rhel/centos 7 有关。

这是其中之一https://github.com/mitchellh/vagrant/issues/8138