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.


  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
Right-click and copy the link for the 64-bit Debian package.
$sudo dpkg -i vagrant_2.2.6_x86_64.deb
$export PATH="$PATH:/c/Windows/System32/WindowsPowerShell/v1.0"
$export PATH=$PATH:/c/Windows/System32


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"
//Set IP address for "Vagrant_Switch"
$New-NetIPAddress -IPAddress -PrefixLength 24 -InterfaceIndex number_findex
//Create NAT for "Vagrant_Switch"
$New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix
//list of Nat networks
//Remove all NAT networks
$Get-NetNat | Remove-NetNat
//OR remove only my MyNATnetwork
$Remove-NetNat MyNATnetwork
//list of switch

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


# 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

$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

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Every Vagrant development environment requires a box. You can search for
  # boxes at
  # List of installed vagrant boxess $vagrant box list -i
  # Update of installed box $vagrant box update generic/rhel8 = "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
      run_remote "sudo subscription-manager unregister"
      puts "Something went wrong, please remove the machine manually from"

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

  #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

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
    - name: Create a file called '/tmp/testfile.txt' with the content 'hello world'.
        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:

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



$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