WSLにAnsibleをインストール

1. WSL のインストール

  1. コントロールパネルの プログラムと機能 を開く

  2. Windowsの機能の有効化または無効化 を選択

  3. Windows Subsystem for Linux にチェックを入れる

  4. OK を選択

  5. Windowsを再起動する

785bf382
図 1. プログラムと機能

1.1. Linuxのインストール

Microsoft Storelinux で検索する。

ea02ef4b
図 2. Microsoft StoreでLinux検索

好みのOSを選択してインストールする。

Ubuntu を選んだ。

9ee2ac13
図 3. Ubuntuインストール

インストールが完了したら起動する。最初の起動時はユーザ作成をすることになるので、ユーザー名とパスワードを設定する。

e8d52f94
図 4. アカウント設定

パッケージをアップデートしておく。

$ sudo apt-get update

/mnc/cWindowsCドライブ にアクセス出来る。

~$ l /mnt/c/
$Recycle.Bin/                   PerfLogs/                       bootmgr
$SysReset/                      Program Files/                  data/
$WINRE_BACKUP_PARTITION.MARKER  Program Files (x86)/            hiberfil.sys
Config.Msi/                     ProgramData/                    pagefile.sys
Documents and Settings          Recovery/                       projects/
HashiCorp/                      System Volume Information/      swapfile.sys
Intel/                          Users/                          tools/
Microsoft/                      Windows/
OneDriveTemp/                   applications/

1.2. WSLからVagrantの仮想マシンへのアクセス

何の設定もなくアクセス出来るのか実験してみる。

Vagrant で仮想PC起動する。

$ vagrant up
$ vagrant ssh
[vagrant@sandbox ~]$ ip addr | grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
    inet6 fe80::5054:ff:fe8a:fee6/64 scope link
    inet 192.168.33.250/24 brd 192.168.33.255 scope global noprefixroute eth1
    inet6 fe80::a00:27ff:fe7e:166a/64 scope link

秘密鍵の場所も確認しておく。

$ vagrant ssh-config | grep IdentityFile
  IdentityFile C:/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key
...snip...

WSLのコンソールから

$ ping -c4 192.168.33.250
PING 192.168.33.250 (192.168.33.250) 56(84) bytes of data.
64 bytes from 192.168.33.250: icmp_seq=1 ttl=64 time=0.989 ms
64 bytes from 192.168.33.250: icmp_seq=2 ttl=64 time=0.815 ms
64 bytes from 192.168.33.250: icmp_seq=3 ttl=64 time=1.19 ms
64 bytes from 192.168.33.250: icmp_seq=4 ttl=64 time=0.563 ms

--- 192.168.33.250 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.563/0.891/1.197/0.232 ms
$ ssh -l vagrant -i "/mnt/c/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key" 192.168.33.250
The authenticity of host '192.168.33.250 (192.168.33.250)' can't be established.
ECDSA key fingerprint is SHA256:C6MhDDkvsIoMcYLVzrZfQ2UJyMyzgusQXWq5iECRSU4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.33.250' (ECDSA) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for '/mnt/c/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/mnt/c/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key": bad permissions
vagrant@192.168.33.250: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

秘密鍵がWindowsのファイルシステム上(`/mnt/c/`配下)にあるのでパーミッションが`0777`になってしまうのでエラーとなっている。

Windowsのファイルシステムについてもパーミッションを設定できるようなのだが、デフォルトでははその機能が有効になっていない。

有効にするには /etc/wsl.conf を下記の内容で作成する。

$ sudo vim /etc/wsl.conf
/etc/wsl.conf
[automount]
options = "metadata"

設定後WSLを再起動する。

再起動したらWSLのコンソールでパーミッションを設定する。

$ chmod 600 /mnt/c/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key
$ ll /mnt/c/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key
-rw------- 1 iwase iwase 1.7K Dec  9 01:23 /mnt/c/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key
$ ssh -l vagrant -i "/mnt/c/projects/vagrant-vm/sandbox/.vagrant/machines/default/virtualbox/private_key" 192.168.33.250
Last login: Sun Dec  8 16:37:14 2019 from 10.0.2.2
[vagrant@sandbox ~]$ uname -a
Linux sandbox.vm 3.10.0-957.12.2.el7.x86_64 #1 SMP Tue May 14 21:24:32 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

WSLから Vagrant の仮想PCへのsshログインに成功した。

