DockerじゃないMolecule!Windows Serverに

DockerじゃないMolecule開発
Colossus CloudによるPixabayからの画像

※この記事にはプロモーションが含まれています。

今回は、Moleculeを用いたWindows Serverのインフラテストを実施します。Moleculeを使えば、Ansibleと同じ構文でテストを書くことが可能です。

スポンサーリンク
スポンサーリンク

はじめに

MoleculeはAnsibleロールをテストするための、Chef社開発のTest Kichenと同じ性質のツールです。

一般的には、検査対象がLinuxで、Moleculeが自動的にDockerコンテナ作成・テスト・コンテナ破棄を行ってくれる、環境にやさしいツールの様です。が、今回はHyper-V上にインストールしたWindows Server 2019をテストしたいと思います。

環境作成

環境は、クライアント・サーバーとも以前紹介したAnsible AWXでIISを構築するで構築したものを使用します。クライアント側がWinRM接続設定されており、サーバー側にpython3とpywinrmがあれば問題ないはずです。

更にサーバー側に下記コマンドで、モジュールをインストールします。

sudo pip3 install molecule[lint]

Moleculeは、サーバーとWindowsの対象クライアントをWinRMで接続する仕様です。

Molecule構成

プロジェクト作成

Moleculeでテストを行うプロジェクトを、下記コマンドで作成します。ドライバには、Dockerではなく直接続したいので、delegatedを指定しています。

molecule init scenario --driver-name delegated

ファイル構成はこんな感じになります。

molecule/
└── default
    ├── INSTALL.rst
    ├── converge.yml
    ├── create.yml
    ├── destroy.yml
    ├── molecule.yml
    └── verify.yml

プロジェクト編集

まず、ターゲットの作成・破棄が必要ないため、create.ymlおよびdestroy.ymlをリネームまたは削除します。

次に、molecule.ymlは以下の様に接続情報を与えます。

---
dependency:
  name: galaxy
driver:
  name: delegated
platforms:
  - name: Windows1
provisioner:
  name: ansible
  inventory:
    hosts:
      all:
        children:
          Windows:
            hosts:
              Windows1:
                ansible_host: 172.26.220.10
            vars:
              ansible_connection: winrm
              ansible_user: ansible
              ansible_password: P@ssw0rd
              ansible_port: 5986
              ansible_winrm_server_cert_validation: ignore
verifier:
  name: ansible

最後に、verify.ymlにテスト内容を記述します。今回は、ポート確認・ファイル確認・レスポンス確認を実施しています。

---
# This is an example playbook to execute Ansible tests.

- name: Verify
  hosts: all
#  gather_facts: false
  tasks:
    - name: open port
      win_wait_for:
        port: 80
        state: started
        msg: 'port is dead'
        timeout: 5

    - name: exist index.html
      win_stat:
        path: C:\inetpub\wwwroot\iisstart.htm
      register: file_info
    - name: check to exist index.html
      assert: 
        that:
          - file_info.stat.exists

    - name: get http response
      win_uri:
        url: http://localhost:80/
        return_content: yes
      register: http_output
    - name: check to get http response
      fail:
        msg: 'service is not correct'
      when: "'IIS Windows Server' not in http_output.content"

ansibleのWindowsモジュール詳細は、Windows modules — Ansible Documentationを参照してください。

実行

それでは実行します。テストは、moleculeフォルダの上階層で下記コマンドを実行します。

molecule test

結果は、以下の様に表示されました。成功です。

...
PLAY [Verify] *****************************************************************************************************

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

TASK [open port] **************************************************************************************************
ok: [Windows1]

TASK [exist index.html] *******************************************************************************************
ok: [Windows1]

TASK [check to exist index.html] **********************************************************************************
ok: [Windows1] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [get http response] ******************************************************************************************
ok: [Windows1]

TASK [check to get http response] *********************************************************************************
skipping: [Windows1]

PLAY RECAP ********************************************************************************************************
Windows1                   : ok=5    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

まとめ

Moleculeを使用すれば、ansible構文でテストを実施可能です。ただ、ansible testが初めてで勘違いしていたのですが、用意されたモジュールを適当に実行すると普通のtaskと同じくインストールや変更が走ります。テストでできるのはあくまで、値や状態の確認だけです。

この点は、以前InSpecでIISにインフラ構成テストで紹介したInSpecの方が色々とテストしやすいモジュールが用意されていて私は好みかもしれません。もう少しMoleculeを使い込んでみたいと思います。

以上、DockerじゃないWindows ServerでのMolecule使用手順の紹介でした。

スポンサーリンク
スポンサーリンク
ハイクラス求人 ビズリーチ
スポンサーリンク
開発
Tech WalkIt

コメント