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
---
- 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
---
- 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
require 'spec_helper'
describe command('wmic qfe') do
its(:stdout) { should contain('KB3079904') }
end
4.参考
ansibleのplaybookでServerspecのように確認行う場合は以下
---
- 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,)となるので
---
- hosts: win
# Don't gather hosts facts for performance
gather_facts: no
# Setting the task
tasks:
- name: Check KB
raw: wmic qfe | findstr "KB3079904"