2. Ansibleインストール

ここまでは、前座でここからが本番。

$ uname -a
Linux DESKTOP-ORVT5JU 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux

インストールの方法は公式サイトに従って行う。

$ sudo apt update
$ sudo apt install software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  python3-software-properties
The following packages will be upgraded:
  python3-software-properties software-properties-common
2 upgraded, 0 newly installed, 0 to remove and 126 not upgraded.
Need to get 33.6 kB of archives.
After this operation, 13.3 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 software-properties-common all 0.96.24.32.11 [9996 B]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-software-properties all 0.96.24.32.11 [23.6 kB]
Fetched 33.6 kB in 1s (34.8 kB/s)
(Reading database ... 28645 files and directories currently installed.)
Preparing to unpack .../software-properties-common_0.96.24.32.11_all.deb ...
Unpacking software-properties-common (0.96.24.32.11) over (0.96.24.32.7) ...
Preparing to unpack .../python3-software-properties_0.96.24.32.11_all.deb ...
Unpacking python3-software-properties (0.96.24.32.11) over (0.96.24.32.7) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up python3-software-properties (0.96.24.32.11) ...
Processing triggers for dbus (1.12.2-1ubuntu1) ...
Setting up software-properties-common (0.96.24.32.11) ...
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Get:2 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic InRelease [15.9 kB]
Hit:3 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Get:6 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main amd64 Packages [692 B]
Get:7 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main Translation-en [472 B]
Fetched 17.0 kB in 3s (5045 B/s)
Reading package lists... Done
$ sudo apt install ansible
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libssl1.1 python python-asn1crypto python-cffi-backend
  python-crypto python-cryptography python-enum34 python-httplib2 python-idna python-ipaddress python-jinja2
  python-markupsafe python-minimal python-paramiko python-pkg-resources python-pyasn1 python-setuptools python-six
  python-yaml python2.7 python2.7-minimal sshpass
Suggested packages:
  python-doc python-tk python-crypto-doc python-cryptography-doc python-cryptography-vectors python-enum34-doc
  python-jinja2-doc python-gssapi python-setuptools-doc python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
  ansible libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-asn1crypto python-cffi-backend
  python-crypto python-cryptography python-enum34 python-httplib2 python-idna python-ipaddress python-jinja2
  python-markupsafe python-minimal python-paramiko python-pkg-resources python-pyasn1 python-setuptools python-six
  python-yaml python2.7 python2.7-minimal sshpass
The following packages will be upgraded:
  libssl1.1
