Windows에서 WSL2/Vagrant/VBox로 Ansible 실습

개요

WSL2에 Vagrant를 설치하여 호스트에 설치된 virtualbox을 활용하여 VM 생성/구성/관리 가능

  • 윈도우에는 ansible control 서버를 설치 할 수 없어 ansible provisioner를 사용할 수 없음

vagrant에서 ansible/ansible_local 지원

  • ansible, where Ansible is executed on the Vagrant host
  • ansible_local, where Ansible is executed on the Vagrant guest
  • ansible provioner는 호스트에 asnible 설치, ansible_local는 guest에 ansible 설치 따라서 window host에서는 ansible_local 사용

참고

주요 참고 : Appendix A - Vagrant with VirtualBox within WSL2 to run ansible-for-devops VMs #291

Common Ansible Options - Provisioning | Vagrant by HashiCorp

Ansible - Short Introduction | Vagrant by HashiCorp

사전 설치

호스트 vagrant(windows 10)와 WSL2 vagrant 버전 통일

NOTE: When Vagrant is installed on the Windows system the version installed within the Linux distribution must match.'

에러 메세지

The provider 'virtualbox' that was requested to back the machine 'default' is reporting that it isn't usable on this system. The reason is shown below:
			
VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

WSL2 vagrant 삭제후 재 설치

> wget https://releases.hashicorp.com/vagrant/2.2.16/vagrant_2.2.16_linux_amd64.zip
> unzip vagrant_2.2.16_linux_amd64.zip
> sudo mv ./vagrant /usr/local/bin/vagrant

basdtar 설치

vagrant가 아카이브 파일 읽고/쓰기 위해 필요, 미 설치로 다음 에러 발생

INFO interface: error: The executable 'bsdtar' Vagrant is trying to run was not
found in the PATH variable. This is an error. Please verify
this software is installed and on the path.
The executable 'bsdtar' Vagrant is trying to run was not
found in the PATH variable. This is an error. Please verify
this software is installed and on the path.
INFO interface: Machine: error-exit ["Vagrant::Errors::CommandUnavailable", "The executable 'bsdtar' Vagrant is trying to run was not\nfound in the PATH variable. This is an error. Please verify\nthis software is installed and on the path."]

설치

> sudo apt install libarchive-tools

ssh 오류 해결

ssh 오류 해결 plugin 설치: vagrant plugin install virtualbox_WSL2

...
default: Warning: Connection refused. Retrying...
default: Warning: Connection refused. Retrying...
default: Warning: Connection refused. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

Private network not getting set up running under WSL2 (same Vagrantfile fine under Windows Vagrant) · Issue #11716 · hashicorp/vagrant

wsl.conf 생성 : /etc/wsl.conf

  • private key 권한 변경 시 리눅스 파일 시스템 권한 설정 기능 필요

wsl.conf의 역할 : WSL 실행시 자동 구성 수행

Automatically configure functionality in WSL that will be applied every time you launch the subsystem using wsl.conf. This includes automount options and network configuration.
wsl.conf is located in each Linux distribution in /etc/wsl.conf.

automout / metata

metadata Whether metadata is added to Windows files to support Linux system permissions

설정 내용

> cat /etc/wsl.conf
[automount]
options = "metadata"

환경 변수 설정

# for ansible + vagrant
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export PATH="$PATH:/mnt/d/virtualbox"
export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATHPATH="/mnt/d/Ansible-Handson/"

Manage Linux Distributions

저자 github clone

  • 명령어 : git clone git@github.com:geerlingguy/ansible-for-devops.git
  • 위치 : /mnt/d/Ansible-Handson/ans-for-devops

vm 생성

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

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "geerlingguy/ubuntu2004"
  config.vm.network :private_network, ip: "192.168.88.8"
  config.vm.hostname = "drupal.test"
  config.ssh.insert_key = false

  config.vm.provider :virtualbox do |v|
    v.memory = 2048
  end
  
  #config.vm.synced_folder '.', '/vagrant', disabled: true
  config.vm.synced_folder '.', '/vagrant'

  # Ansible provisioning.
  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "provisioning/playbook.yml"
  end
end

config.vm.provision "ansible_local"

  • config.vm.provision = ansible로 할경우 호스트 즉 windows10에 ansible을 설치 하라는 에러가 발생함. Windows10에서는 ansible을 설치 할 수 없으므로 ansible_local로 provision 해야 함
ansible [core 2.11.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/igotoo/.ansible/plugins/modules', '/usr/share/ansible/plugin
                                                                                                     ns/modules']
  ansible python module location = /home/igotoo/.local/lib/python3.8/site-packages/ansible
  ansible collection location = /home/igotoo/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
  jinja version = 2.10.1
  libyaml = True

and falls back on the compatibility mode '1.8'.

Alternatively, the compatibility mode can be specified in your Vagrantfile:
https://www.vagrantup.com/docs/provisioning/ansible_common.html#compatibility_mode

    default: Running ansible-playbook...
The Ansible software could not be found! Please verify
that Ansible is correctly installed on your host system.

If you haven't installed Ansible yet, please install Ansible
on your host system. Vagrant can't do this for you in a safe and
automated way.
Please check https://docs.ansible.com for more information.

synced folder : config.vm.synced_folder '.', '/vagrant'

  • ansible_local provision을 할 경우 playbook이 호스와 게스트 공유폴더를 통해 공유 되어야 함
  • 예전 Episode에서슨 synced folder bug로 disable 권고 → bug 해결 : https://github.com/hashicorp/vagrant/pull/12056

Drupal 설치

> vagrant provision
==> default: Running provisioner: ansible_local...
    default: Installing Ansible...
    default: Running ansible-playbook...
  • 사이트 접속 : http://192.168.88.8/
  • vm 생성시 지정했던 IP 192.168.88.8로 웹브라우저를 통해 접속 가능
  • private ntework 즉 virtual host-only network로 호스트 - 게스트 vm간 통신 가능
igotoo

igotoo