【Windows Server 2016】Ansibleリモートサーバーとしての初期設定について

記事タイトルとURLをコピーする

こんにちは、技術4課の城です。

構成管理ツールのAnsibleについて勉強中です。
今回はWindows Server 2016へRDPログインせずに初期設定をしてみましたので、紹介します。
【参考】https://docs.ansible.com/ansible/devel/user_guide/windows_setup.html

リモートサーバーの要件

Ansibleを実行するリモートサーバーの要件としては下記となります。

  • 対応クライアントOS:Windows 7、8.1、10、
  • 対応サーバーOS:Windows Server 2008、2008 R2、2012、2012 R2、2016
  • PowerShell version 3.0以上
  • .NET Framework 4.0以上

利用したAMI:Windows_Server-2016-Japanese-Full-Base-2018.03.24(ami-07f2a061)ですと、初期状態で要件を満たしており、対応は不要でした。

リモートサーバーの初期設定

Ansibleを実行するためにはリモートサーバーに次の設定が必要となります。

  • WinRM(Windows Remote Management)サービスのセットアップ
  • WinRMのリスナーのセットアップ

初期設定については公式で用意されたスクリプトを利用して基本設定を行うことが可能です。
ただし公式の注意書きにもありますが、このスクリプトではWinRMでのBasic認証を利用する設定となるため、本番環境での利用は非推奨です。

The ConfigureRemotingForAnsible.ps1 script is intended for training and development purposes only and should not be used in a production environment, since it enables settings (like Basic authentication) that can be inherently insecure.

今回はuserdataで先に紹介したスクリプト ConfigureRemotingForAnsible.ps1を取得、実行する形で初期設定したいと思います。
実行したuserdataです。


mkdir "$env:SystemDrive\temp"
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:SystemDrive\temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file


セキュリティグループの設定

初期設定でWinRMのHTTPSリスナーのポートはTCP5986なので、セキュリティグループにルールを追加します。

インベントリファイルの作成

インベントリファイルは接続に利用する変数を設定することができます。
今回は次のものを作成しました。

[test-windows2016]
13.231.178.197

[test-windows2016:vars]
ansible_ssh_user=Administrator
ansible_ssh_pass=******************
ansible_ssh_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

ansible_winrm_server_cert_validationはPythonのSSL証明書検証エラー回避のために設定しました。

Ansible実行確認

Ansibleサーバー側からwin_pingモジュールを利用して設定できているか確認します。
SUCCESSと返ってくればOKです。

ansible test-windows2016 -i hosts -m win_ping

13.231.178.197 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}


さいごに

簡単ではありますが、リモートサーバーの設定をしてみました。

多数のサーバーに同一設定を実施するようなケースで、Ansibleは非常に強力なツールです。
是非使いこなしていきたいですね。

城 航太 (記事一覧)

営業部カスタマーサクセス課・課長

AWSへの移行、AWSアカウントセキュリティ、ネットワーク広く浅くといった感じです。最近はAmazon WorkSpacesやAmazon AppStream2.0が好きです。APN Ambassador 2019,2020