You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ansible+Packer构建Windows AMI时出现winrm模块缺失错误

问题:Packer + Ansible 构建Windows AMI时出现winrm模块缺失错误

在GitHub Actions流水线中,通过Packer模板结合Ansible构建Windows AMI,触发流水线后出现以下错误:

"msg": "winrm or requests is not installed: No module named 'winrm'"

提供的配置代码

Packer模板

packer {
  #plugins
  required_plugins {
    amazon = {
      version = ">= 1.2.8"
      source  = "github.com/hashicorp/amazon"
    }
    ansible = {
      version = ">= 1.1.1"
      source  = "github.com/hashicorp/ansible"
    }
  }
}

locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }

#source account details
source "amazon-ebs" "ubuntu" {
  ami_name      = "learn-packer-linux-aws"
  instance_type = "t2.micro"
  region        = "us-east-1"
  source_ami_filter {
    filters = {
      name                = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }
  ssh_username = "ubuntu"
}

#windows source block
source "amazon-ebs" "windows" {
  ami_name      = "packer-windows-demo-${local.timestamp}"
  instance_type = "t2.micro"
  communicator  = "winrm"
  region        = "us-east-1"
  source_ami_filter {
    filters = {
      name                = "Windows_Server-2022-English-Full-Base-2024.02.14"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["amazon"]
  }
  user_data_file = "./bootstrap_win.txt"
  winrm_username = "Administrator"
  winrm_password = "SuperS3cr3t!!!!"
}

#build section of ubuntu
build {
  name = "learn-packer"
  sources = [
    "source.amazon-ebs.ubuntu"
  ]
  provisioner "ansible" {
    playbook_file = "./playbook.yml"
    user          = "ubuntu"
  }
}

#build section of windows
build {
  name = "learn-packer-windows"
  sources = [
    "source.amazon-ebs.windows"
  ]
  provisioner "ansible" {
    playbook_file = "./win_playbook.yml"
    user          = "Administrator"
    use_proxy       = false
    extra_arguments = [
      "-e","ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignore",
      "-vvvv"
    ]
  }
}

GitHub Actions工作流(原)

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      
      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      - name: Packer Init
        run: packer init .

      # Runs a set of commands using the runners shell
      - name: Packer Build - Branches
        if: "startsWith(github.ref, 'refs/heads/')"
        run: packer build -only=learn-packer-windows.amazon-ebs.windows .

解决方案

错误原因是GitHub Actions使用的ubuntu-latest runner默认没有安装Ansible连接Windows所需的winrm Python模块,需要在流水线中手动添加依赖安装步骤。

修改后的完整工作流

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      
      # Install Ansible and WinRM dependencies
      - name: Install Ansible and WinRM dependencies
        run: |
          sudo apt update
          sudo apt install -y ansible
          ansible-galaxy collection install community.windows
          pip install pywinrm requests

      - name: Packer Init
        run: packer init .

      # Runs a set of commands using the runners shell
      - name: Packer Build - Branches
        if: "startsWith(github.ref, 'refs/heads/')"
        run: packer build -only=learn-packer-windows.amazon-ebs.windows .

关键步骤说明

  • sudo apt install -y ansible:确保runner环境安装完整的Ansible
  • ansible-galaxy collection install community.windows:安装Windows专属的Ansible模块集合,提供常用的Windows管理能力
  • pip install pywinrm requests:安装Ansible连接WinRM服务必需的Python库,直接解决No module named 'winrm'的缺失问题

内容的提问来源于stack exchange,提问作者krishna kanth Gade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:35:03