> For the complete documentation index, see [llms.txt](https://davidjosearaujo.gitbook.io/online-courses/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://davidjosearaujo.gitbook.io/online-courses/infrastructure-as-code/on-premises-iac/vagrant-basics.md).

# Vagrant Basics

In our on-prem IaC journey, we will start with [Vagrant](https://developer.hashicorp.com/vagrant). Vagrant is a software solution that can be used for building and maintaining portable virtual software development environments. In essence, Vagrant can be used to create resources from an IaC pipeline. You can think of Vagrant as the big brother of Docker. In the context of Vagrant, Docker would be seen as a provider, meaning that Vagrant could be used to not only deploy Docker instances but also the actual servers that would host them.

## Terminology

Before diving into using Vagrant, let's brush up on some terminology first.

<table><thead><tr><th width="132" align="center">Term</th><th align="center">Definition</th></tr></thead><tbody><tr><td align="center">Provider</td><td align="center">A Vagrant provider is the virtualisation technology that will be used to provision the IaC deployment. Vagrant can use different providers such as Docker, VirtualBox, VMware, and even AWS for cloud-based deployments.</td></tr><tr><td align="center">Provision</td><td align="center">Provision is the term used to perform an action using Vagrant. This can be actions such as adding new files or running a script to configure the host created with Vagrant.</td></tr><tr><td align="center">Configure</td><td align="center">Configure is used to perform configuration changes using Vagrant. This can be changed by adding a network interface to a host or changing its hostname.</td></tr><tr><td align="center">Variable</td><td align="center">A variable stores some value that will be used in the Vagrant deployment script.</td></tr><tr><td align="center">Box</td><td align="center">The Box refers to the image that will be provisioned by Vagrant.</td></tr><tr><td align="center">Vagrantfile</td><td align="center">The Vagrantfile is the provisioning file that will be read and executed by Vagrant.</td></tr></tbody></table>

## Vagrant Example

Let's have a look at a simple Vagrant provisioning script. In our example here, we have the following folder structure:

```bash
.
├── provision
│   ├── files.zip
│   └── script.sh
└── Vagrantfile
```

The Vagrantfile script has the following code:

```ruby
Vagrant.configure("2") do |cfg|
  cfg.vm.define "server" do |config|
    config.vm.box = "ubuntu/bionic64"
    config.vm.hostname = "testserver"
    config.vm.provider :virtualbox do |v, override|
       v.gui = false 
       v.cpus = 1
       v.memory = 4096
    end

    config.vm.network :private_network,
        :ip => 172.16.2.101
    config.vm.network :private_network,
        :ip => 10.10.10.101
  end

  cfg.vm.define "server2" do |config|
    config.vm.box = "ubuntu/bionic64"
    config.vm.hostname = "testserver2"
    config.vm.provider :virtualbox do |v, override|
       v.gui = false 
       v.cpus = 2
       v.memory = 4096
    end

    #Upload resources
    config.vm.provision "file", source: "provision/files.zip",    destination: "/tmp/files.zip"

    #Run script
    config.vm.provision "shell", path: "provision/script.sh"
  end
end
```

In this example, we are going to provision two servers. Both of these servers will start with the base Ubuntu Bionic x64 image. Like Docker, these images will be pulled from a public image repo. We could, however, tell Vagrant where to find these images, meaning they can be pulled from a private repo of images as well. You can see that both servers are configured with a hostname and how many CPUs (1) and RAM (4GB) each host will have. The first server will also have two network interfaces with static IPs attached. The second server will have a file called `files.zip` uploaded, and a script called `script.sh` will be executed on the host.

If we want to provision the entire script, we would use the command `vagrant up`. This would provision both servers in the order specified in the Vagrantfile. We could also decide to only provision one of the servers by using the server name. For example, `testserver` could be provisioned using `vagrant up server`.

If you want to play around with VirtualBox provisioning using Vagrant, look at this [repo](https://github.com/MWR-CyberSec/tabletop-lab-creation). Using VirtualBox and Vagrant, this repo will allow you to create your very own Active Directory (AD) network with two domain controllers, a server, and a workstation. Give the Vagrantfile a read to see what provisioning will be performed on each host.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://davidjosearaujo.gitbook.io/online-courses/infrastructure-as-code/on-premises-iac/vagrant-basics.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
