= 1.2.8" source = "github.com/hashicorp/amazon" } ansi" /> = 1.2.8" source = "github.com/hashicorp/amazon" } ansi" /> = 1.2.8" source = "github.com/hashicorp/amazon" } ansi"/>

ansible + packer +windows ami giving "msg": "winrm or requests is not installed: No module named 'winrm'"

20 views Asked by At

my packer template looks as follows

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"
    ]
  }
}

while build workfolw of github pipeline looks like below, the workflow will be triggered when it identifies the commit in main branch, as of now i am triggring build for windows build only

# 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 .

when i triggred the pipeline the following error occurs "msg": "winrm or requests is not installed: No module named 'winrm'"

errored screenshot

1

There are 1 answers

1
Karov On

Ansible works via SSH for linux machines and WinRM for Windows. So for any agent that is going to be configured for Windows, you need to have a listener for WinRM first.

Before you use the provisioner ansible on the windows machine, you need to use another provisioner (probably powershell) that starts the service (should already be started by default) and creates a listener for the WinRM module/app so that ansible can later connect to it.


- The WinRM service starts automatically on Windows Server 2008 and later. On earlier versions of Windows (client or server), you need to start the service manually.
- By default, no WinRM listener is configured. Even if the WinRM service is running, WS-Management protocol messages that request data can't be received or sent. Internet Connection Firewall (ICF) blocks access to ports.

As taken from https://learn.microsoft.com/en-us/windows/win32/winrm/installation-and-configuration-for-windows-remote-management