2

问题: 在运行最终在软件安装后重新启动 Windows VM 的 powershell 脚本时,我无法让 Packer 工作。

脚本完成后(scripts/watchMaker_stigVM.ps1),机器将重新启动,Packer 继续运行下一个 Provisioner。在这种情况下,下一个配置程序是 sysprep 的 Powershell 内联配置程序

Packer 挂起,然后抛出错误,并删除资源进行清理

我尝试过的事情:”

1) 在导致 VM 重新启动的 Powershell 脚本配置程序之后使用“Windows-Restart”Packer 配置程序 (scripts/watchMaker_stigVM.ps1)。

2) 添加“restart_check_command”以检查“RebootPending”注册表项

3) 在Powershell Script Provisioner 之后和Windows -Restart 配置器之前添加一个“Pause_before” ,让机器有时间重新联机,并允许 WinRM 重新连接。

具有上述尝试的供应商:

    "provisioners": [
{
  "type": "powershell",
  "scripts": [
  "scripts/watchMaker_stigVM.ps1"
  ]
},
{
  "type": "powershell",
  "pause_before": "240s",
  "inline": ["dir c:\\"]
},
{
  "type": "windows-restart",
  "timeout": "2h",
  "restart_check_command": "HKLM:SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootPending"
},
{
  "type": "powershell",
  "pause_before": "120s",
  "inline": ["dir c:\\"]
},
{
  "type": "powershell",
  "inline": [
    "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
    "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
    "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"
  ]
}

] }

上述场景中使用的构建脚本,没有任何 Windows-Restart 或 Pause 配置程序:

    {
  "variables": {
    "client_id": "",
    "client_secret": "",
    "object_id": "",
    "tenant_id": "",
    "subscription_id": "",
    "managed_image_name": "",
    "managed_image_resource_group_name": "",
    "location": "",
    "os_type": "",
    "image_publisher": "",
    "image_offer": "",
    "image_sku": "",
    "dept": "",
    "task": ""
  },
  "builders": [
    {
      "type": "azure-arm",
      "client_id": "{{user `client_id`}}",
      "client_secret": "{{user `client_secret`}}",
      "tenant_id": "{{user `tenant_id`}}",
      "subscription_id": "{{user `subscription_id`}}",
      "managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}",
      "managed_image_name": "{{user `managed_image_name`}}",
      "os_type": "{{user `os_type`}}",
      "image_publisher": "{{user `image_publisher`}}",
      "image_offer": "{{user `image_offer`}}",
      "image_sku": "{{user `image_sku`}}",
      "communicator": "winrm",
      "winrm_use_ssl": true,
      "winrm_insecure": true,
      "winrm_timeout": "2h",
      "winrm_username": "packer",
      "azure_tags": {
        "dept": "{{user `dept`}}",
        "task": "{{user `task`}}"
      },
      "location": "{{user `location`}}",
      "vm_size": "Standard_B4ms"
    }
  ],
  "provisioners": [
    {
      "type": "powershell",
      "timeout": "2h",
      "script":
        "scripts/watchMaker_stigVM.ps1"
    },
    {
      "type": "powershell",
      "inline": [
        "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
        "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
        "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"
      ]
    }
  ]
}

Packer 日志,显示 Powershell 脚本“scripts/watchMaker_stigVM.ps1”的成功完成,下一个 Powershell 内联配置器尝试运行,挂起,然后加壳器失败,并删除资源

    ==> azure-arm: 2019-11-08 17:36:51,030 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Applying the salt "highstate", states=highstate
==> azure-arm: 2019-11-08 17:36:51,030 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: Command: C:\Salt\salt-call.bat --local --retcode-passthrough --no-color --config-dir C:\Watchmaker\Salt\conf --log-file C:\Watchmaker\Logs\salt_call.debug.log --log-file-level debug --log-level error --out quiet --return local state.highstate
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: Command retcode: 0
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Salt states all applied successfully!
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Cleanup Time...
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: working_dir=C:\Watchmaker\WorkingFiles\Salt-1_5kqmta
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Deleted working directory...
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Exiting cleanup routine...
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.Client][INFO ][3392]: Reboot scheduled. System will reboot after the script exits.
==> azure-arm: 2019-11-08 17:38:04,648 [watchmaker.Client][INFO ][3392]: Stop time: 2019-11-08 17:38:04.648938
==> azure-arm: Provisioning with Powershell...
==> azure-arm: Provisioning with powershell script: C:\Users\Giann\AppData\Local\Temp\powershell-provisioner582533063
==> azure-arm: Provisioning step had errors: Running the cleanup provisioner, if present...
==> azure-arm:
==> azure-arm: Cleanup requested, deleting resource group ...

如您所见,Powershell 脚本最终使 VM 重新启动,这不是由 Packer 自己启动的。

==> azure-arm:2019-11-08 17:38:04,508 [watchmaker.Client][INFO][3392]:计划重启。脚本退出后系统将重新启动。

以前的 Packer Success 的上下文:

我已经使用 Packer 成功构建了一个 Windows VM,通过 Powershell 脚本配置器安装软件,然后使用 powershell 内联配置器来对映像进行 sysprep。

感谢您对此的任何帮助。

4

0 回答 0