无法使用ansible从捕获的azure映像创建VM

无法使用ansible从捕获的azure映像创建VM,azure,ansible,azure-virtual-machine,Azure,Ansible,Azure Virtual Machine,我正在尝试使用Ansible创建Azure windows映像,作为我的infra自动化项目的一部分。执行剧本时,每件事都会经历: 我的剧本是这样的: --- - hosts: localhost gather_facts: yes roles: - role: azure_vmcreator - hosts: azure_vms gather_facts: no roles: - role: Generalize-windows - hosts: lo

我正在尝试使用Ansible创建Azure windows映像,作为我的infra自动化项目的一部分。执行剧本时,每件事都会经历:

我的剧本是这样的:

---

- hosts: localhost
  gather_facts: yes
  roles:
   - role: azure_vmcreator


- hosts: azure_vms
  gather_facts: no
  roles:
   - role: Generalize-windows



- hosts: localhost
  gather_facts: yes
  roles: 
    - role: azure_imagecreator
有两个角色概括windows和azure imageCreator

generalize-windows.yml从内部概括窗口,如下所示:

---
- name: sysprep windows
  win_shell: Start-Process -FilePath C:\Windows\System32\Sysprep\Sysprep.exe -Verb RunAs -ArgumentList '/generalize /oobe /reboot /quite'

当azure imageCreator解除分配、概括和捕获图像时:

- name: deallocate vm
  azure_rm_virtualmachine:
  resource_group: "{{ resource_group_vm }}"
  name: "{{ vm_name }}{{random_suffix}}-image"
  allocated: no


- name: Call REST API - VirtualMachines_Generalize
  azure_rm_resource:
     api_version: '2017-12-01'
     method: POST
     resource_group: "{{ resource_group_vm }}"
     provider: compute
     resource_type: virtualmachines
     resource_name: "{{ vm_name }}{{random_suffix}}"
     subresource:
       - type: generalize  

- name: Create an image from a virtual machine
  azure_rm_image:
    resource_group: "{{ resource_group_vm }}"
    os_type: Windows
    name: "{{ vm_name }}{{random_suffix}}-image"
    source: "{{ vm_name }}{{random_suffix}}"
现在,当我用ansible创建的映像创建vm时,我得到以下错误:

"details": [
    {
      "code": "Conflict",
      "message": "{\r\n  \"status\": \"Failed\",\r\n  \"error\": {\r\n    \"code\": \"ResourceDeploymentFailure\",\r\n    \"message\": \"The resource operation completed with terminal provisioning state 'Failed'.\",\r\n    \"details\": [\r\n      {\r\n        \"code\": \"OSProvisioningClientError\",\r\n        \"message\": \"OS Provisioning for VM 'p' did not finish in the allotted time. However, the VM guest agent was detected running. This suggests the guest OS has not been properly prepared to be used as a VM image (with CreateOption=FromImage). To resolve this issue, either use the VHD as is with CreateOption=Attach or prepare it properly for use as an image:\\r\\n * Instructions for Windows: https://azure.microsoft.com/documentation/articles/virtual-machines-windows-upload-image/ \\r\\n * Instructions for Linux: https://azure.microsoft.com/documentation/articles/virtual-machines-linux-capture-image/ \"\r\n      }\r\n    ]\r\n  }\r\n}"
    }
  ]
}

有人能告诉我哪里错了吗?

@vinay,在VM释放后,我们需要等待3-4分钟才能完成释放过程。一旦VM停止,继续推广。

@vinay,我也有同样的问题
我遇到的问题是三重的
首先,解除分配导致Azure VM将powersteate更改为stopped。“generalized:true”要求powerstate运行
第二个原因是win_shell的行为与我预期的不一样(我需要更多的阅读),win_命令收到一个错误:“任务执行期间发生异常。要查看完整的回溯,请使用-vvv。错误是:InvalidCredentialsError:指定的凭据被服务器拒绝。”
这是由于sysprep结束了与服务器的所有连接,以便对其进行泛化
第三个是Sysprep运行后的等待时间。

一旦我意识到我需要使用/quit而不是/shutdown,需要使用win_命令,需要等待一段时间,一切都很好
最后看起来是这样的
这是ansible 2.8.6 BTW.

tasks:
  - name: "Sysprep VM"
    win_command: C:\Windows\System32\Sysprep\Sysprep.exe /generalize /oobe /quiet /mode:vm /quit
    ignore_errors: True

  - name: "Sysprep Wait Time"
    pause:
      minutes: 5

  - name: "Generalize VM"
    delegate_to: localhost
    azure_rm_virtualmachine:
      name:           "{{ vmss_template_name }}"
      resource_group: "{{ resource_template_group }}"
      generalized:    true

  - name: "Check if VM is generalized"
    delegate_to: localhost
    azure_rm_virtualmachine_facts:
        resource_group: "{{ resource_template_group }}"
        name: "{{ vmss_template_name }}"
    register: generalized_output

  - assert:
      that: generalized_output.vms[0].power_state == 'generalized'

  - name: "Create Image from VM"
    delegate_to: localhost
    azure_rm_image:
      name:           "{{ vmss_template_name }}"
      resource_group: "{{ resource_template_group }}"
      source:         "{{ vmss_template_name }}"
最后,我确信有更多/更好的方法,但我对ansible还相当陌生,这对我的POC很有用
希望这有帮助


AA

对不起,误读了问题。你是泛化本地虚拟机还是azure虚拟机?好吧,我会同时做这两件事,然后创建一个映像,一旦创建了映像,我就会用它创建一个虚拟机。在VM部署中,我遇到了上面提到的错误。你能帮我理解我哪里出了问题吗?请不要把代码片段作为截图发布,因为这对搜索是有害的,而且在文章中也明确提到了不可以。在未来,请也正确格式化您的YAML,因为它的缩进问题。很抱歉,我会更正它。正如4c所说,您是否如图所示概括VM?因此您建议我现在应该从azure内部概括VM?azure_virtualmachines中的泛化步骤会解决这个问题??嗨,Vinay,我最近完成了这个任务,我的步骤是1)创建VM(主机是本地主机)2)在VM上安装应用程序(主机是目标VM)3)sysprep、泛化和镜像(主机再次是本地主机,因为VM处于停止状态。这看起来像是您的sysprep问题。请尝试对sysprep使用以下命令。win_命令:C:\Windows\System32\sysprep\sysprep.exe/generalize/oobe/shutdown.Hi Bharat,我尝试了win_命令,但它现在正在我的azure VM系统上运行,在进行故障排除时,我发现win_命令uld自行提升以运行sysprep。这很奇怪。您以前遇到过这样的事情吗?我按照您的步骤进行操作,但无法使用Ansible步骤创建的映像部署新vm