背景
仕事でAnsibleを触る機会があり、ついでにWindows Serverの操作をAnsibleからできないかやってみた。
環境
- 操る側
- Red Hat Enterprise Linux release 9.6 (Plow)
- ansible [core 2.14.18]
- 操られる側
- Microsoft Windows Server 2022 Standard Evaluation
Windows Serverでの準備
- まずはWinRMの設定を行う
- 過去Ansibleより設定用のPowerShellスクリプトが配布されていたが、現在では配信されていない
- 過去のブランチに残っているようなのでそれを使用することに
- 操られれる側のWindowsServerにて管理者権限でPowerShwllを起動し以下のコマンドを実行する
$url = "https://raw.githubusercontent.com/ansible/ansible/stable-2.12/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file
- 今回はBasic認証を用いて操作を行う
- また、WinRM側の設定でデフォルトで非暗号の通信を許可しない設定となっているためこれを許可(AllowUnencryptedをTrue)する
winrm set winrm/config/service '@{AllowUnencrypted="true"}
-
ちなみに無許可の場合だとAnsibleで接続しにいった際に以下のエラーが出る
basic: the specified credentials were rejected by the server -
WinRM設定状況を確認してみる
PS C:\Users\Administrator> winrm get winrm/config/service
Service
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
MaxConcurrentOperations = 4294967295
MaxConcurrentOperationsPerUser = 1500
EnumerationTimeoutms = 240000
MaxConnections = 300
MaxPacketRetrievalTimeSeconds = 120
AllowUnencrypted = true
Auth
Basic = true
Kerberos = true
Negotiate = true
Certificate = false
CredSSP = false
CbtHardeningLevel = Relaxed
DefaultPorts
HTTP = 5985
HTTPS = 5986
IPv4Filter = *
IPv6Filter = *
EnableCompatibilityHttpListener = false
EnableCompatibilityHttpsListener = false
CertificateThumbprint
AllowRemoteAccess = true
Ansible Playbook
Windowsモジュールを試用してコマンドを実行してみる。
まずは、systeminfoコマンドを使って応答があるかを確認してみる。
---
- hosts: windows
gather_facts: false
tasks:
- name: systeminfo
ansible.windows.win_command: systeminfo
register: systeminfo
- name: debug
ansible.builtin.debug:
msg: "{{ systeminfo.stdout_lines }}"
varsは以下のように設定
---
ansible_user: Administrator
ansible_password: パスワード
ansible_port: 5985
ansible_connection: winrm
ansible_winrm_scheme: http
ansible_winrm_server_cert_validation: ignore
プレイブック実行
[hogehoge@ansible windows]$ ansible-playbook -i hosts windows-systeminfo.yml
[WARNING]: Collection ansible.windows does not support Ansible version 2.14.18
PLAY [windows] **************************************************************************************************
TASK [systeminfo] ***********************************************************************************************
Wednesday 29 October 2025 14:49:43 +0900 (0:00:00.039) 0:00:00.039 *****
changed: [win2022]
TASK [debug] ****************************************************************************************************
Wednesday 29 October 2025 14:49:47 +0900 (0:00:04.304) 0:00:04.343 *****
ok: [win2022] => {
"msg": [
"",
"Host Name: WIN-PVA7VETD30H",
"OS Name: Microsoft Windows Server 2022 Standard Evaluation",
"OS Version: 10.0.20348 N/A Build 20348",
"OS Manufacturer: Microsoft Corporation",
"OS Configuration: Standalone Server",
〜省略〜
]
}
PLAY RECAP ******************************************************************************************************
win2022 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
文字化け
- 今回、Windowsのライセンス認証まわりを自動化したくslmgr.vbsを使用しようとした。通常であればポップアップ画面がでるが、cscripts.exeを使用してコマンドラインで表示する方法があるため、こちらをcommandモジュールを使って実行したが、結果が文字化けを起こした。
- 文字化けの原因や解決策については以下の記事に記載がありました。
https://rheb.hatenablog.com/entry/2023/12/20/070000 - output_encoding_override: shift_jisを指定することで文字化け問題は解決しました。
tasks:
- name: slmgr
ansible.windows.win_command: cscript.exe //nologo C:\Windows\System32\slmgr.vbs
args:
output_encoding_override: shift_jis