LoginSignup
8
8

More than 5 years have passed since last update.

Ansibleで WindowsServerへセキュリティアップデート(KB)を個別にインストール・アンインストールする サンプルプレイブック

Last updated at Posted at 2016-06-24

0.はじめに

Windows2012ServerR2へAnsibleから特定のKBをインストール・アンインストールするサンプルのAnsible playbookと 確認用 Serverspec specファイルです。

rawモジュールなどを使った泥臭いやり方ですが、Windows関連の事例が少ないので参考までに。
なお、実際にはserviceの上げ下げやreboot、上位のLB制御なども必要だと思いますが
シンプルにKBのインストール・アンインストールのみを記載しています。
また、インストールについてはDISMではなくpkgmgrを使用しています。


linuxのセキュリティアップデートについてはこちらの記事も参考にしていただければと思います。
http://qiita.com/tbuchi888/items/6807894368ad5b2a874d


2016/07/04
本記事をベースにKBファイル名などを変数化し、MSUファイルをlsコマンドで動的に取得して複数KBをインストールする記事を投稿しました。
http://qiita.com/tbuchi888/items/c23b856b16aba4ed494f


1.大まかな流れ

Ansibleサーバで
0. 事前にプレイブック実行ディレクトリにpatchディレクトリ作成と配下に対象のmsuファイルをダウンロードしておく

Ansible ServerからターゲットのWindowsへ

1. msuファイルをコピーするためのディレクトリを作成
2. msuファイルをコピー
3. msuファイルを圧縮解凍
4. pkgmgrでサイレントインストール

Serverspec で
5. wmic qfeの実行結果に該当のKBが含まれているか確認


2.Ansible playbook

インベントリファイル: hosts

[win]
win0[1:2]

[win:vars]
ansible_user=ansible
ansible_password=password
ansible_port=5985
ansible_connection=winrm

playbook: install_KB.yml

install_KB.yml
---
- hosts: win
# Don't gather hosts facts for performance
  gather_facts: no
# Setting the task
  tasks:
   - name: create directory
     win_file: path=c:/work/ state=directory
   - name: copy the msu file
     win_copy: src=patch/Windows8.1-KB3079904-x64.msu dest=c:/work/Windows8.1-KB3079904-x64.msu
   - name: decompression the msu file
     raw: wusa C:/work/Windows8.1-KB3079904-x64.msu /extract:C:/work/
   - name: install KB
     raw: pkgmgr /n:C:/work/Windows8.1-KB3079904-x64.xml /quiet /norestart

playbook: uninstall_KB.yml

uninstall_KB.yml
---
- hosts: win
# Don't gather hosts facts for performance
  gather_facts: no
# Setting the task
  tasks:
   - name: uninstall KB 
     raw: wusa /uninstall /kb:3079904 /quiet /norestart

3.ServerSpec

chk_KB_spec.rb
require 'spec_helper'

describe command('wmic qfe') do
  its(:stdout) { should contain('KB3079904') }
end

4.参考

ansibleのplaybookでServerspecのように確認行う場合は以下

check_KB1.yml
---
- hosts: win
# Don't gather hosts facts for performance
  gather_facts: no
# Setting the task
  tasks:
   - name: Check KB
     raw: wmic qfe
     register: command_result
     failed_when: "'KB3079904' not in command_result.stdout"

または、findstrで対象が見つからないとエラー("failed": true, "rc": 1,)となるので

check_KB2.yml
---
- hosts: win
# Don't gather hosts facts for performance
  gather_facts: no
# Setting the task
  tasks:
   - name: Check KB
     raw: wmic qfe | findstr "KB3079904"
8
8
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
8
8