LoginSignup
2
3

More than 3 years have passed since last update.

EC2でwindowsインスタンスを作って、Ansibleで操作できるようにセットアップした

Posted at

ansibleでwindows向けのplaybookを作る機会があったので、手軽にテストできるようにansibleで操作可能なwindowsインスタンスをEC2で作成した。そのときの手順をまとめた。

AWSにWindowsServerインスタンスを作成

"Microsoft Windows Server 2016 Base - ami-04f188a745c1a5ecb"で検証を行った。

インスタンス作成時に注意しておくことは次の3点

  • 「セキュリティグループの設定」のインバウンド設定で、RDPとWinRM-HTTPSのポートを開けておく。
  • キーペアの登録は必ず行う。
  • 「インスタンスの詳細設定」の「高度な詳細」>「ユーザーデータ」に次の内容をコピペしておく。これにより、インスタンス起動時にpowershellでスクリプトが実行され、AnsibleがWindowsServerと通信するために必要な、WindowsServer側のセットアップが完了する。このスクリプトは、Ansibleの公式ドキュメント Setting up a Windows Host を参考にした。
<powershell>
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file
</powershell>

ちなみに、上記の設定はあまり安全とはいえない設定(Basic認証など)を有効にしているので、本番環境ではこのまま使わないほうがいい。

inventoryファイルの作成

インスタンスができたら、「アクション」 > 「Windows パスワードの取得」 で、パブリックDNSユーザー名パスワードを取得する。

この情報を元に、inventoryファイルを作成する。

[windows2016]
<パブリックDNS> ansible_user=<ユーザー名> ansible_password=<パスワード> ansible_connection=winrm ansible_winrm_server_cert_validation=ignore

接続確認

$ ansible windows2016 -i ./inventory -m win_ping                                
ec2-13-231-151-252.ap-northeast-1.compute.amazonaws.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

起動テンプレートの作成

上記の設定で通信できることが確認できので、同じ設定でEC2の起動テンプレートを作成して、いつでも同じ設定のWindowsServerを素早く作れるようにした。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3