ようへいの日々精進XP

よかろうもん

Ansible で Windows Server の構成管理(1)~ Windows Server を Ansible で構成管理出来るようにする ~

tl;dr

先日、以下の勉強会に参加させて頂いた。

peatix.com

事例とハンズオンと非常に興味深い内容だったが、事例紹介の中で Ansible を使って Windows Server 数百台をセットアップして効率化を図った旨の紹介がとても印象に残ったので、手元でも以下の内容で Windows Server のセットアップ(独りハンズオン)を試してみることにした。

  1. Windows Server を Ansible で構成管理出来るようにする
  2. Ansible で IIS をセットアップしてみる
  3. セットアップアップした内容を Serverspec でテストする
  4. EC2 の起動も Ansible で管理する

今回は以下のような構成で独りハンズオンを進めていく。

f:id:inokara:20160124164825p:plain


参考


Windows Server を Ansible で構成管理出来るようにする

Windows Remote Management

Ansible で Windows の構成管理を行う場合には Windows Remote Management(以下 WinRM)を有効にする必要があるので、こちらの記事を参考にさせて頂いて作業を進める。

既に Windows Server を起動している場合には Remote Desktop を利用してログインする。

f:id:inokara:20160124171426p:plain

PowerShell を起動して Ansible が配布している WinRM を起動するスクリプトを実行したいので、Administrator 権限を利用して PowerShell を起動する。

f:id:inokara:20160124171738p:plain

以下のようにスクリプトをダウンロードしておく。

PS C:\Users\Administrator\Documents> Invoke-WebRequest -Uri https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1

確認。

PS C:\Users\Administrator\Documents> dir


    Directory: C:\Users\Administrator\Documents


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/24/2016   8:20 AM       6239 ConfigureRemotingForAnsible.ps1


PS C:\Users\Administrator\Documents>

一応、スクリーンショット

f:id:inokara:20160124172317p:plain

  • ネットワークプロファイルを確認する

ネットワークプロファイルの NetworkCategory をプライベートに変更する必要があるとのことなのでネットワークプロファイルを確認する。

PS C:\Users\Administrator\Documents> Get-NetConnectionProfile -IPv4Connectivity Internet


Name             : Network  2
InterfaceAlias   : Ethernet
InterfaceIndex   : 12
NetworkCategory  : Public
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic

以下のように実行して NetworkCategory プライベートに変更する。

Set-NetConnectionProfile -InterfaceAlias (Get-NetConnectionProfile -IPv4Connectivity Internet).InterfaceAlias -NetworkCategory Private

以下、実行結果。

PS C:\Users\Administrator\Documents> Set-NetConnectionProfile -InterfaceAlias (Get-NetConnectionProfile -IPv4Connectivity Internet).InterfaceAlias -NetworkCategory Private
PS C:\Users\Administrator\Documents> Get-NetConnectionProfile -IPv4Connectivity Internet


Name             : Network  2
InterfaceAlias   : Ethernet
InterfaceIndex   : 12
NetworkCategory  : Private
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic
  • WinRM を有効にする

以下のように先ほどダウンロードしておいたスクリプトを実行して WinRM を有効にする。

PS C:\Users\Administrator\Documents> powershell -ExecutionPolicy RemoteSigned .\ConfigureRemotingForAnsible.ps1


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.

-ExecutionPolicyPowerShell を実行する際のポリシーを定義出来る(という認識)。ポリシーの定義は以下の通り。

ポリシー名 説明
Restricted 実行できるスクリプトはありません。Windows PowerShell は対話型モードでのみ使用できます。
AllSigned 信頼できる発行元が署名したスクリプトのみを実行できます。
RemoteSigned ダウンロードしたスクリプトは信頼できる発行元が署名した場合にのみ実行できます。
Unrestricted 制限なし。すべての Windows PowerShell スクリプトを実行できます。

(※Set-ExecutionPolicy コマンドレットの使用 より抜粋)

初めての Ansible で Windows Server の構成管理

準備が整ったところで初めての構成管理として win_ping モジュールを使ってみたいと思う。

  • Ansible から WinRM にアクセスできるように pywinrm をインストールする
#
# pip のバージョンを確認
#
$ pip -V
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)

#
# pywinrm のインストール
#
$ sudo pip install pywinrm
Downloading/unpacking pywinrm
  Downloading pywinrm-0.1.1.tar.gz
  Running setup.py (path:/tmp/pip_build_root/pywinrm/setup.py) egg_info for package pywinrm

Downloading/unpacking xmltodict (from pywinrm)
  Downloading xmltodict-0.9.2.tar.gz
  Running setup.py (path:/tmp/pip_build_root/xmltodict/setup.py) egg_info for package xmltodict

Downloading/unpacking isodate (from pywinrm)
  Downloading isodate-0.5.4.tar.gz
  Running setup.py (path:/tmp/pip_build_root/isodate/setup.py) egg_info for package isodate

Installing collected packages: pywinrm, xmltodict, isodate
  Running setup.py install for pywinrm

  Running setup.py install for xmltodict

  Running setup.py install for isodate

Successfully installed pywinrm xmltodict isodate
Cleaning up...
  • Inventory ファイルを作成
$ cat hosts
[windows]
ec2-xx-xx-xxx-xx.ap-northeast-1.compute.amazonaws.com

[windows:vars]
ansible_ssh_user=Administrator
ansible_ssh_pass=${ansible_ssh_user} のパスワード
ansible_ssh_port=5986
ansible_connection=winrm
  • ansible を実行
#
# Inventory ファイルを確認する
#
$ ls -l
total 4
-rw-rw-r-- 1 vagrant vagrant 186 Jan 24 17:54 hosts

#
# win_ping モジュールを実行
#
$ ansible -i hosts windows -m win_ping
ec2-xx-xx-xxx-xx.ap-northeast-1.compute.amazonaws.comm | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

成功した際の緑色が好きなのでスクリーンショットを。

f:id:inokara:20160124180300p:plain

ひとまずおっけ。


以上

ひとまず、Windows Server を Ansible から弄れる環境は思った以上に簡単にセットアップすることが出来た。参考にさせて頂いた記事を殆ど写経しただけだけど、実際に弄ってみて理解が深まった。次は IIS をセットアップしてみる。