使用WSL2进行Vagrant+ansible的环境配置

标题的备忘录内容。

我想做的事情。

    • WSL2でansible+Vagrantでのサーバー構成環境をつくりたい。

 

    • 同一ディレクトリ内でvagrant upとansible-playbookコマンド実行したい。

 

    (vagrant.exeだと、ansibleはWSL2側、Vagrantfileはwindows側にしないといけなかった。)

安装Vagrant的步骤(简化)

前提是一个条件或假设,在某个情况下需要满足或成立的基础。

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

安装VirtualBox(Windows版本)。

使用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插件

只是到目前为止,在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中添加以下内容(不将主机的.和虚拟机的/vagrant进行同步)。

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

6. 运行vagrant up

可以。

安装Ansible的步骤(简略)

从anyenv安装pyenv

任何环境

$ 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 可以原生地在中文中有以下一种选项的重新表述:
1. Python版本管理工具 pyenv.

$ anyenv install pyenv

虚拟环境

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

2. 安装Python并创建Ansible的虚拟环境

$ 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

执行ansible-playbook或ansible-galaxy之类的操作。

能够做到。

请参考

    • https://zenn.dev/lapi/articles/2021-06-10-vagrant_wsl2

 

    • https://blog.fuga.jp/?p=2179

 

    https://www.vagrantup.com/docs/other/wsl
bannerAds