使用 Ansible 执行 Powershell DSC
Using Ansible to execute Powershell DSC
我的最终目标是使用 Ansible 在 Server 2016 服务器上配置 AdcsCertificationAuthority。
- name: Install ADCS with sub features and management tools
win_feature:
name: Adcs-Cert-Authority
state: present
include_management_tools: yes
register: win_feature
- name: reboot if installing Adcs-Cert-Authority feature requires it
win_reboot:
when: win_feature.reboot_required
- name: Add ActiveDirectoryCSDsc
win_psmodule:
name: ActiveDirectoryCSDsc
state: present
- name: Configure AdcsCertificationAuthority Powershell DSC
win_dsc:
resource_name: AdcsCertificationAuthority
IsSingleInstance: 'Yes'
CAType: 'EnterpriseRootCA'
CryptoProviderName: 'RSA#Microsoft Software Key Storage Provider'
KeyLength: 2048
HashAlgorithmName: 'SHA256'
ValidityPeriod: 'Years'
ValidityPeriodUnits: 99
PsDscRunAsCredential_username: ' {{ ansible_user }}'
PsDscRunAsCredentual_password: '{{ ansible_password }}'
DSC 部分失败,但我不确定如何确定错误的来源及其含义。
TASK [internal/qa_env_dc : Configure AdcsCertificationAuthority Powershell DSC] *************************************************************************************************************************************************************
fatal: [10.0.136.5]: FAILED! => {"changed": false, "module_stderr": "Exception calling \"Run\" with \"1\" argument(s): \"Exception calling \"Invoke\" with \"0\" argument(s): \"The running command \r\nstopped because the preference variable \"ErrorActionPreference\" or common parameter is set to Stop: Cannot bind \r\nargument to parameter 'String' because it is null.\"\"\r\nAt line:65 char:5\r\n+ $output = $entrypoint.Run($payload)\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException\r\n + FullyQualifiedErrorId : ScriptMethodRuntimeException\r\n \r\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
我实际上是在尝试重新创建我直接使用 Powershell 所做的事情。
Add-WindowsFeature Adcs-Cert-Authority -IncludeManagementTools
Install-AdcsCertificationAuthority -CAType EnterpriseRootCa -CryptoProviderName "RSA#Microsoft Software Key Storage Provider" -KeyLength 2048 -HashAlgorithmName SHA256 -ValidityPeriod Years -ValidityPeriodUnits 99 -Credential $mycreds -Force:$true
我的ansible_user和ansible_password是Domain Administrator帐号的,所以我觉得我的权限应该没问题。
我使用的 DSC 模块的 github 回购并不真正直接与 ansible 相关,所以那里没有任何帮助,但它是我获取参数的地方。
https://github.com/PowerShell/ActiveDirectoryCSDsc
我还试图从 ansible 示例中复制我的部署。
https://docs.ansible.com/ansible/2.5/modules/win_dsc_module.html
不幸的是,Ansible 不会在这种情况下帮助您。
最好的方法是使用相同的参数单独调试 DSC 部分。在这种情况下,它有点糟糕,因为这是一个很大的问题。如果成功,您将设置 CA。如果可以,请部署一个测试环境,为了理智起见,您可以不断地拆除和启动它。
如果幸运的话,您会发现 Test
方法中的问题不会改变任何东西。
第一步,进入您运行宁win_dsc
反对的主机。打开 PowerShell。
创建一个包含所有 DSC 模块参数的 [hashtable]
,像这样:
if (-not $cred) {
$cred = Get-Credential # maybe just run this once in your session?
}
$params = @{
IsSingleInstance = $true
CAType = 'EnterpriseRootCA'
CryptoProviderName = 'RSA#Microsoft Software Key Storage Provider'
KeyLength = 2048
HashAlgorithmName = 'SHA256'
ValidityPeriod = 'Years'
ValidityPeriodUnits = 99
PsDscRunAsCredential = $cred
}
接下来,直接调用DSC资源,我们使用Test
方法:
Invoke-DscResource -Name AdcsCertificationAuthority -ModuleName ActiveDirectoryCSDsc -Property $params -Verbose -Method Test
看看它吐出了什么。它可能会因类似的错误而失败。希望它能做到。如果没有,请尝试 Get
方法,以防 Set
使用它但 Test
不使用它。这不太可能,但您希望尽可能避免 Set
。
如果运行一切顺利,运行用方法Set
。如果成功,返回 ansible 并找出不同之处(用户 ansible 是否正在验证是否具有调用 DSC 的权限?)。
如果您在任何时候遇到失败并想要深入挖掘,您可以调试实际的 DSC 调用。有点绕。
第一,Enable-DscDebug -BreakAll
.
接下来,打开一个单独的 PowerShell ISE window(这是我的偏好,让事情变得更容易)。然后,重新运行您之前执行的Invoke-DscResource
命令,在相同的原始window中(不是新的ISE window)。
它会中断,它会给你一系列命令 运行 连接到调试会话。该列表将包括 Enter-PSHostProcess
。 运行 ISE 终端中的那些命令window.
您将进入 运行ning DSC 过程,您将看到模块的源代码并能够逐句查看它并找出问题所在。
此时,您可能会发现您传递的参数不太正确,您可以通过调整它来修复调用。不错。
您可能会发现模块中存在错误,在这种情况下您可以报告错误,甚至可以通过拉取请求提供修复;这需要时间。
与此同时,您可以自己克隆该模块并通过快速修复将其分发到您的服务器,但不符合 PR 的要求。
这里有很多可能性,但如果您发现实际错误,则可能需要提出一个关于如何处理该特定问题的新问题。
备注
我发现在调试过程中,大约有一半的时间连接到会话会导致完全卡住的调试会话无法正常工作。在那种情况下,使用他们给你的 PID 并终止进程。无论如何,您可能必须在 运行 秒之间执行此操作,不要害怕。
最后,在尝试再次使用 DSC 之前(比如从 Ansible 中),不要忘记禁用调试!
Disable-DscDebug
(强烈建议您在禁用调试后终止进程)
我的最终目标是使用 Ansible 在 Server 2016 服务器上配置 AdcsCertificationAuthority。
- name: Install ADCS with sub features and management tools
win_feature:
name: Adcs-Cert-Authority
state: present
include_management_tools: yes
register: win_feature
- name: reboot if installing Adcs-Cert-Authority feature requires it
win_reboot:
when: win_feature.reboot_required
- name: Add ActiveDirectoryCSDsc
win_psmodule:
name: ActiveDirectoryCSDsc
state: present
- name: Configure AdcsCertificationAuthority Powershell DSC
win_dsc:
resource_name: AdcsCertificationAuthority
IsSingleInstance: 'Yes'
CAType: 'EnterpriseRootCA'
CryptoProviderName: 'RSA#Microsoft Software Key Storage Provider'
KeyLength: 2048
HashAlgorithmName: 'SHA256'
ValidityPeriod: 'Years'
ValidityPeriodUnits: 99
PsDscRunAsCredential_username: ' {{ ansible_user }}'
PsDscRunAsCredentual_password: '{{ ansible_password }}'
DSC 部分失败,但我不确定如何确定错误的来源及其含义。
TASK [internal/qa_env_dc : Configure AdcsCertificationAuthority Powershell DSC] *************************************************************************************************************************************************************
fatal: [10.0.136.5]: FAILED! => {"changed": false, "module_stderr": "Exception calling \"Run\" with \"1\" argument(s): \"Exception calling \"Invoke\" with \"0\" argument(s): \"The running command \r\nstopped because the preference variable \"ErrorActionPreference\" or common parameter is set to Stop: Cannot bind \r\nargument to parameter 'String' because it is null.\"\"\r\nAt line:65 char:5\r\n+ $output = $entrypoint.Run($payload)\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException\r\n + FullyQualifiedErrorId : ScriptMethodRuntimeException\r\n \r\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
我实际上是在尝试重新创建我直接使用 Powershell 所做的事情。
Add-WindowsFeature Adcs-Cert-Authority -IncludeManagementTools
Install-AdcsCertificationAuthority -CAType EnterpriseRootCa -CryptoProviderName "RSA#Microsoft Software Key Storage Provider" -KeyLength 2048 -HashAlgorithmName SHA256 -ValidityPeriod Years -ValidityPeriodUnits 99 -Credential $mycreds -Force:$true
我的ansible_user和ansible_password是Domain Administrator帐号的,所以我觉得我的权限应该没问题。
我使用的 DSC 模块的 github 回购并不真正直接与 ansible 相关,所以那里没有任何帮助,但它是我获取参数的地方。
https://github.com/PowerShell/ActiveDirectoryCSDsc
我还试图从 ansible 示例中复制我的部署。
https://docs.ansible.com/ansible/2.5/modules/win_dsc_module.html
不幸的是,Ansible 不会在这种情况下帮助您。
最好的方法是使用相同的参数单独调试 DSC 部分。在这种情况下,它有点糟糕,因为这是一个很大的问题。如果成功,您将设置 CA。如果可以,请部署一个测试环境,为了理智起见,您可以不断地拆除和启动它。
如果幸运的话,您会发现 Test
方法中的问题不会改变任何东西。
第一步,进入您运行宁win_dsc
反对的主机。打开 PowerShell。
创建一个包含所有 DSC 模块参数的 [hashtable]
,像这样:
if (-not $cred) {
$cred = Get-Credential # maybe just run this once in your session?
}
$params = @{
IsSingleInstance = $true
CAType = 'EnterpriseRootCA'
CryptoProviderName = 'RSA#Microsoft Software Key Storage Provider'
KeyLength = 2048
HashAlgorithmName = 'SHA256'
ValidityPeriod = 'Years'
ValidityPeriodUnits = 99
PsDscRunAsCredential = $cred
}
接下来,直接调用DSC资源,我们使用Test
方法:
Invoke-DscResource -Name AdcsCertificationAuthority -ModuleName ActiveDirectoryCSDsc -Property $params -Verbose -Method Test
看看它吐出了什么。它可能会因类似的错误而失败。希望它能做到。如果没有,请尝试 Get
方法,以防 Set
使用它但 Test
不使用它。这不太可能,但您希望尽可能避免 Set
。
如果运行一切顺利,运行用方法Set
。如果成功,返回 ansible 并找出不同之处(用户 ansible 是否正在验证是否具有调用 DSC 的权限?)。
如果您在任何时候遇到失败并想要深入挖掘,您可以调试实际的 DSC 调用。有点绕。
第一,Enable-DscDebug -BreakAll
.
接下来,打开一个单独的 PowerShell ISE window(这是我的偏好,让事情变得更容易)。然后,重新运行您之前执行的Invoke-DscResource
命令,在相同的原始window中(不是新的ISE window)。
它会中断,它会给你一系列命令 运行 连接到调试会话。该列表将包括 Enter-PSHostProcess
。 运行 ISE 终端中的那些命令window.
您将进入 运行ning DSC 过程,您将看到模块的源代码并能够逐句查看它并找出问题所在。
此时,您可能会发现您传递的参数不太正确,您可以通过调整它来修复调用。不错。
您可能会发现模块中存在错误,在这种情况下您可以报告错误,甚至可以通过拉取请求提供修复;这需要时间。
与此同时,您可以自己克隆该模块并通过快速修复将其分发到您的服务器,但不符合 PR 的要求。
这里有很多可能性,但如果您发现实际错误,则可能需要提出一个关于如何处理该特定问题的新问题。
备注
我发现在调试过程中,大约有一半的时间连接到会话会导致完全卡住的调试会话无法正常工作。在那种情况下,使用他们给你的 PID 并终止进程。无论如何,您可能必须在 运行 秒之间执行此操作,不要害怕。
最后,在尝试再次使用 DSC 之前(比如从 Ansible 中),不要忘记禁用调试!
Disable-DscDebug
(强烈建议您在禁用调试后终止进程)