2020.10.30

AnsibleでWindowsを自動化(入門編)

文:SIOS Murata

タグ

CONTENTS

この記事をシェア:

Facebook Twitter LINE

自動化を推進する上で『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_serviceWindows serviceの管理
win_domaindns_domain_nameで指定されたドメインが存在し、到達可能か確認
win_rebootWindowsを再起動
win_regeditレジストリキーと値を追加、変更、または削除
win_pingping モジュールのWindowsバージョン
win_dscPowerShell DSC構成ファイルの呼び出し
win_aclシステム ユーザーまたはグループのファイル/ディレクトリ/レジストリのアクセス許可を設定
Windows系モジュールより一部抜粋

このリストからもモジュールごとに役割があることが一覧からわかるかと思います。サーバー自体を管理するモジュールから、ユーザー管理のモジュールやサービスに関するモジュールなどが利用できます。※この他にも多数の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 の一覧

参考:WINDOWS MODULEの詳しい情報は以下のリンクもご活用ください。

NameDescription
ansible.windows.win_aclSet file/directory/registry permissions for a system user or group
ansible.windows.win_acl_inheritanceChange ACL inheritance
ansible.windows.win_certificate_storeManages the certificate store
ansible.windows.win_commandExecutes a command on a remote Windows node
ansible.windows.win_copyCopies files to remote locations on windows hosts
ansible.windows.win_dns_clientConfigures DNS lookup on Windows hosts
ansible.windows.win_domainEnsures the existence of a Windows domain
ansible.windows.win_domain_controllerManage domain controller/member server state for a Windows host
ansible.windows.win_domain_membershipManage domain/workgroup membership for a Windows host
ansible.windows.win_dscInvokes a PowerShell DSC configuration
ansible.windows.win_environmentModify environment variables on windows hosts
ansible.windows.win_featureInstalls and uninstalls Windows Features on Windows Server
ansible.windows.win_fileCreates, touches or removes files or directories
ansible.windows.win_findReturn a list of files based on specific criteria
ansible.windows.win_get_urlDownloads file from HTTP, HTTPS, or FTP to node
ansible.windows.win_groupAdd and remove local groups
ansible.windows.win_group_membershipManage Windows local group membership
ansible.windows.win_hostnameManages local Windows computer name
ansible.windows.win_optional_featureManage optional Windows features
ansible.windows.win_ownerSet owner
ansible.windows.win_packageInstalls/uninstalls an installable package
ansible.windows.win_pathManage Windows path environment variables
ansible.windows.win_pingA windows version of the classic ping module
ansible.windows.win_rebootReboot a windows machine
ansible.windows.win_reg_statGet information about Windows registry keys
ansible.windows.win_regeditAdd, change, or remove registry keys and values
ansible.windows.win_serviceManage and query Windows services
ansible.windows.win_service_infoGather information about Windows services
ansible.windows.win_shareManage Windows shares
ansible.windows.win_shellExecute shell commands on target hosts
ansible.windows.win_statGet information about Windows files
ansible.windows.win_tempfileCreates temporary files and directories
ansible.windows.win_templateTemplate a file out to a remote server
ansible.windows.win_updatesDownload and install Windows updates
ansible.windows.win_uriInteracts with webservices
ansible.windows.win_userManages local Windows user accounts
ansible.windows.win_user_rightManage Windows User Rights
ansible.windows.win_wait_forWaits for a condition before continuing
ansible.windows.win_whoamiGet information about the current user and process

文:SIOS Murata

タグ

相談したい
DXの課題何ですか?

DXの知識、導入事例を知りたい DXの知識、導入事例を知りたい セミナーに参加する

DXに関連する情報についてご紹介しています。お気軽にセミナー・イベントやウェビナーにご参加ください。

何から手をつけていいか分からない 何から手をつけていいか分からない サービスソリューションをみる

お客様ごとに、DX関連の最適なサービスソリューションがあります。まずはこちらからご覧ください。

自社の具体的な導入を検討している 自社の具体的な導入を検討している 各種お問い合わせへ

「DXの話だけでも聞いてみたい」「資料が欲しい」「相談したい」など、お気軽にご相談ください。