Use vagrant to test your scripts on different operating systems

Linux China 2021-10-14 04:29:44

Vagrant It can help you run other operating systems on your computer , This means that you can build 、 test 、 Go crazy without destroying your system .

I use Vagrant It's been a long time . I use several DevOps Tools , Installing them all on one system will make a mess .Vagrant It allows you to toss around without damaging the system , Because you don't need to experiment on the production system at all .

If you are familiar with    or  , So learning Vagrant be prone to .Vagrant There is a simple and clean interface for managing virtual machines . A group called  Vagrantfile  Configuration file for , Allows you to customize your virtual machine ( be called “Vagrant  The box box”). A simple command line interface allows you to start 、 stop it 、 Suspend or destroy your “ The box ”.

Consider this simple example .

Suppose you want to write Ansible or shell Script , Install... On a new server Nginx. You can't do this on your own system , Because you may not be running the operating system you want to test , Or without all the dependencies . Starting a new ECS for testing can be time-consuming and expensive . This is it. Vagrant A place of use . You can use it to start a virtual machine , Use your script to Equipped with provision it , And prove that everything works as expected . then , You can delete this “ The box ”, Refit it , And rerun your script to verify it . You can repeat this process many times , Until you are sure that your script will work under all conditions . You can Vagrantfile Submit to Git, To make sure your team is testing exactly the same environment ( Because they will use exactly the same testing machine ). There will be no more “ But it works well on my machine !” It's over .

Start using

First ,, Then create a new folder to experiment . In this new folder , Create a file called  Vagrantfile  The new document of , The contents are as follows :

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

You can also run  vagrant init ubuntu/hirsute64, It will generate a new Vagrant file . Now run  vagrant up. This order will come from Vagrant Download... In the warehouse  ubuntu/hirsuite64  Mirror image .

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!

here , If you open your Vagrant Back end ( Such as VirtualBox or virt-manager), You will see your “ The box ” Already there. . Next , function  vagrant ssh  Log in to “ The box ”. If you can see Vagrant Prompt , Then you enter !

~ 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.
[email protected]:~$

Vagrant Use “ Basic box ” To build your local machine . In our case ,Vagrant from   download  ubuntu/hirsuite64  Mirror image , And insert VirtualBox To create the actual “ The box ”.

Shared folders

Vagrant Map your current folder to Vagrant “ The box ” Medium  /vagrant. This allows you in your system and “ The box ” Keep files in sync . This is good for testing Nginx Website , By pointing your file root directory to  /vagrant. You can use IDE Make changes ,“ The box ” Inside Nginx These modifications will be provided .

Vagrant command

There are several Vagrant command , You can use them to control your “ The box ”.

Some of the important commands are :

  • vagrant up: Start a “ The box ”.
  • vagrant status: Show the current “ The box ” The state of .
  • vagrant suspend: The current pause “ The box ”.
  • vagrant resume: Restore the current “ The box ”.
  • vagrant halt: Close current “ The box ”.
  • vagrant destroy: Destroy the current “ The box ”. By running this command , You will lose storage in “ The box ” Any data on .
  • vagrant snapshot: For the current “ The box ” Take a snapshot .

try Vagrant

Vagrant It's a use DevOps A tool for virtual machine management , Time tested . Configure your Tester , Share the configuration with your team , And test your project in a predictable and repeatable environment . If you're developing software , Then by using Vagrant To test , You will provide good service for your users . If you don't develop software , But you like to try new versions of the operating system , Then there's no easier way than this . Try it today Vagrant Well !

This article was originally published in    On , Adapted with permission .


via: 

author :  Topic selection :  translator :  proofreading :

This paper is written by    Original compilation ,  Honor roll out


Please bring the original link to reprint ,thank
Similar articles