使用 Vagrant 在不同的操作系统上测试你的脚本
Vagrant 虚拟机

> 
> Vagrant 可以帮助你在你的电脑上运行其他操作系统，这意味着你可以构建、测试、疯狂折腾而不毁坏你的系统。
> 
> 
> 


![](/data/attachment/album/202109/28/115827gv75kkrhnnhvkhcf.jpg "Woman using laptop concentrating")


我使用 Vagrant 已经很长时间了。我使用几种 DevOps 工具，把它们全安装在一个系统上会搞得很乱。Vagrant 可以让你在不破坏系统的情况下随意折腾，因为你根本不需要在生产系统上做实验。


如果你熟悉 [VirtualBox](https://opensource.com/article/21/6/try-linux-virtualbox) 或 [GNOME Boxes](https://opensource.com/article/19/5/getting-started-gnome-boxes-virtualization)，那么学习 Vagrant 很容易。Vagrant 有一个简单而干净的界面用于管理虚拟机。一个名为 `Vagrantfile` 的配置文件，允许你定制你的虚拟机（称为 “Vagrant <ruby> 盒子 <rt>  box </rt></ruby>”）。一个简单的命令行界面让你启动、停止、暂停或销毁你的“盒子”。


考虑一下这个简单的例子。


假设你想写 Ansible 或 shell 脚本，在一个新的服务器上安装 Nginx。你不能在你自己的系统上这样做，因为你运行的可能不是你想测试的操作系统，或者没有所有的依赖项。启动新的云服务器进行测试可能会很费时和昂贵。这就是 Vagrant 派上用处的地方。你可以用它来启动一个虚拟机，用你的脚本来<ruby> 配备 <rt>  provision </rt></ruby>它，并证明一切按预期工作。然后，你可以删除这个“盒子”，重新配备它，并重新运行你的脚本来验证它。你可以多次重复这个过程，直到你确信你的脚本在所有条件下都能工作。你可以将你的 Vagrantfile 提交给 Git，以确保你的团队正在测试完全相同的环境（因为他们将使用完全相同的测试机）。不会再有“但它在我的机器上运行良好！”这事了。


### 开始使用


首先，[在你的系统上安装 Vagrant](https://www.vagrantup.com/docs/installation)，然后创建一个新的文件夹进行实验。在这个新文件夹中，创建一个名为 `Vagrantfile` 的新文件，内容如下：



```
Vagrant.configure("2") do |config|

 config.vm.box = "ubuntu/hirsute64"

end

```

你也可以运行 `vagrant init ubuntu/hirsute64`，它将为你生成一个新的 Vagrant 文件。现在运行 `vagrant up`。这个命令将从 Vagrant 仓库中下载 `ubuntu/hirsuite64` 镜像。



```
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/hirsute64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/hirsute64' version '20210820.0.0' is up to date...
==> default: Setting the name of the VM: a_default_1630204214778_76885
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!

```

此时，如果你打开你的 Vagrant 后端（如 VirtualBox 或 virt-manager），你会看到你的“盒子”已经有了。接下来，运行 `vagrant ssh` 登录到“盒子”。如果你能看到 Vagrant 的提示符，那么你就进入了！



```
~ vagrant ssh
Welcome to Ubuntu 21.04 (GNU/Linux 5.11.0-31-generic x86_64)

* Documentation: <https://help.ubuntu.com>
* Management:   <https://landscape.canonical.com>
* Support:    <https://ubuntu.com/advantage>

 System information as of Sun Aug 29 02:33:51 UTC 2021

 System load: 0.01       Processes:        110
 Usage of /:  4.1% of 38.71GB  Users logged in:     0
 Memory usage: 17%        IPv4 address for enp0s3: 10.0.2.15
 Swap usage:  0%        IPv4 address for enp0s8: 192.168.1.20

0 updates can be applied immediately.

vagrant@ubuntu-hirsute:~$

```

Vagrant 使用“基础盒子”来建立你的本地机器。在我们的例子中，Vagrant 从 [Hashicorp 的 Vagrant 目录](https://app.vagrantup.com/boxes/search)下载 `ubuntu/hirsuite64` 镜像，并插入 VirtualBox 来创建实际的“盒子”。


### 共享文件夹


Vagrant 将你的当前文件夹映射到 Vagrant “盒子”中的 `/vagrant`。这允许你在你的系统和“盒子”里保持文件同步。这很适合测试 Nginx 网站，通过将你的文件根目录指向 `/vagrant`。你可以使用 IDE 进行修改，“盒子”里的 Nginx 会提供这些修改。


### Vagrant 命令


有几个 Vagrant 命令，你可以用它们来控制你的“盒子”。


其中一些重要的命令是：


* `vagrant up`：启动一个“盒子”。
* `vagrant status`：显示当前“盒子”的状态。
* `vagrant suspend`：暂停当前的“盒子”。
* `vagrant resume`：恢复当前的“盒子”。
* `vagrant halt`：关闭当前的“盒子”。
* `vagrant destroy`：销毁当前的“盒子”。通过运行此命令，你将失去存储在“盒子”上的任何数据。
* `vagrant snapshot`：对当前的“盒子”进行快照。


### 试试 Vagrant


Vagrant 是一个使用 DevOps 原则进行虚拟机管理的工具，久经时间考验。配置你的测试机，与你的团队分享配置，并在一个可预测和可重复的环境中测试你的项目。如果你正在开发软件，那么通过使用 Vagrant 进行测试，你将为你的用户提供良好的服务。如果你不开发软件，但你喜欢尝试新版本的操作系统，那么没有比这更简单的方法了。今天就试试 Vagrant 吧！


这篇文章最初发表在 [作者的个人博客](https://notes.ayushsharma.in/2021/08/introduction-to-vagrant) 上，经许可后被改编。




---


via: <https://opensource.com/article/21/9/test-vagrant>


作者：[Ayush Sharma](https://opensource.com/users/ayushsharma) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
I've been happy using Vagrant for quite a while now. I work with several DevOps tools, and installing them all on one system can get complicated. Vagrant lets you do cool things without breaking your system because you don't have to experiment on your production system at all.

If you're familiar with [VirtualBox](https://opensource.com/article/21/6/try-linux-virtualbox) or [GNOME Boxes](https://opensource.com/article/19/5/getting-started-gnome-boxes-virtualization), then learning Vagrant is easy. Vagrant is a simple and clean interface for working with virtual machines. A single config file, called `Vagrantfile`

, allows you to customize your virtual machines (called *Vagrant boxes*). A simple command-line interface lets you start, stop, suspend, or destroy your boxes.

Consider this simple example.


Let's say you want to write Ansible or shell scripts to install Nginx on a new server. You can't do it on your own system because you might not be running the operating system you want to test, or you may not have all of the dependencies for what you want to do. Launching new cloud servers for testing can be time-consuming and expensive. This is where Vagrant comes in. You can use it to bring up a virtual machine, provision it using your scripts, and prove that everything works as expected. You can then delete the box, re-provision it, and re-run your scripts to verify it. You can repeat this process as many times as you want until you're confident your scripts work under all conditions. And you can commit your Vagrantfile to Git to ensure your team is testing the exact same environment (because they'll be using the exact same test box). No more "…but it works fine on my machine!"

## Getting started

First, [install Vagrant on your system](https://www.vagrantup.com/docs/installation) and then create a new folder to experiment in. In this new folder, create a new file named `Vagrantfile`

with these contents:

```
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/hirsute64"
end
```


You can also run `vagrant init ubuntu/hirsute64`

, and it will generate a new Vagrantfile for you. Now run `vagrant up`

. This command will download the `ubuntu/hirsuite64`

image from the Vagrant registry.

```
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/hirsute64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/hirsute64' version '20210820.0.0' is up to date...
==> default: Setting the name of the VM: a_default_1630204214778_76885
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Remote connection disconnect. Retrying...
default: Warning: Connection reset. Retrying...
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
```


At this point, if you open your Vagrant backend (such as VirtualBox or virt-manager), you'll see your box there. Next, run `vagrant ssh `

to log in to the box. If you can see the Vagrant prompt, then you’re in!

```
~ vagrant ssh
Welcome to Ubuntu 21.04 (GNU/Linux 5.11.0-31-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sun Aug 29 02:33:51 UTC 2021
System load: 0.01 Processes: 110
Usage of /: 4.1% of 38.71GB Users logged in: 0
Memory usage: 17% IPv4 address for enp0s3: 10.0.2.15
Swap usage: 0% IPv4 address for enp0s8: 192.168.1.20
0 updates can be applied immediately.
vagrant@ubuntu-hirsute:~$
```


Vagrant uses "base boxes" to bring up your local machines. In our case, Vagrant downloads the `ubuntu/hirsuite64`

image from [Hashicorp’s Vagrant catalogue](https://app.vagrantup.com/boxes/search) and plugs into VirtualBox to create the actual box.

## Shared folders

Vagrant maps your current folder as `/vagrant`

within the Vagrant box. This allows you to keep your files in sync on your system and within the box. This is great for testing a Nginx website by pointing your document root to `/vagrant`

. You can use an IDE to make changes and Nginx within the box will serve them.

## Vagrant commands

There are several Vagrant commands which you can use to control your box.

Some of the important ones are:

`vagrant up`

: Bring a box online.`vagrant status`

: Show current box status.`vagrant suspend`

: Pause the current box.`vagrant resume`

: Resume the current box.`vagrant halt`

: Shutdown the current box.`vagrant destroy`

: Destroy the current box. By running this command, you will lose any data stored on the box.`vagrant snapshot`

: Take a snapshot of the current box.

## Try Vagrant

Vagrant is a time-tested tool for virtual machine management using DevOps principles. Configure your test machines, share the configs with your team, and test your projects in a predictable and reproducible environment. If you're developing software, then you'll do your users a great service by using Vagrant for testing. If you're not developing software but you love to try out new versions of an OS, then there's no easier way. Try Vagrant today!

*This article was originally published on the author's personal blog and has been adapted with permission.*

## Comments are closed.