How to use Vagrant+Ansible from WSL with hyperV

what we have installed from previous articles:

  • We have installed Windows 10 with hyper-v enabled
  • We have Windows Subsystem for Linux (WSL) enabled
  • We have installed Ubuntu 18.04 LTS from Windows Store
  • Inside Ubuntu we have installed and configured zsh and docker client
  • On Windows 10 we have installed hyper and Docker for Windows

Goal of this article is creating Virtual Machine (VM) with
OS Redhat 8 like testing enviroment automated way.

Procedure:

  1. Install Ansible and Vagrant into Ubuntu
  2. Install and configure Network Adapter for connection with VM OS Redhat 8
  3. Make Vagrantfile for creating VM with OS Redhat 7
  4. Make Ansible script for provisoning VM with Redhat 7
  5. Starting OS Redhat8
overview of environment
for SW development
Install Python3, PIP3 and Ansible
$sudo apt update && apt upgrade -y
$sudo apt-get install python3.7 /$sudo apt-get --only-upgrade install python3
$python3 -V

$sudo apt install python3-pip
$python3 -m pip --version

#for creating Python Virtual enviroment
$sudo apt install python3.7-venv

 
$pip3 install ansible --user
$ansible --version
Install Vagrant
Go to https://www.vagrantup.com/downloads.html.
Right-click and copy the link for the 64-bit Debian package.
$wget https://releases.hashicorp.com/vagrant/2.2.6/vagrant_2.2.6_x86_64.deb
$sudo dpkg -i vagrant_2.2.6_x86_64.deb
$export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
$export PATH="$PATH:/c/Windows/System32/WindowsPowerShell/v1.0"
$export PATH=$PATH:/c/Windows/System32

Prerequisite

Python: 3.7.1
PIP3: 9.0.1
Vagrant: 2.2.6
Ansible: 2.9.1

Install and configure Network Adapter for connection with VM OS Redhat 7

On Windows 10 we should run Windows PowerShell as Administrator and do folowing

//Create new internal Switch
#New-VMSwitch -SwitchName "Vagrant_Switch" -SwitchType Internal
//find "ifindex" of "Vagrant_Switch"
$Get-NetAdapter
//Set IP address for "Vagrant_Switch"
$New-NetIPAddress -IPAddress 192.168.10.3 -PrefixLength 24 -InterfaceIndex number_findex
//Create NAT for "Vagrant_Switch"
$New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.10.0/24
//list of Nat networks
$Get-NetNat
//Remove all NAT networks
$Get-NetNat | Remove-NetNat
//OR remove only my MyNATnetwork
$Remove-NetNat MyNATnetwork
//list of switch
$Get-VMSwitch

So now we have created network interface on Windows 10 that we will use for comunication with VM machine (OS Redhat)

Make Vagrantfile for creating VM with OS Redhat 7

VAGRANTFILE_API_VERSION = "2"


# This script activate developer subscription, so I can install and upgrade my VM with OS Redhat
# Create developer account and change keyword "my_user" and "my_pass" in the script
# source https://printhelloworld.de/posts/working-with-redhat-linux-in-vagrant/


$script = %{
  
if ! sudo subscription-manager status; then
  sudo subscription-manager register --username my_user --password=my_pass --auto-attach --force
  sudo yum-config-manager --disable rhel-7-server-rt-beta-rpms
  sudo subscription-manager repos --enable=rhel-7-server-rpms
  sudo subscription-manager repos --enable=rhel-7-server-extras-rpms
  sudo subscription-manager repos --enable=rhel-7-server-optional-rpms
fi
}


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://app.vagrantup.com/generic/boxes/rhel8.
  # List of installed vagrant boxess $vagrant box list -i
  # Update of installed box $vagrant box update generic/rhel8
  config.vm.box = "generic/rhel8"
  config.vm.provider "hyperv"
  


  #Synchronization folders between WSL and VM does not work properly so we disable it
  #Always we start VM from WSL (vagrant up) inside folder, where Vagrantfile exists
  config.vm.synced_folder ".", "/vagrant", disabled: true


  

  # $vagrant provision --provision-with shell
  # this ensure registering redhat subscription after VM is UP (vagrant up) and delete subscription after (vagrant destroy) 
  config.vm.provision "shell", inline: $script
  config.trigger.before :destroy do
    begin
      run_remote "sudo subscription-manager unregister"
    rescue
      puts "Something went wrong, please remove the machine manually from https://access.redhat.com/management/subscriptions"
    end
  end

  # $vagrant provision --provision-with ansible 
  # run ansible script remotely 
  config.vm.provision "ansible" do |ansible|
    ansible.verbose = "v"
    ansible.playbook = "playbook.yml"
  end

  #Definition of VM
   config.vm.provider "hyperv" do |h|
     h.vmname = "RHEL7VirtualMachine"
     h.enable_virtualization_extensions = true
     #Time synchronization between Host OS and Quest OS (Windows 10 and RedHat OS)
     h.vm_integration_services = {time_synchronization: true}
     h.cpus = 2
     h.maxmemory = 4096
     h.memory = 4096
   end
   
end

Make Ansible script for provisoning VM with Redhat 7

now we create file playbook.yml in the same directory like Vagrantfile

---
- name: This is a hello-world example
  hosts: IP_Adress of our VM with Redhat
  tasks:
    - name: Create a file called '/tmp/testfile.txt' with the content 'hello world'.
      copy:
        content: hello worldn
        dest: /tmp/testfile.txt

This ansible script only create testfile.txt inside VM with RedHat OS.

Starting OS Redhat 7

What is bad, Vagrant can not handle networking with hyper-v. That means we have to set manually IP address inside VM or user DHCP server on Windows 10 which gives IP address of our VM.

Without setting IP address our VM not communicate with Vagrant and Ansible.

So lets asume we are in folder where Vagrantfile and playbook.yml exists and do folowing.

$vagrant up

Vagrant ask you which network interface should use. You should choose our early created “Vagrant_Switch”. Now Vagrant start to download and run our VM.

Because there is no connection between vagrant and running VM now we have to go in Hyper-v Manager in Windows 10 and login into our VM username and password is vagrant.

Note: Vagrant with provider Hyper-V can not create and configure network interface inside VM. Here you can see more information.

$sudo nmtui

After we set IP adress like this:
IP> 192.168.10.2
Gateway>192.168.10.3
DNS>8.8.8.8

save it by this

$vagrant reload

Vagrant restart the VM and now can connect to VM.

So for start shell script that subscribe and add repository inside VM we can use

$vagrant provision --provision-with shell

for running ansible script (playbook.yml) we can use

$vagrant provision --provision-with ansible

running ansible script directly from ansible

$ansible-playbook --private-key=.vagrant/machines/default/virtualbox/private_key -u vagrant -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml

troubleshooting

Troubleshooting


$vagrant up --debug
$

Problem with very slow internet connection of VM Guest (OS Redhat 7 ) – VM Host Windows 10 Enterprise (Build 1803)

Leave a Reply

Close Menu