From e4c2cb96eaea7a948ec137cc955c7d1bf78a0025 Mon Sep 17 00:00:00 2001 From: David O'Brien Date: Fri, 5 Feb 2016 18:03:07 +0100 Subject: [PATCH 1/3] Add documentation to setup.py and remove use of $params.fact_path in setup.ps1 --- system/setup.py | 11 ++++++++--- windows/setup.ps1 | 31 +++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/system/setup.py b/system/setup.py index c3f19eda170..beebd42b8ef 100644 --- a/system/setup.py +++ b/system/setup.py @@ -54,12 +54,14 @@ notes: install I(facter) and I(ohai) means you can avoid Ruby-dependencies on your remote systems. (See also M(facter) and M(ohai).) - The filter option filters only the first level subkey below ansible_facts. - - If the target host is Windows, you will not currently have the ability to use - C(fact_path) or C(filter) as this is provided by a simpler implementation of the module. - Different facts are returned for Windows hosts. + - If the target host is Windows you can now use C(fact_path). Make sure that this path + exists on the target host. Files in this path MUST be PowerShell scripts (*.ps1) and + their output must be formattable in JSON (Ansible will take care of this). Test the + output of your scripts. author: - "Ansible Core Team" - "Michael DeHaan" + - "David O'Brien @david_obrien davidobrien1985" ''' EXAMPLES = """ @@ -74,6 +76,9 @@ ansible all -m setup -a 'filter=facter_*' # Display only facts about certain interfaces. ansible all -m setup -a 'filter=ansible_eth[0-2]' + +# Display facts from Windows hosts with custom facts stored in C(C:\\custom_facts). +ansible windows -m setup -a "fact_path='c:\\custom_facts'" """ diff --git a/windows/setup.ps1 b/windows/setup.ps1 index fcac689b1e7..08c47553a17 100644 --- a/windows/setup.ps1 +++ b/windows/setup.ps1 @@ -17,14 +17,41 @@ # WANT_JSON # POWERSHELL_COMMON -# $params is not currently used in this module -# $params = Parse-Args $args; +# enabled $params (David O'Brien, 06/08/2015) +$params = Parse-Args $args; + + +Function Get-CustomFacts { + [cmdletBinding()] + param ( + [Parameter(mandatory=$false)] + $factpath = $null + ) + + if (-not (Test-Path -Path $factpath)) { + Fail-Json $result "The path $factpath does not exist. Typo?" + } + + $FactsFiles = Get-ChildItem -Path $factpath | Where-Object -FilterScript {($PSItem.PSIsContainer -eq $false) -and ($PSItem.Extension -eq '.ps1')} + + foreach ($FactsFile in $FactsFiles) { + $out = . $($FactsFile.FullName) + Set-Attr $result.ansible_facts "ansible_$(($FactsFile.Name).Split('.')[0])" $out + } +} $result = New-Object psobject @{ ansible_facts = New-Object psobject changed = $false }; +# failifempty = $false is default and thus implied +$factpath = Get-AnsibleParam -obj $params -name fact_path +if ($factpath -ne $null) { + # Get any custom facts + Get-CustomFacts -factpath $factpath +} + $win32_os = Get-CimInstance Win32_OperatingSystem $win32_cs = Get-CimInstance Win32_ComputerSystem $osversion = [Environment]::OSVersion From 18047506af275791350895ff73fee9bac38002f5 Mon Sep 17 00:00:00 2001 From: davidobrien1 Date: Fri, 26 Feb 2016 17:09:54 +1100 Subject: [PATCH 2/3] Change fact invocation from dotsourcing to ampersand so that scripts do not execute in global scope. --- windows/setup.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows/setup.ps1 b/windows/setup.ps1 index 08c47553a17..63e5597d861 100644 --- a/windows/setup.ps1 +++ b/windows/setup.ps1 @@ -35,7 +35,7 @@ Function Get-CustomFacts { $FactsFiles = Get-ChildItem -Path $factpath | Where-Object -FilterScript {($PSItem.PSIsContainer -eq $false) -and ($PSItem.Extension -eq '.ps1')} foreach ($FactsFile in $FactsFiles) { - $out = . $($FactsFile.FullName) + $out = & $($FactsFile.FullName) Set-Attr $result.ansible_facts "ansible_$(($FactsFile.Name).Split('.')[0])" $out } } From b33320d3f115326fd06d32bac85c516c88e5a623 Mon Sep 17 00:00:00 2001 From: davidobrien1 Date: Fri, 26 Feb 2016 22:40:43 +1100 Subject: [PATCH 3/3] Restore filter on Windows and version reference for Windows facts. --- system/setup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/system/setup.py b/system/setup.py index beebd42b8ef..8830e9d255d 100644 --- a/system/setup.py +++ b/system/setup.py @@ -35,7 +35,7 @@ options: description: - path used for local ansible facts (*.fact) - files in this dir will be run (if executable) and their results be added to ansible_local facts - if a file is not executable it is read. + if a file is not executable it is read. Check notes for Windows options. (from 2.1 on) File/results format can be json or ini-format required: false default: '/etc/ansible/facts.d' @@ -54,10 +54,13 @@ notes: install I(facter) and I(ohai) means you can avoid Ruby-dependencies on your remote systems. (See also M(facter) and M(ohai).) - The filter option filters only the first level subkey below ansible_facts. + - If the target host is Windows, you will not currently have the ability to use + C(filter) as this is provided by a simpler implementation of the module. - If the target host is Windows you can now use C(fact_path). Make sure that this path exists on the target host. Files in this path MUST be PowerShell scripts (*.ps1) and their output must be formattable in JSON (Ansible will take care of this). Test the output of your scripts. + This option was added in Ansible 2.1. author: - "Ansible Core Team" - "Michael DeHaan"