ksaitoの日記

日々試したことの覚え書き

Windows 2012 R2をansibleで管理できるようにセットアップする。

移転しました。

自動的にリダイレクトします。

Windows 2012 R2をansibleで管理できるようにセットアップする手順です。

環境

環境は下記の通りです。

OS 環境
Windows 2012 R2 Datacenter 管理対象のAzureの"Windows 2012 R2 Datacenter"を利用
Ubuntu 14.04 LST ansibleを動かす環境

ansibleのバージョンは、以下の通り

$ ansible --version
ansible 2.0.0.2
  config file = 
  configured module search path = Default w/o overrides
$

管理対象(Windows)の設定

ansible設定用スクリプトのダウンロード

下記のコマンドでNetworkCategoryPrivateになっているか確認する。

PS C:\Users\winrm\Downloads> Get-NetConnectionProfile -IPv4Connectivity Internet | fl -Property NetworkCategory,InterfaceAlias

NetworkCategory : Private
InterfaceAlias  : Ethernet

PS C:\Users\winrm\Downloads>

Publicの場合は、変更する。

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

適当なバージョンのスクリプトをダウンロードします。

PS C:\Users\winrm\Downloads> Invoke-WebRequest -Uri https://raw.githubusercontent.com/ansible/ansible/stable-2.0.0.1/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1
PS C:\Users\winrm\Downloads>

スクリプトの実行

ダウンロードしたスクリプトを実行してOk.が表示されたら管理対象の準備は完了です。

PS C:\Users\winrm\Downloads> .\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.

PS C:\Users\winrm\Downloads>

firewall設定

Azureで作成したインスタンスは、Windows 2012 R2インスタンスのネットワークセキュリティグループで下記を設定します。

  • 受信ルールを追加
  • ソースにansibleサーバのIPアドレス
  • 受信ポートは、5986

ansibleの設定

疎通確認

管理対象のWindows側で使っているポートを確認します。

PS C:\Users\winrm\Downloads> winrm e winrm/config/listener | Select-String -Pattern " Port "

    Port = 5985
    Port = 5986


PS C:\Users\winrm\Downloads>

ポートが疎通しているか確認します。 STATEfilteredとなっている場合は、管理対象マシンに疎通していないので、firewall関連の設定やWinRMの設定を見直します。

$ nmap -Pn -p 5986 40.74.124.244

Starting Nmap 6.40 ( http://nmap.org ) at 2016-02-06 14:12 JST
Nmap scan report for 40.74.124.244
Host is up (0.010s latency).
PORT     STATE SERVICE
5986/tcp open  wsmans

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
$

ansibleの疎通確認用のインベントリファイルを作成します。

$ cat hosts
[windows]
40.74.124.244

[windows:vars]
ansible_ssh_user=winrm
ansible_ssh_pass=password
ansible_ssh_port=5986
ansible_connection=winrm
$

ansibleを動かすサーバで疎通を確認します。

$ ansible -i hosts windows -m win_ping
40.74.124.244 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
$