Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win_dsc: DSC Resources that specify 'System.ServiceProcess' as one of the 'RequiredAssemblies' are not discoverable using Get-DSCResource when run via Ansible #66

Closed
Pantharian opened this issue Jun 16, 2020 · 6 comments · Fixed by #67

Comments

@Pantharian
Copy link

Pantharian commented Jun 16, 2020

SUMMARY

If a custom DSC Resource contains a reference to System.ServiceProcess as one of the RequiredAssemblies in the respective .psd1 file, and the associated .psm1 file contains references to that assembly, the call to Get-DscResource in win_dsc, when run via the Ansible, cannot find that DSC Resource (even though it does exist) and the Ansible call fails.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_dsc

ANSIBLE VERSION
ansible 2.9.7
  config file = /media/sf_trusty64/Ansible_Configuration_Management/windows/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
CONFIGURATION

DISPLAY_SKIPPED_HOSTS(env: ANSIBLE_DISPLAY_SKIPPED_HOSTS) = False

OS / ENVIRONMENT

Windows 10

STEPS TO REPRODUCE
  1. Unzip these files (ExampleDscResource.zip) to C:\Program Files\WindowsPowerShell\Modules\ExampleDscResource\1.0.0.0:

  2. Run Get-DscResource to confirm the DSC Resource is installed correctly

  3. Create a playbook containing the following:

---
- name: Example Dsc Resource
  hosts: all
  tasks:
  - name: Run ExampleDscResource
    win_dsc:
      resource_name: "ExampleDscResource"
      Ensure: present
EXPECTED RESULTS

The DSC Resource runs:

PLAY [Example Dsc Resource] **********************************************************

TASK [Gathering Facts] *********************************************************
ok: [windows]

TASK [Run ExampleDscResource] **************************************************
ok: [windows]

PLAY RECAP *********************************************************************
windows                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ACTUAL RESULTS
PLAY [Example Dsc Resource] **********************************************************

TASK [Gathering Facts] *********************************************************
ok: [windows]

TASK [Run ExampleDscResource] **************************************************
fatal: [windows]: FAILED! => {"changed": false, "msg": "Resource 'ExampleDscResource' not found."}

PLAY RECAP *********************************************************************
windows                    : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Adding Add-Type -assemblyname System.ServiceProcess to win_dsc at the line preceding the call to Get-DscResource here causes the playbook to execute successfully.

@Pantharian Pantharian changed the title DSC Resources that specify 'System.ServiceProcess' as one of the 'RequiredAssemblies' are not discoverable using Get-DSCResource when run via Ansible using 'win_dsc' win_dsc: DSC Resources that specify 'System.ServiceProcess' as one of the 'RequiredAssemblies' are not discoverable using Get-DSCResource when run via Ansible Jun 16, 2020
@jborean93
Copy link
Collaborator

Do you have an example DSC resource that we can test with? The particular check where this is failing is very early on in the module and the only real call that happens is the Get-DscResource call. If that is working locally then it should be working remotely but something does seem to be up.

If you can provide an example resource I can try I'm happy to try and replicate the error and find a fix.

@Pantharian
Copy link
Author

Files to reproduce the problem and the corresponding playbook code are detailed above.

@jborean93
Copy link
Collaborator

I downloaded your example resource and I can replicate the error. The issue is because the System.ServiceProcess is not automatically loaded by default when PowerShell starts up so the class fails to parse. As you've said by manually running Add-Type -AssemblyName System.ServiceProcess in win_dsc will cause it to work for you, you could also just run any cmdlet like Get-Service and it will auto load it for you as well.

This isn't necessarily a problem with win_dsc but how PowerShell loads assemblies and how classes are parsed. There's a longstanding issue for this PowerShell/PowerShell#2074 that details what exactly is going on. You can even replicate this problem outside of Ansible by running powershell.exe -NoProfile Get-DscResource ExampleDscResource. The -NoProfile part is only there in case your profile has something that loaded the assembly. For me I don't have anything in my profile so I don't even need the -NoProfile part to replicate the error.

As for the fix, I don't think we can really manage a list of assemblies that we are manually load as that would become quite unwieldy to maintain. I'm trying to see if there are any other options we can go with here, one potential one referenced in that issue is to load the class as a separate file but I'm seeing if there are other options.

@jborean93
Copy link
Collaborator

Could you try out the changes in #67. I'm weirdly unable to replicate the error you have when running it through the module but this fixes the problem I have when loading it directly on the console.

@Pantharian
Copy link
Author

If I add just the additions you made in #67 to the win_dsc version for Ansible 2.9.7, then it produces the Expected Results.

If I use the contents of the file #67 and remove the changes you made, then it also produces the Expected Results!

The main difference between the version of win_dsc is that the #67 includes a new method Invoke-SafeDscResource.

@jborean93
Copy link
Collaborator

Yea that's the weirdness I found, in any case it doesn't hurt to have, will merge it in for the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants