我的最终目标是使用 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 是域管理员帐户,所以我相信我的权限应该没问题。
我使用的 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 窗口(这是我的偏好,让事情变得更容易)。然后,在同一个原始窗口(不是新的 ISE 窗口)中重新运行您之前执行的 Invoke-DscResource
命令。
它会中断,它会给你一系列的命令来运行以连接到调试 session 。该列表将包括 Enter-PSHostProcess
。在 ISE 窗口的终端中运行这些命令。
您将进入正在运行的 DSC 进程,您将看到模块的源代码并能够逐步执行它并找出问题所在。
在这一点上,您可能会发现您传递的参数不太正确,并且您可以通过调整它来修复调用。那很好。
您可能会发现模块中存在错误,在这种情况下,您可以报告错误,甚至可以通过拉取请求提供修复;这需要时间。
与此同时,您可以自己克隆模块并将其分发到您的服务器,并快速修复不符合 PR 要求的问题。
这里有很多可能性,但如果您发现实际错误,则可能需要提出一个新问题,即如何处理该特定问题。
注意事项
我发现在调试过程中,大约有一半的时间连接到 session 会导致调试 session 完全卡住,无法正常工作。在那种情况下,使用他们给你的 PID 并终止进程。无论如何,您可能必须在运行之间执行此操作,不要害怕。
最后,在尝试再次使用 DSC 之前(比如从 Ansible 中),不要忘记禁用调试!
Disable-DscDebug
(强烈建议您在禁用调试后终止进程)
关于powershell - 使用 Ansible 执行 Powershell DSC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51544791/