Showing posts with label Vagrant. Show all posts
Showing posts with label Vagrant. Show all posts

Vagrant: How to provide custom SSH key location to solve chmod 600 error ?

Hello Everyone,
So today I am trying to spin a new vagrant instance from my Arch installation in a NTFS Partition.
So during `vagrant up` command, I got an error stating that it cant execute `chmod 600` command in my NTFS parition as my Vagrantfile located at my NTFS mount.
So on searching a bit, I came across custom SSH location config can be added to Vagrantfile as below

config.ssh.insert_key = false
config.ssh.private_key_path = "~/.vagrant.d/insecure_private_key"


so my Vagrantfile config over looks like below

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.ssh.insert_key = false
  config.ssh.private_key_path = "~/.vagrant.d/insecure_private_key"
end


And I have deleted the old vagrant machine with `vagrant destroy` command, and re provisioned the current vagrant machine with `vagrant up` and issue then I was able to SSH into the vagrant machine successfully with `vagrant ssh`.
Hope it helps.
Thank you.

Vagrantfile: In a more ruby way to customize RAM and CPU core.

Hello Everyone,

    Here is my new Vagrantfile which coded in more ruby way. This code helps you with customize RAM, CPU and OS based provision script.

Most of the code is self explanatory if you have basic understanding of Vagrant.



# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.

$hostnames = <<-script echo="" master.mypuppetsetup.com="" puppet="" puppetmaster="">> /etc/hosts
echo "192.168.66.20 k8sworker1 k8sworker1.mypuppetsetup.com" >> /etc/hosts
echo "192.168.66.30 k8sworker2 k8sworker2.mypuppetsetup.com" >> /etc/hosts
SCRIPT

$centos_setup = <<-script -y="" :hostname="" apt-get="" epel-release="" install="" script="" servers="[" ubuntu_setup="<<-SCRIPT" update="" yum=""> "puppetmaster",
    :ip => "192.168.66.10",
    :box => "centos/7",
    :ram => 4096,
    :cpu => 2
  },
  {
    :hostname => "k8sworker1",
    :ip => "192.168.66.20",
    :box => "centos/7",
    :ram => 2048,
    :cpu => 1
  },
   {
    :hostname => "k8sworker2",
    :ip => "192.168.66.30",
    :box => "ubuntu/trusty64",
    :ram => 2048,
    :cpu => 1
  }
]
Vagrant.configure("2") do |config|

  servers.each do |machine|
    config.vm.define machine[:hostname] do |node|
      node.vm.box = machine[:box]
      node.vm.hostname = machine[:hostname]
      node.vm.network "private_network", ip:machine[:ip]
      node.vm.provision "shell", inline: $hostnames
      node.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", machine[:ram]]
        vb.customize ["modifyvm", :id, "--cpus", machine[:cpu]]
      end
    node.vm.provision "shell" ,inline: $hostnames
    
    if machine[:box] == "centos/7"
      node.vm.provision "shell" ,inline: $centos_setup
    end

    if machine[:box] == "ubuntu/trusty64"
      node.vm.provision "shell" ,inline: $ubuntu_setup
    end

    end
  end
end
 
 
Hope it helps
Thank you.

A Go-Ready vagrant setup

Hello,

I hope you know what is a Vagrant. If you don't , please visit https://www.vagrantup.com/intro/index.html.

For a Vagrant setup Vagrantfile is important. In the Vagrantfile we can define so many parameters for the VM we would like to build, for example IP Address, Hostname, Port Forwarding and while spinning the VM itself we can install the new packages and make our development or testing environment ready when ever we want.

I dont write Installation of Vagrant here, because their documentation is excellent and I dont want to duplicate it. If you want to install Vagrant , look at https://www.vagrantup.com/downloads.html and VirtuboxVM is one of pre-requisite for Vagrant and I hope you know how to install VirtualBox, if you dont know then look at https://www.virtualbox.org/wiki/Downloads.

Now I assume , you have installed Vagrant and VirtulBox.  Whether you are using Windows or Linux commands are same , just use your senses at path format which is different for Windows and Linux.

Here I will show you how to generate a new Vagrant file and how to use it as per our requirement.
Open you command prompt or powershell prompt depends on the OS and type as


mkdir vagrant_1
cd vagrant_1
vagrant init

Here vagrant_1 is the directory name where I have initialized the vagrant and you can replace it with anything you want. If you see contents of vagrant_1 directory after executing vagrant init , you will see a file with name Vagrantfile.

This is our main file. Open it with your favorite editor like Sublime, Atom , Brackets or any other you like.

Now observe carefully. Remove everything between

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

Now lets define some configuration.

Vagrant.configure("2") do |config|
config.vm.define "vagrant-centos01" do |vc01|
vc01.vm.box = "centos/7"
vc01.vm.hostname = "vagrant-centos01"
vc01.vm.network "private_network", ip: "192.168.20.20"
end
end

Lets go through each of them ,

1. With Vagrant.configure("2") do |config| , we saying to Vagrant that use Vagrant from 1.1+ to 2.0.X.

2. With config.vm.define "vagrant-centos01" do |vc01| we are saying as define or create a new VM with name as vagrant-centos01

3. With vc01.vm.box = "centos/7" , we are saying as for use centos 7 box. if you centos-7 not available locally , Vagrant download it from Hashicorp.

4. vc01.vm.hostname = "vagrant-centos01" , says what is the hostname

5. vc01.vm.network "private_network", ip: "192.168.20.20" , says what is the private IP or static IP and this is similar to Host-Only adapter at VirtualBox.

Thats it, A very basic VM setup is ready with hostname and Private IP. After saving this configuration use command as

vagrant up

and then

vagrant ssh 

To login into that machine. And this is very basic Vagrantfile setup. The deeper you dive the complex and beautiful it will.

For more information on building a Vagrantfile , check https://www.vagrantup.com/docs/vagrantfile/


Hope that helps.


=======================THIS IS NOT THE END===========================