2
3

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 5 years have passed since last update.

Windows(WSL)上のAnsibleでVMを管理する

Last updated at Posted at 2018-11-09

WSLからAnsibleを実行してVMを管理する

uname -aってコマンド実行する際は「君の名は?」って指さし呼称しますよね。
さて、この記事はWindows Subsystem for Linux (WSL) 上にAnsibleインストールして実行環境として使ったのでそのメモ書きです。管理するとタイトル記載していますが、OS情報取得するまで。

期待する効果

・WindowsPC上にVMを立ち上げて検証することも多々あるので、WindowsからAnsibleを実行できると管理の手間が部分的に省略できるはず
・WindowsPCをサーバ保守作業時の作業端末にすることを多くみかけるので、一時的に接続した作業用の端末からAnsibleを実行できると嬉しいこともあるはず

検証した環境

今回使用したソフトウェアとバージョンは以下の通りです。

Windows 10 Pro
Ubuntu 16.04
ansible 2.7.1
Python 2.7.12
Oracle VirtualBox 5.2.16

下図のような構成で、OracleVirtualBox上のLinuxにコマンドを送信します。
AnsibleOnWSLpng.png

WSL上にAnsbileをインストール

WSLを起動しましょう。プロンプトが表示されます。

ユーザ名@ホスト名: $

必要なパッケージを導入

Ansibleはpython上で動作しますので、python/python-devをインストールします。
今回はsshパスワード指定してサーバ接続するので、sshpassをインストールします。

python

今回は以下のPythonバージョンを使用

$ python --version
Python 2.7.12

pythonインストールされてなければ、インストールしましょうね。

$ sudo apt install python

python-dev

python-devもインストールしましょうね。

$ sudo apt list python-dev -a
Listing... Done
python-dev/xenial-updates,now 2.7.12-1~16.04 amd64 [installed]
python-dev/xenial 2.7.11-1 amd64

入ってた。
入ってなかったらinstallしましょう。

$ sudo apt install python-dev

sshpass

sshpassをインストールしましょうね。

$ sudo apt list sshpass
Listing... Done
sshpass/xenial 1.05-1 amd64

installedはありませんね。
sshpassをインストールします。

$ sudo apt install sshpass
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  sshpass
0 upgraded, 1 newly installed, 0 to remove and 33 not upgraded.
(中略)
Setting up sshpass (1.05-1) .

Ansibleをインストールする

準備ができたら、Ansibleをインストールします。

$ sudo pip install ansible
(中略)
Successfully installed MarkupSafe-1.0 PyYAML-3.13 ansible-2.7.1 asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.3.1 enum34-1.1.6 idna-2.7 ipaddress-1.0.22 jinja2-2.10 paramiko-2.4.2 pyasn1-0.4.4 pycparser-2.19 pynacl-1.3.0 six-1.11.0

installできました。確認しましょう。

$ ansible --version
ansible 2.7.1
  config file = None
(中略)
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]

検証する

では、Ansibleを使ってVM上のLinuxの情報を確認するplaybookをたたいてみます。

あらかじめ用意しておいたVMを起動

Oracle Virtual Boxで構築しておいたLinuxを起動します。

起動したらログインしてuname -aを実行しましょう。

# uname -a
Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

今回はこのコマンドをAnsbileから実行します。

検証用Ansible-Playbookを用意しよう

では、WSL上でPlaybookを用意します。
まずは、Inventoryを用意しましょうね。

$ more hosts
test ansible_ssh_host=localhost ansible_port=2222 ansible_ssh_user=root ansible_ssh_pass=P@ssw0rd

ホスト名やらポートやらユーザ名やらパスワードは環境に合わせてください。

今回用意するPlaybookは、"uname"コマンドでLinuxのバージョンを取得して、ansible-playbookコマンドの結果に出力させる内容です。

$ more hello.yml
- hosts: test
  tasks:
    - shell: uname -a
      register: v
    - debug: msg="{{v}}"

実行してみましょう。

$ ansible-playbook -i hosts hello.yml

PLAY [test] ************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [test]

TASK [shell] ***********************************************************************************************************
changed: [test]

TASK [debug] ***********************************************************************************************************
ok: [test] => {
    "msg": {
        "changed": true,
        "cmd": "uname -a",
        "delta": "0:00:00.006087",
        "end": "2018-11-09 22:24:12.177135",
        "failed": false,
        "rc": 0,
        "start": "2018-11-09 22:24:12.171048",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux",
        "stdout_lines": [
            "Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux"
        ]
    }
}

PLAY RECAP *************************************************************************************************************
test                       : ok=3    changed=1    unreachable=0    failed=0

正常に動作しました。

まとめ

Windows(WSL)からAnsible Playbookを実行しOracle VirtualBox上のOSの情報を確認しました。特に問題なしですね。

では、快適なAnsibleライフを!

参照

「debug - Print statements during execution」
https://docs.ansible.com/ansible/2.6/modules/debug_module.html
「Ansible:さらにPlaybookをきわめる」
https://thinkit.co.jp/article/9990
「ansible sshpass error」
https://qiita.com/park-jh/items/d14cb20c9dfa0e2628d5
「ansible | シェルの標準出力を ansible-playbook コマンドで出力させる」
https://qiita.com/YumaInaura/items/5bee47677ec903551bb3

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?