4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AnsibleでWindowsServer初期セットアップをする

Last updated at Posted at 2021-08-17

お世話になります。

仮想環境ではテンプレートを使ってVMをクローンしますが、Windowsの場合SIDの重複という問題があるため、Sysprepした状態でテンプレートを作成します。

これによりクローンしたVMは設定が初期化されているため、各マシンごとにIPアドレス設定やローカル管理者の削除、といった手作業が毎回必要になってしまいます。
今回、こうしたGUIによる操作を自動化するため、Ansibleを利用してみます。流れとしては以下のようになります。

・Win2019のテンプレートイメージにてWinRM機能を有効化
・Ansibleサーバ(イニシエータというべき?)でhostsを更新
・Playbookの実行

今回、Turnkey Ansibleで以下のスペックでVMを作成してみました。
https://www.turnkeylinux.org/ansible
image.png
image.png
image.png

インストールが終わったら、対象となるWindowsServerで以下のコマンドを実行します。以下のサイトを参考にさせて頂きました。
https://qiita.com/yunano/items/f9d5652a296931a09a70

$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

image.png

受付状態を確認します。ポート5985と5986がリッスンしていればOKです。この状態でSysprepをして、Proxmoxのテンプレートとして格納しておきます。

winrm enumerate winrm/config/listener

image.png

Ansibleサーバにログインします。
image.png

適宜スナップショットを取っておきます。
image.png

以下のコマンドで、hosts一覧にWindowsServerを追加しておきます。IPは実際のものに変更します。

[windows]
<Windowsの解決可能なホスト名かIPアドレス>

[windows:vars]
ansible_ssh_user=<Windows側のユーザ名>
ansible_ssh_pass=<Windows側ユーザのパスワード>
ansible_ssh_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

image.png

Ping疎通コマンドで確認します。SUCCESSとなればOKです。

ansible windows -i hosts -m win_ping

image.png

以下の名前でansible/test.ymlの名前で保存します。今回はC:\ansible_testというフォルダを作り、ローカル管理者を無効化するというシンプルな検証となります。

- hosts: windows
  tasks:
    - win_file:
        path: C:\ansible_test
        state: directory
    - win_user:
        name: Administrator
        account_disabled : yes

準備が整いましたので、対象Windows Serverの現状を表示しておきます。
image.png

プレイブックを実行します。

ansible-playbook -i hosts test.yml

image.png

win_userでエラーが出ていますが、これはWinRMイニシエータユーザが、今回無効化したいユーザと同一(ビルトインAdministrator)のためと思われます。

フォルダが作成され、Administratorが無効化されました。
image.png

Ansibleを使えば、たとえば以下のようなよくある展開で効率化ができそうです。

・テンプレートから5台のWindowsをクローン
・DHCPで取得したIPを確認し、hostsに追記
・各マシンに静的IPとDNSサーバの追加
・コンピュータ名の割り当て
・ドメインへの参加
・ローカルAdminの削除

Ansibleの特徴として、べき等性というものがあるそうです。まだ完全に理解していませんが、運用の上で注意する点かと思います。こちらのサイトが分かりやすかったです。
https://blog.adachin.me/archives/4739

また、Ansibleサーバはスペックはほとんど必要なさそうで、512MB/1コアでも十分動きそうです。SCCMやジョブフローといった巨大なサーバを立てずに済む、こうした点も良いと思います。

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?