# Ansible-建立虛擬機 Vagrant 練習 Ansible 001-2 :::info - Linux Ubuntu ::: ### 下載並安裝 - updating the package list ```shell sudo apt update ``` - Download the Vagrant package 2.2.14 version ```shell curl -O https://releases.hashicorp.com/vagrant/2.2.14/vagrant_2.2.14_x86_64.deb ``` - install Vagrant ``` shell sudo apt install ./vagrant_2.2.14_x86_64.deb ``` ### 查看 Vagrant 版本 ```shell vagrant --version ``` ==Vagrant 2.2.14== ### 建立 Vagrant 主機 ```shell # 版本可以自己設定 vagrant init ubuntu/trusty64 ``` ==ubuntu/trusty64 為 vagrant 系統版本== ### 建立多台 Vagrant 主機 - 建立生成vagrant檔案 FileName: Vagrantfile ```yaml Vagrant.configure("2") do |config| config.vm.box = "ubuntu/trusty64" config.vm.define "web" do |web| web.vm.network "private_network", ip: "192.168.33.11" end config.vm.define "db" do |db| db.vm.network "private_network", ip: "192.168.33.12" end end ``` ### 啟動 vagrant - 需將 teminal 路徑進到在 Vagrantfile 所在位置的目錄 將會根據 Vagrantfile 的內容建立主機 ```shell vagrant up ``` ### 查看 vagrant 訊息 - 需將 teminal 路徑進到在 Vagrantfile 所在位置的目錄 ```shell vagrant status ``` ```shell vagrant ssh-config ``` ![](https://i.imgur.com/RfrVyrf.png) ### 關閉和移除Vagrant主機 ```shell vagrant halt # 關閉機器 vagrant destroy # 移除虛擬機器 ``` ### 連線到Vagrant主機 ```shell vagrant ssh [Node name, default=""] # 直接連線到主機預設空白 ssh vagrant@127.0.0.1 -p 2222 預設密碼為 vagrant ``` ###### tags: `Ansible pre-work`