ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
199 stars 153 forks source link

win_feature_info: The term ''Get-WindowsFeature'' is not recognized as the name of a cmdlet #434

Closed mfann-or closed 1 year ago

mfann-or commented 2 years ago
SUMMARY

The ServerManager PowerShell module doesn't always import automatically. Causing the Get-WindowsFeature cmdlet to not be found.

ISSUE TYPE
COMPONENT NAME

community.windows.win_feature_info

ANSIBLE VERSION
ansible [core 2.12.5.post0]
  config file = /workspaces/infrastructure/ansible/ansible.cfg
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
  ansible collection location = /workspaces/infrastructure/ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.8.12 (default, Sep 21 2021, 00:10:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 2.10.3
  libyaml = True
COLLECTION VERSION
# /workspaces/infrastructure/ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.windows 1.11.0 
CONFIGURATION
CALLBACKS_ENABLED(/workspaces/infrastructure/ansible/ansib/ansible.cfg) = ['ansible.posix.profile_tasks']
COLLECTIONS_PATHS(/workspaces/infrastructure/ansible/ansible.cfg) = ['/workspaces/infrastructure/ansible/collections', '/usr/sh>
DEFAULT_FORKS(/workspaces/infrastructure/ansible/ansible.cfg) = 15
DEFAULT_HOST_LIST(/workspaces/infrastructure/ansible/ansible.cfg) = ['/workspaces/infrastructure/ansible/inventory/global.aws_e>
DEFAULT_ROLES_PATH(/workspaces/infrastructure/ansible/ansible.cfg) = ['/workspaces/infrastructure/ansible/roles', '/usr/share/a>
DEFAULT_STDOUT_CALLBACK(/workspaces/infrastructure/ansible/ansible.cfg) = yaml
GALAXY_SERVER_LIST(/workspaces/infrastructure/ansible/ansible.cfg) = ['private_automation_hub', 'automation_hub', 'public_galaxy']
HOST_KEY_CHECKING(/workspaces/infrastructure/ansible/ansible.cfg) = False
OS / ENVIRONMENT

Controller: awx-ee Target: Windows Server 2019

STEPS TO REPRODUCE

Not all machines successfully load the ServerManager PowerShell Module automatically. This causes the Get-WindowsFeature cmdlet to not be found and fail.

- name: Get info for a single feature
      community.windows.win_feature_info:
        name: Web-Server
EXPECTED RESULTS

Successfully return info about the Windows Feature

ACTUAL RESULTS
TASK [Get info for a single feature] ********************************************************************************************************************************
task path: /workspaces/infrastructure/ansible/playbooks/ad_hoc/windows_services_info.yml:47
Friday 09 September 2022  22:02:28 +0000 (0:00:00.301)       0:00:00.301 ****** 
The full traceback is:
The term 'Get-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:26 char:13
+ $features = Get-WindowsFeature -Name $name
+             ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-WindowsFeature:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

ScriptStackTrace:
at <ScriptBlock>, <No file>: line 26
fatal: [dove-app1]: FAILED! => changed=false 
  msg: 'Unhandled exception while executing module: The term ''Get-WindowsFeature'' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'
POSSIBLE SOLUTION

This issue can be fixed by explicitly importing the ServerManager module in win_feature_info.ps1. Shown here w/ a try/catch block.

#!powershell

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#AnsibleRequires -CSharpUtil Ansible.Basic

$spec = @{
    options = @{
        name = @{ type = "str"; default = '*' }
    }
    supports_check_mode = $true
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)

$name = $module.Params.name

$module.Result.exists = $false

try {
    Import-Module -Name ServerManager
}
catch {
    $module.FailJson("The ServerManager module failed to load properly: $($_.Exception.Message)", $_)
}

$features = Get-WindowsFeature -Name $name

$module.Result.features = @(foreach ($feature in ($features)) {
        # These should closely reflect the options for win_feature
        @{
            name = $feature.Name
            display_name = $feature.DisplayName
            description = $feature.Description
            installed = $feature.Installed
            install_state = $feature.InstallState.ToString()
            feature_type = $feature.FeatureType
            path = $feature.Path
            depth = $feature.Depth
            depends_on = $feature.DependsOn
            parent = $feature.Parent
            server_component_descriptor = $feature.ServerComponentDescriptor
            sub_features = $feature.SubFeatures
            system_service = $feature.SystemService
            best_practices_model_id = $feature.BestPracticesModelId
            event_query = $feature.EventQuery
            post_configuration_needed = $feature.PostConfigurationNeeded
            additional_info = $feature.AdditionalInfo
        }
        $module.Result.exists = $true
    })

$module.ExitJson()
jborean93 commented 1 year ago

This shouldn't be needed as far as I know. PowerShell should know that Get-WindowsFeature is part of the ServerManager module and just autoload it for you. The fact this isn't happening is puzzling and needs to be figured out to either avoid the problem with this scenario for other modules. Are you saying this happens on only certain hosts but others are fine? Are there any differences/similarities with the hosts that do not work compared to the ones that do? How are you connecting to the hosts, is it through ssh/winrm/psrp/something else? What happens when you run Get-WindowsFeature on a brand new process on the server, does it also fail?

mfann-or commented 1 year ago

Agreed @jborean93 . After looking into this a bit further, I believe it is an internal problem with some of our instances. I'll go ahead and close this issue. Thanks for your time!