1 upgraded, 25 newly installed, 0 to remove and 125 not upgraded.
Need to get 12.6 MB of archives.
After this operation, 83.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython2.7-minimal amd64 2.7.15-4ubuntu4~18.04.2 [336 kB]
Get:2 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main amd64 ansible all 2.9.2-1ppa~bionic [5778 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python2.7-minimal amd64 2.7.15-4ubuntu4~18.04.2 [1296 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-minimal amd64 2.7.15~rc1-1 [28.1 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl1.1 amd64 1.1.1-1ubuntu2.1~18.04.5 [1300 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython2.7-stdlib amd64 2.7.15-4ubuntu4~18.04.2 [1916 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python2.7 amd64 2.7.15-4ubuntu4~18.04.2 [239 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/main amd64 libpython-stdlib amd64 2.7.15~rc1-1 [7620 B]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 python amd64 2.7.15~rc1-1 [140 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-markupsafe amd64 1.0-1build1 [13.0 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-jinja2 all 2.10-1ubuntu0.18.04.1 [94.8 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-yaml amd64 3.12-1build2 [115 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-asn1crypto all 0.24.0-1 [72.7 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-cffi-backend amd64 1.11.5-1 [63.4 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-enum34 all 1.1.6-2 [34.8 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-idna all 2.6-1 [32.4 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-ipaddress all 1.0.17-1 [18.2 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-six all 1.11.0-2 [11.3 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-cryptography amd64 2.1.4-1ubuntu1.3 [221 kB]
Get:20 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-pyasn1 all 0.4.2-3 [46.7 kB]
Get:21 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-paramiko all 2.0.0-1ubuntu1.2 [110 kB]
Get:22 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-httplib2 all 0.9.2+dfsg-1ubuntu0.1 [34.7 kB]
Get:23 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-crypto amd64 2.6.1-8ubuntu2 [244 kB]
Get:24 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-pkg-resources all 39.0.1-2 [128 kB]
Get:25 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-setuptools all 39.0.1-2 [329 kB]
Get:26 http://archive.ubuntu.com/ubuntu bionic/universe amd64 sshpass amd64 1.06-1 [10.5 kB]
Fetched 12.6 MB in 27s (468 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libpython2.7-minimal:amd64.
(Reading database ... 28647 files and directories currently installed.)
Preparing to unpack .../0-libpython2.7-minimal_2.7.15-4ubuntu4~18.04.2_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.15-4ubuntu4~18.04.2) ...
Selecting previously unselected package python2.7-minimal.
Preparing to unpack .../1-python2.7-minimal_2.7.15-4ubuntu4~18.04.2_amd64.deb ...
Unpacking python2.7-minimal (2.7.15-4ubuntu4~18.04.2) ...
Selecting previously unselected package python-minimal.
Preparing to unpack .../2-python-minimal_2.7.15~rc1-1_amd64.deb ...
Unpacking python-minimal (2.7.15~rc1-1) ...
Preparing to unpack .../3-libssl1.1_1.1.1-1ubuntu2.1~18.04.5_amd64.deb ...
Unpacking libssl1.1:amd64 (1.1.1-1ubuntu2.1~18.04.5) over (1.1.0g-2ubuntu4.3) ...
Selecting previously unselected package libpython2.7-stdlib:amd64.
Preparing to unpack .../4-libpython2.7-stdlib_2.7.15-4ubuntu4~18.04.2_amd64.deb ...
Unpacking libpython2.7-stdlib:amd64 (2.7.15-4ubuntu4~18.04.2) ...
Selecting previously unselected package python2.7.
Preparing to unpack .../5-python2.7_2.7.15-4ubuntu4~18.04.2_amd64.deb ...
Unpacking python2.7 (2.7.15-4ubuntu4~18.04.2) ...
Selecting previously unselected package libpython-stdlib:amd64.
Preparing to unpack .../6-libpython-stdlib_2.7.15~rc1-1_amd64.deb ...
Unpacking libpython-stdlib:amd64 (2.7.15~rc1-1) ...
Setting up libpython2.7-minimal:amd64 (2.7.15-4ubuntu4~18.04.2) ...
Setting up python2.7-minimal (2.7.15-4ubuntu4~18.04.2) ...
Linking and byte-compiling packages for runtime python2.7...
Setting up python-minimal (2.7.15~rc1-1) ...
Selecting previously unselected package python.
(Reading database ... 29396 files and directories currently installed.)
Preparing to unpack .../00-python_2.7.15~rc1-1_amd64.deb ...
Unpacking python (2.7.15~rc1-1) ...
Selecting previously unselected package python-markupsafe.
Preparing to unpack .../01-python-markupsafe_1.0-1build1_amd64.deb ...
Unpacking python-markupsafe (1.0-1build1) ...
Selecting previously unselected package python-jinja2.
Preparing to unpack .../02-python-jinja2_2.10-1ubuntu0.18.04.1_all.deb ...
Unpacking python-jinja2 (2.10-1ubuntu0.18.04.1) ...
Selecting previously unselected package python-yaml.
Preparing to unpack .../03-python-yaml_3.12-1build2_amd64.deb ...
Unpacking python-yaml (3.12-1build2) ...
Selecting previously unselected package python-asn1crypto.
Preparing to unpack .../04-python-asn1crypto_0.24.0-1_all.deb ...
Unpacking python-asn1crypto (0.24.0-1) ...
Selecting previously unselected package python-cffi-backend.
Preparing to unpack .../05-python-cffi-backend_1.11.5-1_amd64.deb ...
Unpacking python-cffi-backend (1.11.5-1) ...
Selecting previously unselected package python-enum34.
Preparing to unpack .../06-python-enum34_1.1.6-2_all.deb ...
Unpacking python-enum34 (1.1.6-2) ...
Selecting previously unselected package python-idna.
Preparing to unpack .../07-python-idna_2.6-1_all.deb ...
Unpacking python-idna (2.6-1) ...
Selecting previously unselected package python-ipaddress.
Preparing to unpack .../08-python-ipaddress_1.0.17-1_all.deb ...
Unpacking python-ipaddress (1.0.17-1) ...
Selecting previously unselected package python-six.
Preparing to unpack .../09-python-six_1.11.0-2_all.deb ...
Unpacking python-six (1.11.0-2) ...
Selecting previously unselected package python-cryptography.
Preparing to unpack .../10-python-cryptography_2.1.4-1ubuntu1.3_amd64.deb ...
Unpacking python-cryptography (2.1.4-1ubuntu1.3) ...
Selecting previously unselected package python-pyasn1.
Preparing to unpack .../11-python-pyasn1_0.4.2-3_all.deb ...
Unpacking python-pyasn1 (0.4.2-3) ...
Selecting previously unselected package python-paramiko.
Preparing to unpack .../12-python-paramiko_2.0.0-1ubuntu1.2_all.deb ...
Unpacking python-paramiko (2.0.0-1ubuntu1.2) ...
Selecting previously unselected package python-httplib2.
Preparing to unpack .../13-python-httplib2_0.9.2+dfsg-1ubuntu0.1_all.deb ...
Unpacking python-httplib2 (0.9.2+dfsg-1ubuntu0.1) ...
Selecting previously unselected package python-crypto.
Preparing to unpack .../14-python-crypto_2.6.1-8ubuntu2_amd64.deb ...
Unpacking python-crypto (2.6.1-8ubuntu2) ...
Selecting previously unselected package python-pkg-resources.
Preparing to unpack .../15-python-pkg-resources_39.0.1-2_all.deb ...
Unpacking python-pkg-resources (39.0.1-2) ...
Selecting previously unselected package python-setuptools.
Preparing to unpack .../16-python-setuptools_39.0.1-2_all.deb ...
Unpacking python-setuptools (39.0.1-2) ...
Selecting previously unselected package sshpass.
Preparing to unpack .../17-sshpass_1.06-1_amd64.deb ...
Unpacking sshpass (1.06-1) ...
Selecting previously unselected package ansible.
Preparing to unpack .../18-ansible_2.9.2-1ppa~bionic_all.deb ...
Unpacking ansible (2.9.2-1ppa~bionic) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Setting up sshpass (1.06-1) ...
Setting up libssl1.1:amd64 (1.1.1-1ubuntu2.1~18.04.5) ...
Checking for services that may need to be restarted...done.
Checking for services that may need to be restarted...done.
Checking init scripts...

Restarting services possibly affected by the upgrade:
invoke-rc.d: could not determine current runlevel

Services restarted successfully.

Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up libpython2.7-stdlib:amd64 (2.7.15-4ubuntu4~18.04.2) ...
Setting up python2.7 (2.7.15-4ubuntu4~18.04.2) ...
Setting up libpython-stdlib:amd64 (2.7.15~rc1-1) ...
Setting up python (2.7.15~rc1-1) ...
Setting up python-idna (2.6-1) ...
Setting up python-yaml (3.12-1build2) ...
Setting up python-asn1crypto (0.24.0-1) ...
Setting up python-crypto (2.6.1-8ubuntu2) ...
Setting up python-pyasn1 (0.4.2-3) ...
Setting up python-pkg-resources (39.0.1-2) ...
Setting up python-markupsafe (1.0-1build1) ...
Setting up python-httplib2 (0.9.2+dfsg-1ubuntu0.1) ...
Setting up python-cffi-backend (1.11.5-1) ...
Setting up python-six (1.11.0-2) ...
Setting up python-enum34 (1.1.6-2) ...
Setting up python-ipaddress (1.0.17-1) ...
Setting up python-setuptools (39.0.1-2) ...
Setting up python-jinja2 (2.10-1ubuntu0.18.04.1) ...
Setting up python-cryptography (2.1.4-1ubuntu1.3) ...
Setting up python-paramiko (2.0.0-1ubuntu1.2) ...
Setting up ansible (2.9.2-1ppa~bionic) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...

※途中2回選択肢を訪ねられる場面(スクリーンショット忘れた)があったが、デフォルトの状態のまま進める。

$ ansible --version
ansible 2.9.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/siwa/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15+ (default, Oct  7 2019, 17:39:04) [GCC 7.4.0]
$ ll /etc/ansible/
total 24K
-rw-r--r-- 1 root root  20K Dec  5 08:10 ansible.cfg
-rw-r--r-- 1 root root 1016 Dec  5 08:10 hosts
drwxr-xr-x 1 root root  512 Dec  5 08:11 roles/

とりあえずここまで