我正在使用 vagrant 启动多个虚拟机,这是我的 vagrantfile(直到第一个主机,删除注释):
# Installed Vagrant Version: 2.2.14
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-20.04"
config.vm.provider :virtualbox
mount_new_disk = <<-SCRIPT
sudo useradd -m -s /bin/bash -G sudo testuser1
sudo echo -e "123\n123"|sudo passwd testuser1
sudo mkdir /mnt/da
sudo mkfs -t ext4 /dev/sdb
sudo mount /dev/sdb /mnt/da
echo "/dev/sdb /mnt/da ext4 auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0" |sudo tee -a /etc/fstab
SCRIPT
make_default_gw = <<-SCRIPT
superda=$(sudo ip route show|head -1|awk '{print $3}');if ! [[ $superda =~ "192.168.1.254" ]]; then echo "router is not default, removing";sudo ip route del default via $superda ;else echo "yes it's default, gud";fi
SCRIPT
nfs_host = <<-SCRIPT
sudo apt update
sudo apt install nfs-kernel-server
echo "/mnt/da 10.118.8.0/24(rw,sync,no_subtree_check)"|sudo tee -a /etc/exports
sudo chown nobody:nogroup /mnt/da
sudo chmod 777 /mnt/da
sudo systemctl restart nfs-kernel-server
sudo ufw allow from 10.118.8.0/24 to any port nfs
sudo ufw allow from 10.118.8.0/24 to any port ssh
sudo ufw enable
SCRIPT
nfs_client = <<-SCRIPT
sudo apt update
sudo apt install nfs-common
sudo mkdir /mnt/nfs
sudo mount 10.118.8.10:/mnt/da /mnt/nfs
echo "10.118.8.10:/mnt/da /mnt/nfs nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0" |sudo tee -a /etc/fstab
SCRIPT
config.vm.define "da1" do |da1|
da1.vm.synced_folder ".", "/vagrant", disabled: true
da1.vm.disk :disk, name: "backup", size: "10GB"
da1.vm.hostname = "da1"
da1.vm.network "private_network", ip: "10.118.8.10"
da1.vm.network "public_network", use_dhcp_assigned_default_route: true, netmask: "255.255.255.0", bridge: "Intel(R) I211 Gigabit Network Connection"
da1.vm.provision :shell, :inline => mount_new_disk
da1.vm.provision :shell, :inline => make_default_gw
da1.vm.provision :shell, :inline => nfs_host
config.ssh.forward_agent = true
end
config.vm.define "da2" do |da2|
da2.vm.synced_folder ".", "/vagrant", disabled: true
da2.vm.disk :disk, name: "backup", size: "10GB"
da2.vm.hostname = "da2"
da2.vm.network "private_network", ip: "10.118.8.11"
da2.vm.network "public_network", use_dhcp_assigned_default_route: true, netmask: "255.255.255.0", bridge: "Intel(R) I211 Gigabit Network Connection"
config.vm.provision :shell, :inline => mount_new_disk
config.vm.provision :shell, :inline => make_default_gw
config.vm.provision :shell, :inline => nfs_client
config.ssh.forward_agent = true
end
config.vm.define "da3" do |da3|
da3.vm.synced_folder ".", "/vagrant", disabled: true
da3.vm.disk :disk, name: "backup", size: "10GB"
da3.vm.hostname = "da3"
da3.vm.network "private_network", ip: "10.118.8.12"
da3.vm.network "public_network", use_dhcp_assigned_default_route: true, netmask: "255.255.255.0", bridge: "Intel(R) I211 Gigabit Network Connection"
# da3.vm.network "forwarded_port", guest: 22, host: 5555
config.vm.provision :shell, :inline => mount_new_disk
config.vm.provision :shell, :inline => make_default_gw
config.vm.provision :shell, :inline => nfs_client
config.ssh.forward_agent = true
end
现在脚本mount_new_disk
和make_default_gw
应该在所有主机上运行,而nfs_host
应该仅在上运行da1
,并且nfs_client
应该仅在上运行da[23]
。
但是对于某些 readon,每个脚本在每个主机上运行,并且它们也运行多次。
请注意,我所遵循的教程说要写config.vm.provision :shell, :inline => mount_new_disk
,但我也尝试了,da1.vm.provision :shell, :inline => mount_new_disk
结果相同。
例如:我将echo 'RUNNING SCRIPT START: ...'
在每个脚本的顶部和echo 'RUNNING SCRIPT END: ...'
底部写一个来查看哪个脚本运行了以及运行了多少次(捕获输出然后对其进行 grepped):
unu@here:~/yes$ grep -n 'RUNNING SCRIPT' opt
44: da1: RUNNING SCRIPT START: mount_new_disk
62: da1: RUNNING SCRIPT STOP: mount_new_disk
65: da1: RUNNING SCRIPT START: make_default_gw
67: da1: RUNNING SCRIPT STOP: make_default_gw
70: da1: RUNNING SCRIPT START: nfs_client
120: da1: RUNNING SCRIPT STOP: nfs_client
123: da1: RUNNING SCRIPT START: mount_new_disk
137: da1: RUNNING SCRIPT STOP: mount_new_disk
140: da1: RUNNING SCRIPT START: make_default_gw
142: da1: RUNNING SCRIPT STOP: make_default_gw
145: da1: RUNNING SCRIPT START: nfs_client
174: da1: RUNNING SCRIPT STOP: nfs_client
177: da1: RUNNING SCRIPT START: mount_new_disk
191: da1: RUNNING SCRIPT STOP: mount_new_disk
194: da1: RUNNING SCRIPT START: make_default_gw
196: da1: RUNNING SCRIPT STOP: make_default_gw
199: da1: RUNNING SCRIPT START: nfs_host
261: da1: RUNNING SCRIPT STOP: nfs_host
正如您所看到的,它运行了所有脚本(甚至是那些在其自己的配置部分中未提及的脚本),并且多次运行了它们。
有趣的是,在 nfs 客户端 da2 上,它没有运行该nfs_host
脚本,但它仍然运行了其他所有内容两次:
unu@here:~/yes$ grep -n 'RUNNING SCRIPT' noua
41: da2: RUNNING SCRIPT START: mount_new_disk
61: da2: RUNNING SCRIPT STOP: mount_new_disk
64: da2: RUNNING SCRIPT START: make_default_gw
66: da2: RUNNING SCRIPT STOP: make_default_gw
69: da2: RUNNING SCRIPT START: nfs_client
118: da2: RUNNING SCRIPT STOP: nfs_client
121: da2: RUNNING SCRIPT START: mount_new_disk
134: da2: RUNNING SCRIPT STOP: mount_new_disk
137: da2: RUNNING SCRIPT START: make_default_gw
139: da2: RUNNING SCRIPT STOP: make_default_gw
142: da2: RUNNING SCRIPT START: nfs_client
171: da2: RUNNING SCRIPT STOP: nfs_client
现在我知道我可以将所有这些都放在配置部分本身中,如下所示:
config.vm.define "da1" do |da1|
da1.vm.synced_folder ".", "/vagrant", disabled: true
da1.vm.disk :disk, name: "backup", size: "10GB"
da1.vm.hostname = "da1"
da1.vm.network "private_network", ip: "10.118.8.10"
da1.vm.network "public_network", use_dhcp_assigned_default_route: true, netmask: "255.255.255.0", bridge: "Intel(R) I211 Gigabit Network Connection"
da1.vm.provision :shell, :inline => mount_new_disk
da1.vm.provision :shell, :inline => make_default_gw
da1.vm.provision :shell, :inline => nfs_host
da1.vm.provision :shell, :inline => <<-SCRIPT
echo "yohoho and a bottle of rum!"
SCRIPT
config.ssh.forward_agent = true
但是另一种方式,只需在需要时调用它,就更方便了。
有人知道为什么会发生这种情况吗?以及如何解决它?
答案1
发现问题:我必须config.vm.provision :shell, :inline => script_name
根据配置块用适当的虚拟机名称进行替换。
所以da1
我做到了da1.vm.provision :shell, :inline => script_name
,因为da2
我做到了da2.vm.provision :shell, :inline => script_name
,因为da3
我做到了da3.vm.provision :shell, :inline => script_name
。