4
4

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 5 years have passed since last update.

AnsibleでWindowsの「役割と機能」を追加する時の指定方法

Posted at

概要

Ansible+Windowsで、「機能と役割」を追加する場合、書き方を工夫したら少しは早くなるかと思ったのですが、そうでもなかったという話です。

環境

  • CentOS7.6 + Ansible 2.8.4
  • ターゲットはWindows2016

「機能と役割」について

WindowsOSで、GUIから「機能と役割」を追加する場合、依存関係のあるものは、一緒にインストールできます。
ただし、階層の配下にあるものが、全てインストール対象となる訳ではないです。

例えば「Webサーバー」。
機能の階層構造は、以下の通りです。

get-windowsfeature-1
~略~
[ ] Web サーバー (IIS)                                             Web-Server
    [ ] Web サーバー                                               Web-WebServer
        [ ] HTTP 共通機能                                          Web-Common-Http
            [ ] HTTP エラー                                       Web-Http-Errors
            [ ] ディレクトリの参照                                      Web-Dir-Browsing
            [ ] 既定のドキュメント                                      Web-Default-Doc
            [ ] 静的なコンテンツ                                       Web-Static-Content
            [ ] HTTP リダイレクト                                    Web-Http-Redirect
            [ ] WebDAV 発行                                      Web-DAV-Publishing
        [ ] セキュリティ                                             Web-Security
            [ ] 要求フィルター                                        Web-Filtering
            [ ] IIS クライアント証明書マッピング認証                           Web-Cert-Auth
            [ ] IP およびドメインの制限                                  Web-IP-Security
~略~

「Web サーバー (IIS) 」にチェックを入れると、以下の感じになります。

get-windowsfeature-2
~略~
[X] Web サーバー (IIS)                                             Web-Server
    [X] Web サーバー                                               Web-WebServer
        [X] HTTP 共通機能                                          Web-Common-Http
            [X] HTTP エラー                                       Web-Http-Errors
            [X] ディレクトリの参照                                      Web-Dir-Browsing
            [X] 既定のドキュメント                                      Web-Default-Doc
            [X] 静的なコンテンツ                                       Web-Static-Content
            [ ] HTTP リダイレクト                                    Web-Http-Redirect
            [ ] WebDAV 発行                                      Web-DAV-Publishing
        [X] セキュリティ                                             Web-Security
            [X] 要求フィルター                                        Web-Filtering
            [ ] IIS クライアント証明書マッピング認証                           Web-Cert-Auth
            [ ] IP およびドメインの制限                                  Web-IP-Security
~略~

Playbookでは何を指定するか?

Ansibleで「機能と役割」を追加する場合、win_featureを利用します。
その際、nameで指定する名前は、Get-WindowsFeatureで取得した名前を指定します。
例えばこんな感じです。

webserver-1.yml
  win_feature:
    name: Web-Server
    state: present
    include_sub_features: no
    include_management_tools: yes

これで、GUIの「Webサーバー」でチェックを入れた場合と同じことをやってくれます。
以下のように、個別で書いても、同じ結果が得られます。

webserver-2.yml
  win_feature:
    name: 
      - Web-Http-Errors
      - Web-Dir-Browsing
      - Web-Default-Doc
      - Web-Static-Content
      - Web-Filtering
      ~略~
    state: present
    include_sub_features: no
    include_management_tools: yes

上記2つの処理時間を比較すると、前者のほうが早い!と思っていたのですが、当方の検証環境でざっくり試したところ、大差ありませんでした。。
(違う結果が出た方、コメントお待ちしています!)

この結果から、

1)Playbookの文字数を減らしたい場合は、「webserver-1.yml」
2)後から見直したときに、何が入っているか明確にしたい場合は「webserver-2.yml」

といったところが、記載方法の目安になるかと思います。
個人的には、1)をベースにして、個別導入が必要なものを追記するのが、良いと思います。
(ちなみに、
1)の方針で作成したときは、後日見直したとき、何をやりたかったのか思い出すのに苦労しました・・・
2)の方針で作成したときは、Playbook(というかvars)の作成が大変でした・・・)
当然、ケースバイケースなので、みんなで話し合うことが大事です。。

(参考)「機能と役割」の依存関係

上記の1)にする場合、指定した名前で、何が入るか調べる必要があります。
例えば、「Web サーバー (IIS)」の依存関係を調べる場合、以下のようなコマンドで調査できます。

add-windowsfeature.ps1
Add-WindowsFeature Web-Server -IncludeManagementTools -WhatIf |Select -ExpandProperty FeatureResult |Select Id,DisplayName, Name|sort Id|ft -AutoSize

上記のWeb-Serverを、別の何かに変更すれば、いろいろ調査できます。

ただし、インストール済みのものや、インストールできないものは抽出できません。
(最初から入っている.net45関連や、仮想環境上のOSでは、基本的にHyper-Vはエラーになります。)

4
4
0

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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?