AnsibleによるIISのセットアップを検証する


1. はじめに

AnsibleにはWindows用のモジュールも色々用意されていますが、
IISのセットアップについて、何ができるのかを検証したいと思います。

2. 環境

AWS上に以下のインスタンスを構築。
・Ansibleを実行するサーバ (Amazon Linux 2)
・Windows Server 2019

3. 設定手順

Windows Serverの設定

Ansibleサーバから通信ができるようにWindows Serverを設定します。

WinRMの設定

WinRMとは?

WinRM は、 Windows が別のサーバーとリモート通信するために使用する管理プロトコルです。HTTP/HTTPS を介して通信する SOAP ベースのプロトコルであり、 最近の全 Windows オペレーティングシステムに含まれています。

公式の手順にあるスクリプトを使用します。Powershellで以下を実行します。

PS> $url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
PS> $file = "$env:temp\ConfigureRemotingForAnsible.ps1"
PS> (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
PS> powershell.exe -ExecutionPolicy ByPass -File $file
Self-signed SSL certificate generated; thumbprint: ****************************************

wxf                 : http://schemas.xmlsoap.org/ws/2004/09/transfer
a                   : http://schemas.xmlsoap.org/ws/2004/08/addressing
w                   : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
lang                : en-US
Address             : http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
ReferenceParameters : ReferenceParameters

Ok.

フォルダの作成

後の手順で使用するフォルダを作成します。

IIS用物理フォルダ:C:\inetpub\testsite
IIS用ウェブアプリケーション物理フォルダ:C:\apps\testapp
IIS用ログ保管フォルダ:C:\sites\logs
IIS用仮想ディレクトリフォルダ:C:\virtualdirectory\testdir

「C:\inetpub\testsite」配下にindex.htmlを置いておきます。
最後にブラウザから接続確認を実施します。

Linuxサーバの設定

続いて、Linuxサーバの設定を実施します。

各種インストール

以下の2つをインストールします。今回は2020/12時点の最新バージョンを指定してインストールしています。
pywinrmはLinuxからWindowsのコマンドをリモート実行するために必要です。

$ pip install "ansible==2.10"
$ pip install "pywinrm==0.4.1"

inventoryファイルの作成

次に、Ansibleのinventoryファイルを作成します。

inventory
[targets]
test_windows ansible_host=10.0.1.100

[targets:vars]
ansible_user=Administrator
ansible_password=**************************
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

※古いバージョンのpythonや自己証明書を使用している場合は証明書検証でエラーになるようです。
そのため、以下を[targets:vars]に記載しています。

ansible_winrm_server_cert_validation=ignore

参考:
https://docs.ansible.com/ansible/latest/user_guide/windows_faq.html
https://www.redhat.com/ja/explore/ansible/getting-started/windows-host

playbook.ymlの作成

playbookのtasksで実行するのは以下です。タスクの順番を入れ替えるとIISのツリー構造の都合上、エラーになります。
・IISのインストール
・アプリケーションプールの作成
・IISサイトの作成
・ウェブアプリケーションの作成
・仮想ディレクトリの作成
・Windows Firewallの受信の規則のルール追加

今回はIISのDefault Web Siteを使用せず、新しくサイトを作成します。
また、HTTPのポートは8080を使用するように設定するため、Windows Firewallの規則も追加します。

playbook.yml
- hosts: targets
  gather_facts: no
  tasks:
    - name: Install IIS Web-Server with sub features and management tools
      ansible.windows.win_feature:
        name: Web-Server
        state: present
        include_sub_features: yes
        include_management_tools: yes
    - name: Create a new application pool in 'Started' state
      community.windows.win_iis_webapppool:
        name: test-pool
        state: started
    - name: Create IIS test site
      community.windows.win_iis_website:
        name: test-site
        state: started
        port: 8080
        ip: '*'
        hostname: '*'
        application_pool: test-pool
        physical_path: C:\inetpub\testsite
        parameters: logfile.directory:C:\sites\logs
    - name: Add test webapplication on IIS
      community.windows.win_iis_webapplication:
        name: test-app
        site: test-site
        state: present
        physical_path: C:\apps\testapp
    - name: Create a virtual directory if it does not exist
      community.windows.win_iis_virtualdirectory:
        name: test-directory
        site: test-site
        state: present
        physical_path: C:\virtualdirectory\testdir
    - name: Firewall rule to allow HTTP on TCP port 8080
      community.windows.win_firewall_rule:
        name: HTTP-8080
        localport: 8080
        action: allow
        direction: in
        protocol: tcp
        state: present
        enabled: yes

AWSの設定

WinRMの通信を許可するため、セキュリティグループ(SG)を設定します。
LinuxサーバとWindowsサーバでSGを分けているので、Windowsサーバ側のSGに以下を追加します。
インバウンドルール:5986/TCPを許可。ソースは属しているサブネットIDを指定。

4. 実行結果

では、playbookを実行します。

$ ansible-playbook -i ./inventory ./playbook.yml

PLAY [targets] ***************************************************************************************************

TASK [Install IIS Web-Server with sub features and management tools] *********************************************
changed: [test_windows]

TASK [Create a new application pool in 'Started' state] **********************************************************
changed: [test_windows]

TASK [Create IIS test site] **************************************************************************************
changed: [test_windows]

TASK [Add test webapplication on IIS] ****************************************************************************
changed: [test_windows]

TASK [Create a virtual directory if it does not exist] ***********************************************************
changed: [test_windows]

TASK [Firewall rule to allow HTTP on TCP port 8080] **************************************************************
changed: [test_windows]

PLAY RECAP *******************************************************************************************************
test_windows               : ok=6    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

5. 結果確認

IISがインストールされ、playbookに記載した設定がされていることが確認できました。

また、Windows Firewallの受信の規則も追加されています。

ブラウザからもアクセスできました。

6. おわりに

Ansibleを使って基本的なIISのセットアップはできることが確認できました。
HTTPS接続を追加したければ、サーバ証明書をインストールしてAnsibleモジュールでHTTPSのバインド追加も可能です。
(下記参考サイトの「バインド設定」参照)

参考サイト

■Ansibleモジュール
Webサイト作成:
https://docs.ansible.com/ansible/latest/collections/community/windows/win_iis_website_module.html)
仮想ディレクトリ作成:
https://docs.ansible.com/ansible/latest/collections/community/windows/win_iis_virtualdirectory_module.html
ウェブアプリケーション:
https://docs.ansible.com/ansible/latest/collections/community/windows/win_iis_webapplication_module.html
ウェブアプリケーションプール:
https://docs.ansible.com/ansible/latest/collections/community/windows/win_iis_webapppool_module.html
バインド設定:
https://docs.ansible.com/ansible/latest/collections/community/windows/win_iis_webbinding_module.html
Windows Firewallの受信の規則の許可追加:
https://docs.ansible.com/ansible/latest/collections/community/windows/win_firewall_rule_module.html