0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WSL2でVagrant+ansibleの環境整備

Last updated at Posted at 2021-09-09

表題の件メモ。

やりたいこと

  • WSL2でansible+Vagrantでのサーバー構成環境をつくりたい。
  • 同一ディレクトリ内でvagrant upとansible-playbookコマンド実行したい。
    (vagrant.exeだと、ansibleはWSL2側、Vagrantfileはwindows側にしないといけなかった。)

Vagrantインストール手順(ざっくり)

0. 前提

  • WSL2(Ubuntu)がインストール済のWindows10

1. VirtualBoxをインストールする(Windows版でよい)

2. WSL2のaptでvagrantをインストールする

$ apt install vagrant

# apt installで入ったバージョン
$ vagrant --version
Vagrant 2.2.6

3. 環境変数設定

.bashrcに以下を追加

export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS=1
export VAGRANT_WSL_WINDOWS_ACCESS_USER=ユーザー名

4. virtualbox_WSL2プラグインインストール

3.までだけだと、vagrant up時にWarning: Connection refused. Retrying...が出てSSH接続できなかった。
プラグインをインストールしたら解決した。

$ vagrant plugin install virtualbox_WSL2

Installing the 'virtualbox_WSL2' plugin. This can take a few minutes...
Fetching vagrant-libvirt-0.5.3.gem
Fetching virtualbox_WSL2-0.1.3.gem
Parsing documentation for vagrant-libvirt-0.5.3
Installing ri documentation for vagrant-libvirt-0.5.3
Parsing documentation for virtualbox_WSL2-0.1.3
Installing ri documentation for virtualbox_WSL2-0.1.3
Done installing documentation for vagrant-libvirt, virtualbox_WSL2 after 1 seconds
Installed the plugin 'virtualbox_WSL2 (0.1.3)'!

5. Vagrantfile修正

デフォルトだと/vagrantがマウントできずにこけるようである。

$ vagrant up
Bringing machine 'hostname' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The host path of the shared folder is not supported from WSL. Host
path of the shared folder must be located on a file system with
DrvFs type. Host path: .

Vagrantfileに以下を追加する。(ホストの.とvmの/vagrantをsyncしない)

h.vm.synced_folder ".", "/vagrant", disabled: true

6. vagrant upする

できる。

ansibleインストール手順(ざっくり)

1. anyenvからのpyenvインストール

anyenv

$ cd ~
$ git clone https://github.com/riywo/anyenv ~/.anyenv
$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(anyenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l

pyenv

$ anyenv install pyenv

virtualenv

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

2. pythonインストールしてansibleのvirtualenv作る

$ pyenv install 3.9.7
$ pyenv virtualenv 3.9.7 ansible
$ pyenv versions
$ pyenv versions
  system
  3.9.7
  3.9.7/envs/ansible
  ansible

3. 対象ディレクトリでpythonバージョン指定

$ cd the_ansible_dir
$ pyenv local ansible
$ pyenv versions
  system
  3.9.7
  3.9.7/envs/ansible387
* ansible387 (set by /root/the_ansible_dir/.python-version)

$ cat .python-version
ansible

4. ansible-playbookとかansible-galaxyとかする

できる。

参考

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?