LoginSignup
5
9

More than 5 years have passed since last update.

WindowsServerをAnsibleでリモート管理するための第一歩(Ansible for Windows)

Last updated at Posted at 2018-09-26

Ansible for Windowsの2回目。
前回はAnsibleのインストールについてまとめました。今回はサーバー管理の第一歩として、WindowsServer2016サーバーをAnsibleで接続できるようにします。

前の記事:
  Windows10 に ゼロから Ansible をインストールする
次の記事:
  IISにWebサイトを追加するplaybookサンプル

管理されるサーバー側の設定

  • Windows Server 2016 Std.
  • PowerShell 5.1
  • ActiveDirectory ドメインに属する
  • ホスト名をw16srvとします

設定用スクリプトの実行

いろいろな設定を一括で実行してくれるPowerShellスクリプト(ConfigureRemotingForAnsible.ps1)が用意されています。これをダウンロードして保存します。

w16test上でPowerShellを管理者モードで起動し、ConfigureRemotingForAnsible.ps1を実行します。
このスクリプトの詳細は別記事でまとめます。

PS> .\ConfigureRemotingForAnsible.ps1 -Verbose -CertValidityDays 3650
詳細: Verifying WinRM service.
詳細: Enabling PS Remoting.
詳細: Enabled PS Remoting.
詳細: Setting LocalAccountTOkenFilterPolicy to 1
Self-signed SSL certificate generated; thumbprint: *********************************
詳細: Enabling SSL listener.


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                : ja-JP
Address             : http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
ReferenceParameters : ReferenceParameters

詳細: Enabling basic auth support.
詳細: Adding firewall rule to allow WinRM HTTPS.
OK

詳細: HTTP: Enabled | HTTPS: Enabled
詳細: PS Remoting has been successfully configured for Ansible.

今回はBasic認証で接続する設定です。公式ドキュメントにも記載されていますが、実際の運用時にはよりセキュアな設定で稼働すべきでしょう。

疎通確認

前回設定したAnsibleホストから、w16srvにAnsibleで接続できることを以下の手順で確認します。

接続用ユーザーの作成

今回はサーバーw16svr上のローカルユーザーとしてAnsible接続ユーザーを用意します。
アカウント名:AnsibleAdmin
ローカルグループ:Administrators

インベントリファイルの用意

インベントリファイルとは管理対象のノード(サーバーやデータベースなど)を定義したテキストファイルです。
ここでは「hosts」というファイル名で「D:\Projects\SRE\Ansible\MyData」以下に作ります。
「D:\Projects\SRE\Ansible\MyData」はDocker起動時の指定によりDocker内の「/mydata」にマウントされています。

hosts
[w16svr]
192.168.0.2

[w16svr:vars]
ansible_user=AnsibleAdmin
ansible_password=****************
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

pingの実行

前回のまとめに記載した手順で、管理者モードのPowerShellコマンドプロンプトからAnsibleホストに接続します。

PS> docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
899225e6b593        my-ansible          "/bin/bash"         About a minute ago   Up About a minute                       sad_shannon
PS> docker exec -it 899225e6b593 /bin/bash
[root@899225e6b593 /]# 

Ansibleでw16svrに導通確認を行います。先ほど作成したインベントリファイル「hosts」はDocker内では「/mydata/hosts」でアクセス可能です。

[root@899225e6b593 /]# ansible w16svr -i /mydata/hosts -m win_ping
192.168.0.2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

「SUCCESS」が返ってくれば疎通確認OKです。

5
9
1

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
5
9