From 6f7835a73f590c7412f5919f7a3ae2fb0d973e0b Mon Sep 17 00:00:00 2001 From: Phil Schwartz Date: Tue, 30 Dec 2014 08:42:00 -0600 Subject: [PATCH] init commit --- .../modules/extras/windows/win_unzip.ps1 | 83 +++++++++++++++++ .../modules/extras/windows/win_unzip.py | 90 +++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 lib/ansible/modules/extras/windows/win_unzip.ps1 create mode 100644 lib/ansible/modules/extras/windows/win_unzip.py diff --git a/lib/ansible/modules/extras/windows/win_unzip.ps1 b/lib/ansible/modules/extras/windows/win_unzip.ps1 new file mode 100644 index 00000000000..de9fb73e7ce --- /dev/null +++ b/lib/ansible/modules/extras/windows/win_unzip.ps1 @@ -0,0 +1,83 @@ +#!powershell +# This file is part of Ansible +# +# Copyright 2014, Phil Schwartz +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# WANT_JSON +# POWERSHELL_COMMON + +$params = Parse-Args $args; + +$result = New-Object psobject @{ + win_unzip = New-Object psobject + changed = $false +} + +If ($params.zip) { + $zip = $params.zip.toString() + + If (-Not (Test-Path -path $zip)){ + Fail-Json $result "zip file: $zip does not exist." + } +} +Else { + Fail-Json $result "missing required argument: zip" +} + +If (-Not($params.dest -eq $null)) { + $dest = $params.dest.toString() + + If (-Not (Test-Path $dest -PathType Container)){ + New-Item -itemtype directory -path $dest + } +} +Else { + Fail-Json $result "missing required argument: dest" +} + +Try { + cd C:\ + $shell = New-Object -ComObject Shell.Application + $shell.NameSpace($dest).copyhere(($shell.NameSpace($zip)).items(), 20) + $result.changed = $true +} +Catch { + $sp = $zip.split(".") + $ext = $sp[$sp.length-1] + + # Used to allow reboot after exe hotfix extraction (Windows 2008 R2 SP1) + # This will have no effect in most cases. + If (-Not ($ext -eq "exe")){ + $result.changed = $false + Fail-Json $result "Error unzipping $zip to $dest" + } +} + +If ($params.rm -eq "true"){ + Remove-Item $zip -Recurse -Force + Set-Attr $result.win_unzip "rm" "true" +} + +If ($params.restart -eq "true") { + Restart-Computer -Force + Set-Attr $result.win_unzip "restart" "true" +} + + +Set-Attr $result.win_unzip "zip" $zip.toString() +Set-Attr $result.win_unzip "dest" $dest.toString() + +Exit-Json $result; diff --git a/lib/ansible/modules/extras/windows/win_unzip.py b/lib/ansible/modules/extras/windows/win_unzip.py new file mode 100644 index 00000000000..ae2bfa94ad8 --- /dev/null +++ b/lib/ansible/modules/extras/windows/win_unzip.py @@ -0,0 +1,90 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2014, Phil Schwartz +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# this is a windows documentation stub. actual code lives in the .ps1 +# file of the same name + +DOCUMENTATION = ''' +--- +module: win_unzip +version_added: "" +short_description: Unzips compressed files on the Windows node +description: + - Unzips compressed files, and can force reboot (if needed, i.e. such as hotfixes). +options: + zip: + description: + - Zip file to be unzipped (provide absolute path) + required: true + default: null + aliases: [] + dest: + description: + - Destination of zip file (provide absolute path of directory) + required: true + default: null + aliases: [] + rm: + description: + - Remove the zip file, after unzipping + required: no + default: false + aliases: [] + restart: + description: + - Restarts the computer after unzip, can be useful for hotfixes such as http://support.microsoft.com/kb/2842230 (Restarts will have to be accounted for with wait_for module) + choices: + - true + - false + required: false + default: false + aliases: [] +author: Phil Schwartz +''' + +EXAMPLES = ''' +# This unzips hotfix http://support.microsoft.com/kb/2842230 and forces reboot (for hotfix to take effect) +$ ansible -i hosts -m win_unzip -a "zip=C:\\463984_intl_x64_zip.exe dest=C:\\Hotfix restart=true" all +# This unzips a library that was downloaded with win_get_url, and removes the file after extraction +$ ansible -i hosts -m win_unzip -a "zip=C:\\LibraryToUnzip.zip dest=C:\\Lib rm=true" all +# Playbook example +--- +- name: Install WinRM PowerShell Hotfix for Windows Server 2008 SP1 + hosts: all + gather_facts: false + tasks: + - name: Grab Hotfix from URL + win_get_url: + url: 'http://hotfixv4.microsoft.com/Windows%207/Windows%20Server2008%20R2%20SP1/sp2/Fix467402/7600/free/463984_intl_x64_zip.exe' + dest: 'C:\\463984_intl_x64_zip.exe' + - name: Unzip hotfix + win_unzip: + zip: "C:\\463984_intl_x64_zip.exe" + dest: "C:\\Hotfix" + restart: true + - name: Wait for server reboot... + local_action: + module: wait_for + host={{ inventory_hostname }} + port={{ansible_ssh_port|default(5986)}} + delay=15 + timeout=600 + state=started +'''