win_product_facts: New module to get Windows product information (#35206)
* win_setup: Add Product ID and Product Key in facts So this is actually a very nice way to get product key information from systems collected centrally. Especially with systems that have been upgraded from Windows 7 or Windows 8 to Windows 10 may not have a valid Windows 10 product license key printed anywhere, it was a digital license. If you ever have to reinstall the system, you may recover the system from the recovery partition, or the original media, but cannot upgrade to Windows 10 for free. By collecting the product key, one can always reinstall your free Windows upgrade. My only question is, do we want this to be part of the default facts, as it may be considered important information. Or should we make a special **win_product_key_facts** ? * Add ACPI product key support * Add integration test * Remove Get-ProductKey function, move inline
This commit is contained in:
parent
4a1cc661c4
commit
2a4b3ef0c1
4 changed files with 115 additions and 0 deletions
67
lib/ansible/modules/windows/win_product_facts.ps1
Normal file
67
lib/ansible/modules/windows/win_product_facts.ps1
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
#!powershell
|
||||||
|
# This file is part of Ansible
|
||||||
|
|
||||||
|
# Copyright: (c) 2017, Dag Wieers (dagwieers) <dag@wieers.com>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||||
|
|
||||||
|
$ErrorActionPreference = "Stop"
|
||||||
|
|
||||||
|
$params = Parse-Args -arguments $args -supports_check_mode $true
|
||||||
|
|
||||||
|
$result = @{
|
||||||
|
changed = $false
|
||||||
|
ansible_facts = @{
|
||||||
|
ansible_os_product_id = (Get-CimInstance Win32_OperatingSystem).SerialNumber
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# First try to find the product key from ACPI
|
||||||
|
try {
|
||||||
|
$product_key = (Get-CimInstance -Class SoftwareLicensingService).OA3xOriginalProductKey
|
||||||
|
} catch {
|
||||||
|
$product_key = $null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $product_key) {
|
||||||
|
# Else try to get it from the registry instead
|
||||||
|
try {
|
||||||
|
$data = Get-ItemPropertyValue -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion" -Name DigitalProductId
|
||||||
|
} catch {
|
||||||
|
$data = $null
|
||||||
|
}
|
||||||
|
|
||||||
|
# And for Windows 2008 R2
|
||||||
|
if (-not $data) {
|
||||||
|
try {
|
||||||
|
$data = Get-ItemPropertyValue -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion" -Name DigitalProductId4
|
||||||
|
} catch {
|
||||||
|
$data = $null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($data) {
|
||||||
|
$product_key = $null
|
||||||
|
$hexdata = $data[52..66]
|
||||||
|
$chardata = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
|
||||||
|
|
||||||
|
# Decode base24 binary data
|
||||||
|
for ($i = 24; $i -ge 0; $i--) {
|
||||||
|
$k = 0
|
||||||
|
for ($j = 14; $j -ge 0; $j--) {
|
||||||
|
$k = $k * 256 -bxor $hexdata[$j]
|
||||||
|
$hexdata[$j] = [math]::truncate($k / 24)
|
||||||
|
$k = $k % 24
|
||||||
|
}
|
||||||
|
$product_key = $chardata[$k] + $product_key
|
||||||
|
if (($i % 5 -eq 0) -and ($i -ne 0)) {
|
||||||
|
$product_key = "-" + $product_key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result.ansible_facts.ansible_os_product_key = $product_key
|
||||||
|
|
||||||
|
Exit-Json -obj $result
|
36
lib/ansible/modules/windows/win_product_facts.py
Normal file
36
lib/ansible/modules/windows/win_product_facts.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2017, Dag Wieers (dagwieers) <dag@wieers.com>
|
||||||
|
# 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 = '''
|
||||||
|
---
|
||||||
|
module: win_product_facts
|
||||||
|
short_description: Provides Windows product information (product id, product key)
|
||||||
|
description:
|
||||||
|
- Provides Windows product information.
|
||||||
|
version_added: '2.5'
|
||||||
|
author:
|
||||||
|
- Dag Wieers (@dagwieers)
|
||||||
|
options: {}
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = r'''
|
||||||
|
- name: Get product id and product key
|
||||||
|
win_product_facts:
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
ansible_facts:
|
||||||
|
description: returned facts by this module
|
||||||
|
returned: always
|
||||||
|
type: dictionary
|
||||||
|
sample:
|
||||||
|
ansible_os_product_id: 00326-10000-00000-AA698
|
||||||
|
ansible_os_product_key: T49TD-6VFBW-VV7HY-B2PXY-MY47H
|
||||||
|
'''
|
1
test/integration/targets/win_product_facts/aliases
Normal file
1
test/integration/targets/win_product_facts/aliases
Normal file
|
@ -0,0 +1 @@
|
||||||
|
windows/ci/group3
|
11
test/integration/targets/win_product_facts/tasks/main.yml
Normal file
11
test/integration/targets/win_product_facts/tasks/main.yml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# This file is part of Ansible
|
||||||
|
|
||||||
|
# Copyright: (c) 2017, Dag Wieers (dagwieers) <dag@wieers.com>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
- win_product_facts:
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- ansible_os_product_id is defined
|
||||||
|
- ansible_os_product_key is defined
|
Loading…
Reference in a new issue