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

igotoo·2021년 6월 5일
0

개요

윈도우즈 호스트 환경에서 WSL2에 Vagrant/Ansible을 설치하여 호스트에 설치된 virtualbox을 활용하여 Ansible 실습/테스트 환경을 구성하고 환경구성 검증을 위해 Drupal provisioning 함

환경 구성

  • 호스트 Windows 10에는 WSL2, VagrantVirtualBox 설치되어 있어야 하고 WSL2에 Ubuntu, Vagrant와 Ansible 설치되어 있어야 함

vagrant 버전 통일

  • 호스트 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 found in the PATH variable. 
    This is an error. Please verify
    this 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"

환경변수

  • 환경 변수 설정

    Other useful WSL related environment variables:
    VAGRANT_WSL_WINDOWS_ACCESS_USER - Override current Windows username
    VAGRANT_WSL_DISABLE_VAGRANT_HOME - Do not modify the VAGRANT_HOME variable
    VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH - Custom Windows system home path
    출처 : https://www.vagrantup.com/docs/other/wsl

  • 설정 내용
    # for ansible + vagrant
    export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
    export PATH="$PATH:/mnt/d/virtualbox"
    export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/d/Ansible-Handson/"

환경구성 검증

  • 환경구성이 제대로 되었는지 테스트 하기 위해 Drupal 서비스를 ansible_local provision을 통해 Drupal 설치

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/plugins/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.
  • Update 2022.10.11 provisoner를 ansible을 사용해도 에러없이 provision 됨

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

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

provisioning

> 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간 통신 가능

이미지

추가

ssh 오류

  • 새로운 랩탑에 상기 환경을 구성하고 구성점검을 위해 간단한 vm 생성시 ssh 접속에러 발생
  • Window defender(방화벽) ssh 접근을 차단이 원인
  • 방화벽 차단 해제(자세한 사용법을 몰라 방화벽 설정 해제)

ansible 버전 오류

  • ansible 2.11.2 , Vagrant 2.2.16 환경에서 Vagrant gathered an unknown Ansible version 오류 발생
Vagrant gathered an unknown Ansible version:

ansible [core 2.11.2]
  config file = None
  configured module search path = ['/home/igotoo/.ansible/plugins/modules', '/usr/share/ansible/plugins/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 = /home/igotoo/.local/bin/ansible
  python version = 3.8.5 (default, May 27 2021, 13:30:53) [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
  • 버전 미 인식으로 ansible 1.8 버전으로 실행하여 provision이 더 이상 진행 되지 않음
  • 원인 : 설치된 ansible이 vagrant 미호환 또는 아직 미지원 버전

    If Vagrant is not able to detect any supported Ansible version, it will fall back on the compatibility mode "1.8" with a warning.

  • 해결 : 에러메세지에 나오는 url의 내용을 참고 Vagrantfile에 ansible.compatibility_mode = "2.0" 추가
# Ansible provisioner.
  config.vm.provision :ansible do |ansible|
    ansible.compatibility_mode = "2.0"
    ansible.playbook = "playbook.yml"
    ansible.become = true
  end
profile
개발자가 되고 싶은 오래된 아키텍트

0개의 댓글