Win data deduplication module (#59723)

* win_data_deduplication initial commit

* Ansible version added bump

* integration tests

* missing aliases

* Fixing documentation

* license and metadata

* documentation formating

* removing win_format ref

* documentation fixes

* trailing whitespace

* Fixing more documentation :(

* missing return

* documentation cleanup

* align copyright with doc

* indentation fixes...

* updated examples

* ignore meta and future for python doc

* removing when

* use Get-PSVolume

* Get-Volume not PSVolume

* missing updated var

* updated old drive refs

* make sure that the T drive is formated as NTFS

* path and drive_letter are exclusive

* idempotence test fix

* changing task order + reboot timeout

* implementing the requested changes to the code

* updating documentation to reflect code changes

* simplifying tests

* missing feature install before running the tasks

* pslint trailing whitespace

* putting old tests back

* missing win_format

* skip windows 2012

* Fixing message for OS check

* pass settings and dedup_job variables

* Removing unnecessary module

* logic issue

* replacing tabs with double space

* documentation fix + removing tabs

* Update documentation with recent changes

* Apply changes requested

* switch feature install with partition format

* replace tabs with spaces

* trailing whitespace

* we don't need those ignores anymore

* minor fixes

* updated test to match latest code changes

* removing dedup job task

* adding check mode yes test

* fixes for check_mode support

* updating examples in documentation

* wrong indentation for check_mode in tests

* convert indentation to spaces

* -not $check_mode

* removing unneeded spec in documentation

* Switch to Ansible.Basic

* 2.9 is already gone, so let's add this module to 2.10...

* removing useless else condition

* updated documentation

* fixing specs and removing useless try/catch + fix exit/fail

* spaces indentation

* $null check is actually needed if volume never had dedup

* Missing check_mode update

* removing required for default state in documentation

* converted tabs to spaces
This commit is contained in:
rnsc 2019-12-04 05:21:21 +01:00 committed by Jordan Borean
parent 584824f560
commit cfb6cb4cef
9 changed files with 325 additions and 0 deletions

View file

@ -0,0 +1,129 @@
#!powershell
# Copyright: 2019, rnsc(@rnsc) <github@rnsc.be>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt
#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -OSVersion 6.3
$spec = @{
options = @{
drive_letter = @{ type = "str"; required = $true }
state = @{ type = "str"; choices = "absent", "present"; default = "present"; }
settings = @{
type = "dict"
required = $false
options = @{
minimum_file_size = @{ type = "int"; default = 32768 }
minimum_file_age_days = @{ type = "int"; default = 2 }
no_compress = @{ type = "bool"; required = $false; default = $false }
optimize_in_use_files = @{ type = "bool"; required = $false; default = $false }
verify = @{ type = "bool"; required = $false; default = $false }
}
}
}
supports_check_mode = $true
}
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
$drive_letter = $module.Params.drive_letter
$state = $module.Params.state
$settings = $module.Params.settings
$module.Result.changed = $false
$module.Result.reboot_required = $false
$module.Result.msg = ""
function Set-DataDeduplication($volume, $state, $settings, $dedup_job) {
$current_state = 'absent'
try {
$dedup_info = Get-DedupVolume -Volume "$($volume.DriveLetter):"
} catch {
$dedup_info = $null
}
if ($dedup_info.Enabled) {
$current_state = 'present'
}
if ( $state -ne $current_state ) {
if( -not $module.CheckMode) {
if($state -eq 'present') {
# Enable-DedupVolume -Volume <String>
Enable-DedupVolume -Volume "$($volume.DriveLetter):"
} elseif ($state -eq 'absent') {
Disable-DedupVolume -Volume "$($volume.DriveLetter):"
}
}
$module.Result.changed = $true
}
if ($state -eq 'present') {
if ($null -ne $settings) {
Set-DataDedupJobSettings -volume $volume -settings $settings
}
}
}
function Set-DataDedupJobSettings ($volume, $settings) {
try {
$dedup_info = Get-DedupVolume -Volume "$($volume.DriveLetter):"
} catch {
$dedup_info = $null
}
ForEach ($key in $settings.keys) {
# See Microsoft documentation:
# https://docs.microsoft.com/en-us/powershell/module/deduplication/set-dedupvolume?view=win10-ps
$update_key = $key
$update_value = $settings.$($key)
# Transform Ansible style options to Powershell params
$update_key = $update_key -replace('_', '')
if ($update_key -eq "MinimumFileSize" -and $update_value -lt 32768) {
$update_value = 32768
}
$current_value = ($dedup_info | Select-Object -ExpandProperty $update_key)
if ($update_value -ne $current_value) {
$command_param = @{
$($update_key) = $update_value
}
# Set-DedupVolume -Volume <String>`
# -NoCompress <bool> `
# -MinimumFileAgeDays <UInt32> `
# -MinimumFileSize <UInt32> (minimum 32768)
if( -not $module.CheckMode ) {
Set-DedupVolume -Volume "$($volume.DriveLetter):" @command_param
}
$module.Result.changed = $true
}
}
}
# Install required feature
$feature_name = "FS-Data-Deduplication"
if( -not $module.CheckMode) {
$feature = Install-WindowsFeature -Name $feature_name
if ($feature.RestartNeeded -eq 'Yes') {
$module.Result.reboot_required = $true
$module.FailJson("$feature_name was installed but requires Windows to be rebooted to work.")
}
}
$volume = Get-Volume -DriveLetter $drive_letter
Set-DataDeduplication -volume $volume -state $state -settings $settings -dedup_job $dedup_job
$module.ExitJson()

View file

@ -0,0 +1,87 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: 2019, rnsc(@rnsc) <github@rnsc.be>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = r'''
---
module: win_data_deduplication
version_added: "2.10"
short_description: Module to enable Data Deduplication on a volume.
description:
- This module can be used to enable Data Deduplication on a Windows volume.
- The module will install the FS-Data-Deduplication feature (a reboot will be necessary).
options:
drive_letter:
description:
- Windows drive letter on which to enable data deduplication.
required: yes
type: str
state:
description:
- Wether to enable or disable data deduplication on the selected volume.
default: present
type: str
choices: [ present, absent ]
settings:
description:
- Dictionary of settings to pass to the Set-DedupVolume powershell command.
type: dict
suboptions:
minimum_file_size:
description:
- Minimum file size you want to target for deduplication.
- It will default to 32768 if not defined or if the value is less than 32768.
type: int
default: 32768
minimum_file_age_days:
description:
- Minimum file age you want to target for deduplication.
type: int
default: 2
no_compress:
description:
- Wether you want to enabled filesystem compression or not.
type: bool
default: no
optimize_in_use_files:
description:
- Indicates that the server attempts to optimize currently open files.
type: bool
default: no
verify:
description:
- Indicates whether the deduplication engine performs a byte-for-byte verification for each duplicate chunk
that optimization creates, rather than relying on a cryptographically strong hash.
- This option is not recommend.
- Setting this parameter to True can degrade optimization performance.
type: bool
default: no
author:
- rnsc (@rnsc)
'''
EXAMPLES = r'''
- name: Enable Data Deduplication on D
win_data_deduplication:
drive_letter: 'D'
state: present
- name: Enable Data Deduplication on D
win_data_deduplication:
drive_letter: 'D'
state: present
settings:
no_compress: true
minimum_file_age_days: 1
minimum_file_size: 0
'''
RETURN = r'''
#
'''

View file

@ -0,0 +1,4 @@
shippable/windows/group4
skip/windows/2008
skip/windows/2008-R2
skip/windows/2012

View file

@ -0,0 +1,2 @@
dependencies:
- setup_remote_tmp_dir

View file

@ -0,0 +1,2 @@
---
- include: pre_test.yml

View file

@ -0,0 +1,40 @@
---
- set_fact:
AnsibleVhdx: '{{ remote_tmp_dir }}\AnsiblePart.vhdx'
- name: Install FS-Data-Deduplication
win_feature:
name: FS-Data-Deduplication
include_sub_features: true
state: present
register: data_dedup_feat_reg
- name: Reboot windows after the feature has been installed
win_reboot:
reboot_timeout: 3600
when:
- data_dedup_feat_reg.success
- data_dedup_feat_reg.reboot_required
- name: Copy VHDX scripts
win_template:
src: "{{ item.src }}"
dest: '{{ remote_tmp_dir }}\{{ item.dest }}'
loop:
- { src: partition_creation_script.j2, dest: partition_creation_script.txt }
- { src: partition_deletion_script.j2, dest: partition_deletion_script.txt }
- name: Create partition
win_command: diskpart.exe /s {{ remote_tmp_dir }}\partition_creation_script.txt
- name: Format T with NTFS
win_format:
drive_letter: T
file_system: ntfs
- name: Run tests
block:
- include: tests.yml
always:
- name: Detach disk
win_command: diskpart.exe /s {{ remote_tmp_dir }}\partition_deletion_script.txt

View file

@ -0,0 +1,47 @@
---
- name: Enable Data Deduplication on the T drive - check mode
win_data_deduplication:
drive_letter: "T"
state: present
settings:
no_compress: true
minimum_file_age_days: 2
minimum_file_size: 0
check_mode: yes
register: win_data_deduplication_enable_check_mode
- name: Check that it was successful with a change - check mode
assert:
that:
- win_data_deduplication_enable_check_mode is changed
- name: Enable Data Deduplication on the T drive
win_data_deduplication:
drive_letter: "T"
state: present
settings:
no_compress: true
minimum_file_age_days: 2
minimum_file_size: 0
register: win_data_deduplication_enable
- name: Check that it was successful with a change
assert:
that:
- win_data_deduplication_enable is changed
- name: Enable Data Deduplication on the T drive
win_data_deduplication:
drive_letter: "T"
state: present
settings:
no_compress: true
minimum_file_age_days: 2
minimum_file_size: 0
register: win_data_deduplication_enable_again
- name: Check that it was successful without a change
assert:
that:
- win_data_deduplication_enable_again is not changed

View file

@ -0,0 +1,11 @@
create vdisk file="{{ AnsibleVhdx }}" maximum=2000 type=fixed
select vdisk file="{{ AnsibleVhdx }}"
attach vdisk
convert mbr
create partition primary
assign letter="T"

View file

@ -0,0 +1,3 @@
select vdisk file="{{ AnsibleVhdx }}"
detach vdisk