自動化を推進する上で『Windowsの自動化』というニーズは多くいただきますが、その中でも自動化を導入する価値、Playbookの書き方や効率的な管理方法を知りたいという声を多くいただきます。本記事ではWindowsの自動化とは?Ansibleでどのように使つかうの?という最初にうかぶ疑問にお答えします。
Red Hat Ansible Automationとは?という方はこちらのソリューションページをご参照ください。
Red Hat Ansible Automationの仕組み
どのようにWindows Serverの自動化を行うのか、Ansibleの仕組みとともに紹介していきます。
Ansibleでは、プレイブック(Playbook)と呼ばれるYAML形式で作成された自動化のための指示書を読み込み、Playbookに記載された内容に沿った自動化プロセスが実行されます。
この時に作業対象のノード情報はインベントリの情報より取得し、自動化のプロセスはModuleが実行します。
Windowsモジュールの場合、WinRMを使用して対象ノードにアクセスしてモジュールを対象ノードにコピーした上でPlaybookに記述された状態になるようプログラムが実行されます。

Module毎に対応するOSや機器、クラウドサービスが異なります。自動化の導入を検討する際には、その対象および用途に対するモジュールが提供されているかを確認する必要があります。
AnsibleのModuleを調べる場合、利用するバージョンにより参照する先が変わりますのでご注意ください。これはAnsible 2.10よりモジュールがAnsible Collection より提供されるように仕様変更されたことが要因です。
Ansible 2.10における Windows Server モジュールを確認する場合は以下のドキュメントを参照してください。
ansible.windows collection
https://docs.ansible.com/ansible/latest/collections/ansible/windows/index.html#plugins-in-ansible-windows
Ansible 2.9(2.9以下)をご利用の場合は、下記のドキュメントが参照先となります。
Windows module index
https://docs.ansible.com/ansible/2.9_ja/modules/list_of_windows_modules.html
Windows Modulesを使ってみる
実際にどのようなModuleが用意されているのか、一部のモジュールを例に上げてみてみましょう。
win_copy | リモートのWindowsホストへファイルをコピー |
win_service | Windows serviceの管理 |
win_domain | dns_domain_nameで指定されたドメインが存在し、到達可能か確認 |
win_reboot | Windowsを再起動 |
win_regedit | レジストリキーと値を追加、変更、または削除 |
win_ping | ping モジュールのWindowsバージョン |
win_dsc | PowerShell DSC構成ファイルの呼び出し |
win_acl | システム ユーザーまたはグループのファイル/ディレクトリ/レジストリのアクセス許可を設定 |
このリストからもモジュールごとに役割があることが一覧からわかるかと思います。サーバー自体を管理するモジュールから、ユーザー管理のモジュールやサービスに関するモジュールなどが利用できます。※この他にも多数のWindowsモジュールがあるので一覧をご覧ください。
次に実際のサンプルスクリプトを使用して動作を見てみましょう。
WindowsにてWeb-Serverをインストールし、IISサービスを起動
---
- name: install the iis web service
hosts: windows
tasks:
- name: install iis
win_feature:
name: Web-Server
state: present
- name: start iis service
win_service:
name: W3Svc
state: started
このサンプルでは、7行目の最初のタスクにて win_feature モジュールが指定されています。win_featureモジュールを使用し Web-Server をインストールされた状態となることを指示している記述になります。
次のタスクでは、win_service モジュールよりIISサービスが起動している状態を指示しています。
※Ansibleではあるべき状態を記述するため、「インストールされている状態」、「起動している状態」と表記しています。
既にLinux ServerでAnsibleをご利用されている方は、書き方は基本的には変わらないのだなとご理解いただけるのではないでしょうか。
参考までに、LinuxサーバーでApacheサーバーをインストールする場合は以下のサンプルコードになります。
Linux ServerにてApacheをインストールし、サービスを起動
---
- name: Apache server installed
hosts: webserver
tasks:
- name: latest Apache version installed
yum:
name: httpd
state: latest
- name: Apache enabled and running
service:
name: httpd
enabled: true
state: started
如何でしょうか?書式は基本的には同じでモジュールの指定とそのオプションがそれぞれモジュールにて用意されたものを記述している形になり、Windows にそれほど詳しくない私でも簡単な操作ならPlaybookで書けました。
今回は簡単にWindows Serverの自動化をご紹介しましたが、ご要望があればより高度なWindows Serverの自動化にも触れていこうかと思います。ぜひ、皆様のリクエストお待ちしております。
参考:Ansible Collection の一覧
- amazon.aws
- ansible.builtin
- ansible.netcommon
- ansible.posix
- ansible.windows ←Windows Moduleはこちら
- arista.eos
- awx.awx
- azure.azcollection
- check_point.mgmt
- chocolatey.chocolatey
- cisco.aci
- cisco.asa
- cisco.intersight
- cisco.ios
- cisco.iosxr
- cisco.meraki
- cisco.mso
- cisco.nxos
- cisco.ucs
- cloudscale_ch.cloud
- community.aws
- community.azure
- community.crypto
- community.digitalocean
- community.general
- community.grafana
- community.kubernetes
- community.libvirt
- community.mongodb
- community.mysql
- community.network
- community.proxysql
- community.rabbitmq
- community.skydive
- community.vmware
- community.windows
- community.zabbix
- containers.podman
- cyberark.conjur
- cyberark.pas
- dellemc.os10
- dellemc.os6
- dellemc.os9
- f5networks.f5_modules
- fortinet.fortimanager
- fortinet.fortios
- frr.frr
- gluster.gluster
- google.cloud
- hetzner.hcloud
- ibm.qradar
- infinidat.infinibox
- junipernetworks.junos
- mellanox.onyx
- netapp.aws
- netapp.elementsw
- netapp.ontap
- netapp_eseries.santricity
- netbox.netbox
- ngine_io.cloudstack
- ngine_io.exoscale
- ngine_io.vultr
- openstack.cloud
- openvswitch.openvswitch
- ovirt.ovirt
- purestorage.flasharray
- purestorage.flashblade
- servicenow.servicenow
- splunk.es
- theforeman.foreman
- vyos.vyos
- wti.remote
参考:WINDOWS MODULEの詳しい情報は以下のリンクもご活用ください。
Name | Description |
ansible.windows.win_acl | Set file/directory/registry permissions for a system user or group |
ansible.windows.win_acl_inheritance | Change ACL inheritance |
ansible.windows.win_certificate_store | Manages the certificate store |
ansible.windows.win_command | Executes a command on a remote Windows node |
ansible.windows.win_copy | Copies files to remote locations on windows hosts |
ansible.windows.win_dns_client | Configures DNS lookup on Windows hosts |
ansible.windows.win_domain | Ensures the existence of a Windows domain |
ansible.windows.win_domain_controller | Manage domain controller/member server state for a Windows host |
ansible.windows.win_domain_membership | Manage domain/workgroup membership for a Windows host |
ansible.windows.win_dsc | Invokes a PowerShell DSC configuration |
ansible.windows.win_environment | Modify environment variables on windows hosts |
ansible.windows.win_feature | Installs and uninstalls Windows Features on Windows Server |
ansible.windows.win_file | Creates, touches or removes files or directories |
ansible.windows.win_find | Return a list of files based on specific criteria |
ansible.windows.win_get_url | Downloads file from HTTP, HTTPS, or FTP to node |
ansible.windows.win_group | Add and remove local groups |
ansible.windows.win_group_membership | Manage Windows local group membership |
ansible.windows.win_hostname | Manages local Windows computer name |
ansible.windows.win_optional_feature | Manage optional Windows features |
ansible.windows.win_owner | Set owner |
ansible.windows.win_package | Installs/uninstalls an installable package |
ansible.windows.win_path | Manage Windows path environment variables |
ansible.windows.win_ping | A windows version of the classic ping module |
ansible.windows.win_reboot | Reboot a windows machine |
ansible.windows.win_reg_stat | Get information about Windows registry keys |
ansible.windows.win_regedit | Add, change, or remove registry keys and values |
ansible.windows.win_service | Manage and query Windows services |
ansible.windows.win_service_info | Gather information about Windows services |
ansible.windows.win_share | Manage Windows shares |
ansible.windows.win_shell | Execute shell commands on target hosts |
ansible.windows.win_stat | Get information about Windows files |
ansible.windows.win_tempfile | Creates temporary files and directories |
ansible.windows.win_template | Template a file out to a remote server |
ansible.windows.win_updates | Download and install Windows updates |
ansible.windows.win_uri | Interacts with webservices |
ansible.windows.win_user | Manages local Windows user accounts |
ansible.windows.win_user_right | Manage Windows User Rights |
ansible.windows.win_wait_for | Waits for a condition before continuing |
ansible.windows.win_whoami | Get information about the current user and process |